Skip to content

Commit 3025242

Browse files
authored
Merge pull request #5 from josethz00/final_example
Final example
2 parents 647a9c9 + 114b116 commit 3025242

22 files changed

+479
-62
lines changed

.vscode/settings.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
"python.linting.flake8Enabled": true,
44
"python.linting.flake8Args": [
55
"--ignore=E303",
6-
"--ignore=E302"
6+
"--ignore=E302",
7+
"--ignore=F401"
78
],
9+
"python.pythonPath": "server/.venv"
810
}

README.md

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -29,28 +29,15 @@ $ pip3 install -r requirements.txt
2929
-----------------------------------
3030

3131
```
32-
$ python3 app.py
32+
$ python3 run.py
3333
```
3434

35-
5- Start redis-server (if is not active)
36-
-----------------------------------
37-
```
38-
$ redis-server --daemonize yes
39-
```
40-
41-
6- Start celery worker (if your application needs)
42-
-----------------------------------
43-
44-
```
45-
$ celery -A tasks app.celery --loglevel=INFO
46-
```
4735
<br />
4836

49-
## Check the default env variables (available on ``` config/variables.py ```)
37+
## Check some default env variables (available on ``` config/variables.py ```)
5038

5139
| Env Variable | Default Value |
5240
| ------------- | ------------- |
53-
| REDIS_URL | 'redis://localhost:6379/0' |
5441
| HOST | '0.0.0.0' |
5542
| PORT | '8080' |
5643
| DEBUG | 'True' |

server/.editorconfig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,4 @@ indent_size = 4
99
end_of_line = lf
1010
charset = utf-8
1111
trim_trailing_whitespace = true
12-
insert_final_newline = true
12+
insert_final_newline = true

server/.gitignore

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
.env
1+
.venv
22
env
33
__pycache__
4-
dump.rdb
4+
dump.rdb
5+
.vscode

server/app.py

Lines changed: 41 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,55 @@
11
from flask import Flask
22
from flask_cors import CORS
3-
from celery import Celery
43
from werkzeug.exceptions import HTTPException
4+
from flask_mail import Mail
55

6-
from src.shared.utils.handle_exception import handle_exception
6+
from src.shared.utils.exception_handler import exception_handler
77
from src.modules.projects.project_blueprint import project_bp
88
from src.modules.users.user_blueprint import user_bp
99
from src.modules.tasks.task_blueprint import task_bp
10-
from config import variables
10+
from src.shared.database.sql_connector import SQLConnector
11+
from src.shared.database.db import db
12+
from config import variables, extensions
13+
import manage
1114

12-
app = Flask(__name__)
13-
CORS(app)
1415

15-
app.register_blueprint(user_bp)
16-
app.register_blueprint(project_bp)
17-
app.register_blueprint(task_bp)
16+
def create_app() -> Flask:
1817

19-
app.config['CELERY_BROKER_URL'] = variables.REDIS_URL
20-
app.config['CELERY_RESULT_BACKEND'] = variables.REDIS_URL
18+
app = Flask(__name__)
19+
db_conn = SQLConnector()
2120

22-
celery = Celery(app.name, broker=app.config['CELERY_BROKER_URL'])
23-
celery.conf.update(app.config)
21+
configure_cors(app, extensions.cors)
2422

25-
@app.errorhandler(HTTPException)
26-
def get_error(error):
27-
return handle_exception(error)
23+
app.config['SQLALCHEMY_DATABASE_URI'] = db_conn.connection_url
24+
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
2825

26+
app.config['MAIL_SERVER'] = variables.MAIL_SERVER
27+
app.config['MAIL_PORT'] = variables.MAIL_PORT
28+
app.config['MAIL_USERNAME'] = variables.MAIL_USERNAME
29+
app.config['MAIL_PASSWORD'] = variables.MAIL_PASSWORD
30+
app.config['MAIL_USE_TLS'] = bool(variables.MAIL_USE_TLS)
31+
app.config['MAIL_USE_SSL'] = bool(variables.MAIL_USE_SSL)
2932

