-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
executable file
·64 lines (45 loc) · 1.29 KB
/
app.py
File metadata and controls
executable file
·64 lines (45 loc) · 1.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import sys
import logging
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
from flask_migrate import Migrate
from flask_jwt_extended import JWTManager
from flask_cors import CORS
from flask_restful import Api
from config import Config
logger = logging.getLogger(__name__)
logger.addHandler(logging.StreamHandler(sys.stdout))
# db
db = SQLAlchemy()
migrate = Migrate()
# jwt
jwt = JWTManager()
jwt.invalid_token_loader(lambda *_: ({'token': 'Token is invalid'}, 400))
jwt.expired_token_loader(lambda *_: ({'token': 'Token has expired'}, 401))
# cors
cors = CORS()
# api
api = Api()
def create_app(config_class=Config):
app = Flask(__name__)
app.config.from_object(config_class)
# init db
db.init_app(app)
migrate.init_app(app, db)
# init jwt
jwt.init_app(app)
# init cors
cors.init_app(app, resources={r'*': {'origins': '*'}})
# url converters
from commons.url_converters import DatetimeConverter
app.url_map.converters['datetime'] = DatetimeConverter
# blueprints
from api import api_blueprint
api.init_app(api_blueprint)
app.register_blueprint(api_blueprint, url_prefix='/api')
app.app_context().push()
db.create_all()
return app
created_app = create_app()
if __name__ == '__main__':
created_app.run()