2020-05-09 18:00:07 +00:00
|
|
|
from flask.app import Flask
|
2023-10-09 19:58:44 +00:00
|
|
|
from flask import g
|
|
|
|
from flask.json.provider import DefaultJSONProvider
|
2020-05-09 18:00:07 +00:00
|
|
|
import time
|
2020-05-10 14:54:53 +00:00
|
|
|
import subprocess
|
2022-06-17 11:38:49 +00:00
|
|
|
from lenticular_cloud.lenticular_services import lenticular_services
|
2022-02-06 22:57:01 +00:00
|
|
|
import os
|
2023-10-09 19:58:44 +00:00
|
|
|
import toml
|
|
|
|
import json
|
|
|
|
import logging
|
|
|
|
from uuid import UUID
|
2020-05-09 18:00:07 +00:00
|
|
|
|
2022-04-08 19:29:23 +00:00
|
|
|
from pathlib import Path
|
2020-05-09 18:00:07 +00:00
|
|
|
|
2022-06-17 11:38:49 +00:00
|
|
|
from .pki import pki
|
2022-02-19 22:16:13 +00:00
|
|
|
from .hydra import hydra_service
|
|
|
|
from .translations import init_babel
|
2022-06-17 11:38:49 +00:00
|
|
|
from .model import db, migrate
|
|
|
|
from .views import auth_views, frontend_views, init_login_manager, api_views, pki_views, admin_views, oauth2_views
|
2020-05-09 18:00:07 +00:00
|
|
|
|
2023-10-09 19:58:44 +00:00
|
|
|
logger = logging.getLogger(__name__)
|
2020-05-09 18:00:07 +00:00
|
|
|
|
2020-05-10 14:54:53 +00:00
|
|
|
def get_git_hash():
|
|
|
|
try:
|
|
|
|
return subprocess.check_output(['git', 'rev-parse', 'HEAD'])[:10].decode()
|
|
|
|
except Exception:
|
|
|
|
return ''
|
|
|
|
|
2020-05-09 18:00:07 +00:00
|
|
|
|
2023-10-09 19:58:44 +00:00
|
|
|
|
|
|
|
class CustomJSONEncoder(DefaultJSONProvider):
|
|
|
|
def default(self, obj):
|
|
|
|
if isinstance(obj, UUID):
|
|
|
|
# if the obj is uuid, we simply return the value of uuid
|
|
|
|
return obj.hex
|
|
|
|
return super().default(obj)
|
|
|
|
|
|
|
|
|
|
|
|
def create_app_raw(config_files: list[Path]) -> Flask:
|
2022-02-06 22:57:01 +00:00
|
|
|
name = "lenticular_cloud"
|
|
|
|
app = Flask(name, template_folder='template')
|
2023-10-09 19:58:44 +00:00
|
|
|
app.json_provider_class = CustomJSONEncoder
|
|
|
|
|
|
|
|
# config
|
|
|
|
app.config.from_file('config_development.toml', toml.load)
|
|
|
|
for config_file in config_files:
|
|
|
|
active_cfg = str(config_file.absolute())
|
|
|
|
if active_cfg.endswith(".toml"):
|
|
|
|
logger.info(f"load toml config file from {active_cfg}")
|
|
|
|
app.config.from_file(active_cfg, toml.load)
|
|
|
|
elif active_cfg.endswith(".json"):
|
|
|
|
logger.info(f"load json config file from {active_cfg}")
|
|
|
|
app.config.from_file(active_cfg, json.load)
|
|
|
|
else:
|
|
|
|
logger.info(f"load pyfile config file from {active_cfg}")
|
|
|
|
app.config.from_pyfile(active_cfg)
|
2020-05-10 14:54:53 +00:00
|
|
|
app.jinja_env.globals['GIT_HASH'] = get_git_hash()
|
|
|
|
|
2020-05-21 11:20:27 +00:00
|
|
|
db.init_app(app)
|
2022-04-08 19:29:23 +00:00
|
|
|
migration_dir = Path(app.root_path) / 'migrations'
|
|
|
|
migrate.init_app(app, db, directory=str(migration_dir))
|
2022-02-20 15:52:06 +00:00
|
|
|
# with app.app_context():
|
|
|
|
# db.create_all()
|
2020-05-21 11:20:27 +00:00
|
|
|
|
2022-02-19 22:16:13 +00:00
|
|
|
init_babel(app)
|
2020-05-09 18:00:07 +00:00
|
|
|
|
2020-05-21 11:20:27 +00:00
|
|
|
#init hydra admin api
|
2022-02-19 22:16:13 +00:00
|
|
|
# hydra_config = hydra.Configuration(
|
|
|
|
# host=app.config['HYDRA_ADMIN_URL'],
|
|
|
|
# username=app.config['HYDRA_ADMIN_USER'],
|
|
|
|
# password=app.config['HYDRA_ADMIN_PASSWORD'])
|
2023-10-09 19:58:44 +00:00
|
|
|
hydra_service.init_app(app)
|
2022-02-19 22:16:13 +00:00
|
|
|
|
2023-10-09 19:58:44 +00:00
|
|
|
init_login_manager(app) # has to be after hydra_service
|
2020-05-09 18:00:07 +00:00
|
|
|
app.register_blueprint(auth_views)
|
|
|
|
app.register_blueprint(frontend_views)
|
2020-05-21 11:20:27 +00:00
|
|
|
app.register_blueprint(api_views)
|
2020-05-25 18:23:27 +00:00
|
|
|
app.register_blueprint(pki_views)
|
2020-06-01 21:43:10 +00:00
|
|
|
app.register_blueprint(admin_views)
|
2022-02-19 22:16:13 +00:00
|
|
|
app.register_blueprint(oauth2_views)
|
2020-05-09 18:00:07 +00:00
|
|
|
|
|
|
|
@app.before_request
|
|
|
|
def befor_request():
|
|
|
|
request_start_time = time.time()
|
|
|
|
g.request_time = lambda: "%.5fs" % (time.time() - request_start_time)
|
|
|
|
|
2022-06-17 11:38:49 +00:00
|
|
|
lenticular_services.init_app(app)
|
2020-05-09 18:00:07 +00:00
|
|
|
|
2022-06-17 11:38:49 +00:00
|
|
|
pki.init_app(app)
|
2020-05-09 18:00:07 +00:00
|
|
|
|
|
|
|
return app
|
|
|
|
|
2023-10-09 19:58:44 +00:00
|
|
|
|
|
|
|
def create_app() -> Flask:
|
|
|
|
evn_var = os.getenv('CONFIG_FILE', None)
|
|
|
|
if isinstance(evn_var, str):
|
|
|
|
active_cfgs = list(map(Path, evn_var.split(':')))
|
|
|
|
else:
|
|
|
|
active_cfgs = [ Path() / 'production.toml' ]
|
|
|
|
|
|
|
|
return create_app_raw(active_cfgs)
|