30-
if __name__ == '__main__':
31-
app.run(
32-
host=variables.HOST,
33-
port=variables.PORT,
34-
debug=bool(variables.DEBUG)
35-
)
33+
db.init_app(app)
34+
with app.app_context():
35+
db.create_all()
36+
37+
configure_mail(app, extensions.mail)
38+
39+
app.register_blueprint(user_bp)
40+
app.register_blueprint(project_bp)
41+
app.register_blueprint(task_bp)
42+
43+
@app.errorhandler(Exception)
44+
def get_error(error):
45+
return exception_handler(error)
46+
47+
return app
48+
49+
50+
def configure_mail(app: Flask, mail: Mail) -> None:
51+
mail.init_app(app)
52+
53+
54+
def configure_cors(app: Flask, cors: CORS) -> None:
55+
cors.init_app(app)

server/config/extensions.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
from flask_mail import Mail
2+
from flask_cors import CORS
3+
4+
from config import variables
5+
6+
7+
mail = Mail()
8+
cors = CORS()

server/config/variables.py

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,35 @@
33
os.environ['HOST'] = '0.0.0.0'
44
os.environ['PORT'] = '8080'
55
os.environ['DEBUG'] = 'True'
6-
os.environ['REDIS_URL'] = 'redis://localhost:6379/0'
6+
os.environ['SQL_DATABASE'] = 'postgresql'
7+
os.environ['DATABASE_USER'] = 'postgres'
8+
os.environ['DATABASE_PASSWORD'] = 'docker'
9+
os.environ['DATABASE_NAME'] = 'flask_tutorial'
10+
os.environ['DATABASE_HOST'] = 'localhost'
11+
os.environ['DATABASE_PORT'] = '5433'
12+
os.environ['FLASK_ENV'] = 'development'
13+
os.environ['SECRET_KEY'] = '@flask/7c4ed787-e708-4fe1-837d-6beb3320697f'
14+
os.environ['MAIL_SERVER'] = 'smtp.mailtrap.io'
15+
os.environ['MAIL_PORT'] = '2525'
16+
os.environ['MAIL_USERNAME'] = 'e67f4bf7c074a1'
17+
os.environ['MAIL_PASSWORD'] = '6f26693be07364'
18+
os.environ['MAIL_USE_TLS'] = 'True'
19+
os.environ['MAIL_USE_SSL'] = 'False'
720

821
HOST = os.environ.get('HOST')
922
PORT = os.environ.get('PORT')
1023
DEBUG = os.environ.get('DEBUG')
11-
REDIS_URL = os.environ.get('REDIS_URL')
24+
SQL_DATABASE = os.environ.get('SQL_DATABASE')
25+
DATABASE_USER = os.environ.get('DATABASE_USER')
26+
DATABASE_PASSWORD = os.environ.get('DATABASE_PASSWORD')
27+
DATABASE_NAME = os.environ.get('DATABASE_NAME')
28+
DATABASE_HOST = os.environ.get('DATABASE_HOST')
29+
DATABASE_PORT = os.environ.get('DATABASE_PORT')
30+
FLASK_ENV = os.environ.get('FLASK_ENV')
31+
SECRET_KEY = os.environ.get('SECRET_KEY')
32+
MAIL_SERVER = os.environ.get('MAIL_SERVER')
33+
MAIL_PORT = os.environ.get('MAIL_PORT')
34+
MAIL_USERNAME = os.environ.get('MAIL_USERNAME')
35+
MAIL_PASSWORD = os.environ.get('MAIL_PASSWORD')
36+
MAIL_USE_TLS = os.environ.get('MAIL_USE_TLS')
37+
MAIL_USE_SSL = os.environ.get('MAIL_USE_SSL')

server/manage.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
from src.modules.users.user import User
2+
from src.modules.projects.project import Project
3+
from src.modules.tasks.task import Task

server/requirements.txt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
flask
2+
psycopg2-binary
23
flask_sqlalchemy
34
flask_cors
45
gunicorn
5-
celery
6-
redis
6+
Flask-Mail
7+
PyJWT

server/run.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
from app import create_app
2+
from config import variables
3+
4+
5+
if __name__ == '__main__':
6+
app = create_app()
7+
app.app_context().push()
8+
app.run(
9+
host=variables.HOST,
10+
port=int(variables.PORT),
11+
debug=bool(variables.DEBUG)
12+
)

0 commit comments

Comments
 (0)