forked from PnX-SI/GeoNature-atlas
-
Notifications
You must be signed in to change notification settings - Fork 0
/
initAtlas.py
92 lines (69 loc) · 2.72 KB
/
initAtlas.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
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
import os
import sys
from flask import Flask, render_template
from flask_sqlalchemy import SQLAlchemy
from werkzeug.serving import run_simple
from atlas.configuration import config
from atlas.utils import format_number
from sqlalchemy import create_engine, MetaData, Table
from flask_compress import Compress
from atlas.configuration.config_parser import read_and_validate_conf
from atlas.configuration.config_schema import AtlasConfig, SecretSchemaConf
from atlas.configuration import config
db = SQLAlchemy()
compress = Compress()
APP_DIR = os.path.abspath(os.path.dirname(__file__))
class ReverseProxied(object):
def __init__(self, app, script_name=None, scheme=None, server=None):
self.app = app
self.script_name = script_name
self.scheme = scheme
self.server = server
def __call__(self, environ, start_response):
script_name = environ.get("HTTP_X_SCRIPT_NAME", "") or self.script_name
if script_name:
environ["SCRIPT_NAME"] = script_name
path_info = environ["PATH_INFO"]
if path_info.startswith(script_name):
environ["PATH_INFO"] = path_info[len(script_name) :]
scheme = environ.get("HTTP_X_SCHEME", "") or self.scheme
if scheme:
environ["wsgi.url_scheme"] = scheme
server = environ.get("HTTP_X_FORWARDED_SERVER", "") or self.server
if server:
environ["HTTP_HOST"] = server
return self.app(environ, start_response)
def create_app():
"""
renvoie une instance de l'app Flask
"""
# validation de la configuration
# configuration publique
valid_config = read_and_validate_conf(config, AtlasConfig)
app = Flask(__name__, template_folder=APP_DIR)
# push the config in app config at 'PUBLIC' key
app.config.update(valid_config)
app.debug = valid_config["modeDebug"]
with app.app_context() as context:
from atlas.atlasRoutes import main as main_blueprint
app.register_blueprint(main_blueprint)
from atlas.atlasAPI import api
app.register_blueprint(api, url_prefix="/api")
compress.init_app(app)
app.wsgi_app = ReverseProxied(
app.wsgi_app, script_name=valid_config["URL_APPLICATION"]
)
@app.context_processor
def inject_config():
return dict(configuration=valid_config)
@app.template_filter("pretty")
def pretty(val):
return format_number(val)
return app
app = create_app()
if __name__ == "__main__":
# validation de la configuration secrète
secret_conf = read_and_validate_conf(config, SecretSchemaConf)
app.run(
host="0.0.0.0", port=secret_conf["GUNICORN_PORT"], debug=app.config["modeDebug"]
)