forked from codeforpdx/recordexpungPDX
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
60 lines (40 loc) · 1.32 KB
/
app.py
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
import pkgutil
import sys
from importlib import import_module
from flask import Flask, g
from flask_session import Session # type: ignore
from os import path
from expungeservice.database import get_database
from .config import app_config
from . import endpoints
import logging
from .loggers import attach_logger
FRONTEND_BUILD_DIR = path.abspath(path.join(
path.dirname(__file__), "..", "..", "frontend", "build"))
def before():
g.database = get_database()
def after(response):
g.database.connection.commit()
return response
def create_app(env_name):
"""
Create app
"""
# app initiliazation
app = Flask(__name__, static_folder=FRONTEND_BUILD_DIR)
app.config.from_object(app_config[env_name])
sess = Session()
sess.init_app(app)
attach_logger(app)
app.logger.setLevel(logging.DEBUG)
__register_endpoints(app)
app.before_request(before)
app.after_request(after)
app.teardown_request(lambda exception: g.database.close_connection())
sys.setrecursionlimit(15000)
return app
def __register_endpoints(app):
for _, module_name, _ in pkgutil.iter_modules(endpoints.__path__): # type: ignore # mypy issue #1422
module = import_module(f"{endpoints.__name__}.{module_name}")
register_fn = getattr(module, "register")
register_fn(app)