Repository navigation
Expand file tree
/
Copy pathapp.py
More file actions
115 lines (100 loc) · 3.82 KB
/
Copy pathapp.py
File metadata and controls
115 lines (100 loc) · 3.82 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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
"""
app.py - Flask Application
This module serves as the main entry point for our Flask application.
It contains the initialization of the Flask app, routing for different URL endpoints,
and configuration settings. The app is responsible for handling incoming HTTP
requests and generating responses.
Features:
- URL routing: Define blueprints and associated view functions.
- Configuration: Configure the Flask app (e.g., database connections, secret keys).
- App initialization: Create and configure the Flask app instance.
"""
import os
import warnings
import firebase_admin
from flask import Flask
from flask_smorest import Api
from flask_migrate import Migrate
from werkzeug.middleware.proxy_fix import ProxyFix
from dotenv import load_dotenv
from firebase_admin import credentials
from db import db
from jwt_ext import config_jwt
from messages import BEARER_AUTH_DESC, IGNORE_WARNING_MESSAGE
from socket_utils import mysocket
from resources.users import blp as UserBlueprint
from resources.posts import blp as PostBlueprint
from resources.comments import blp as CommentBlueprint
from resources.reply_comments import blp as ReplyCommentBlueprint
from resources.followers import blp as FollowersBlueprint
from resources.feeds import blp as FeedsBlueprint
from resources.chats import blp as ChatBlueprint
from resources.stories import blp as StoriesBlueprint
from tasks import celery_init_app
def create_app():
"""App initialization: Create and configure the Flask app instance."""
app = Flask(__name__)
app = config_app(app=app)
cred = credentials.Certificate("firebase-adminsdk.json")
firebase_admin.initialize_app(cred)
app.wsgi_app = ProxyFix(
app.wsgi_app, x_for=1, x_proto=1, x_host=1, x_prefix=1
)
app = config_jwt(app=app)
db.init_app(app)
Migrate(app, db)
app = config_api(app=app)
celery_init_app(app)
mysocket.init_app(app)
return app
def config_api(app):
"""Configure blueprints and view functions"""
warnings.filterwarnings(
"ignore",
message=IGNORE_WARNING_MESSAGE,
)
API_SPEC_OPTIONS = {
"components": {
"securitySchemes": {
"Bearer Auth": {
"type": "apiKey",
"in": "header",
"name": "Authorization",
"bearerFormat": "JWT",
"description": BEARER_AUTH_DESC,
}
}
},
}
api = Api(app, spec_kwargs=API_SPEC_OPTIONS)
api.register_blueprint(UserBlueprint)
api.register_blueprint(ChatBlueprint)
api.register_blueprint(PostBlueprint)
api.register_blueprint(CommentBlueprint)
api.register_blueprint(ReplyCommentBlueprint)
api.register_blueprint(FollowersBlueprint)
api.register_blueprint(FeedsBlueprint)
api.register_blueprint(StoriesBlueprint)
return app
def config_app(app):
"""Configure the Flask app (e.g., database connections, secret keys)."""
load_dotenv()
app.config["API_TITLE"] = os.getenv("API_TITLE")
app.config["API_VERSION"] = os.getenv("API_VERSION")
app.config["OPENAPI_VERSION"] = os.getenv("OPENAPI_VERSION")
app.config["OPENAPI_URL_PREFIX"] = os.getenv("OPENAPI_URL_PREFIX")
app.config["OPENAPI_SWAGGER_UI_PATH"] = os.getenv(
"OPENAPI_SWAGGER_UI_PATH"
)
app.config["OPENAPI_SWAGGER_UI_URL"] = os.getenv("OPENAPI_SWAGGER_UI_URL")
app.config["SQLALCHEMY_DATABASE_URI"] = os.getenv("DATABASE_URL")
app.config["PROPAGATE_EXCEPTIONS"] = True
app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = False
app.config["JWT_SECRET_KEY"] = os.getenv("JWT_SECRET_KEY")
app.config["CELERY"] = {
'broker_url': os.getenv("CELERY_BROKER_URL"),
'result_backend': os.getenv("CELERY_BACKEND_URL"),
'task_ignore_result': True,
'broker_connection_retry_on_startup': False
}
return app