-
Notifications
You must be signed in to change notification settings - Fork 116
Expand file tree
/
Copy path__init__.py
More file actions
64 lines (53 loc) · 1.99 KB
/
Copy path__init__.py
File metadata and controls
64 lines (53 loc) · 1.99 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
# type:ignore
import string
from typing import Any
from sqlalchemy import MetaData
from flask import Flask
from flask_cors import CORS
from flask_sqlalchemy import SQLAlchemy
from flask_caching import Cache
from flask_compress import Compress
from application.config import CMDConfig, config
import os
import random
convention = {
"ix": "ix_%(column_0_label)s",
"uq": "uq_%(table_name)s_%(column_0_name)s",
"ck": "ck_%(table_name)s_%(constraint_name)s",
"fk": "fk_%(table_name)s_%(column_0_name)s_%(referred_table_name)s",
"pk": "pk_%(table_name)s",
}
metadata = MetaData(naming_convention=convention)
sqla = SQLAlchemy(metadata=metadata)
compress = Compress()
cache = Cache()
def create_app(mode: str = "production", conf: any = None) -> Any:
app = Flask(__name__)
if not conf:
app.config.from_object(config[mode])
else:
app.config.from_object(conf)
# CLI passes an explicit URI via ``CMDConfig`` (``cre_main.db_connect``). A
# leftover ``SQLALCHEMY_DATABASE_URI`` in the environment (e.g. sqlite from a
# prior shell export) must not override it — that produced sqlite errors
# against a Heroku ``DATABASE_URL`` session.
if os.environ.get("SQLALCHEMY_DATABASE_URI") and not isinstance(conf, CMDConfig):
app.config["SQLALCHEMY_DATABASE_URI"] = os.environ.get(
"SQLALCHEMY_DATABASE_URI"
)
GOOGLE_CLIENT_SECRET = os.environ.get("GOOGLE_CLIENT_SECRET")
app.secret_key = GOOGLE_CLIENT_SECRET
if os.environ.get("NO_LOGIN"):
letters = string.ascii_lowercase
app.secret_key = "".join(random.choice(letters) for i in range(20))
# config[mode].init_app(app)
sqla.init_app(app=app)
from application.web.web_main import app as app_blueprint
app.register_blueprint(app_blueprint)
from application.web.openapi_registry import init_openapi
init_openapi(app)
CORS(app)
app.config["CORS_HEADERS"] = "Content-Type"
compress.init_app(app)
cache.init_app(app)
return app