main.py 1.66 KB
Newer Older
jvoisin's avatar
jvoisin committed
1
import os
jfriedli's avatar
jfriedli committed
2
import jinja2
jvoisin's avatar
jvoisin committed
3

jfriedli's avatar
jfriedli committed
4 5 6
from matweb import utils, rest_api, frontend
from flask import Flask
from flask_restful import Api
jfriedli's avatar
jfriedli committed
7
from flask_cors import CORS
jvoisin's avatar
jvoisin committed
8 9


jfriedli's avatar
jfriedli committed
10 11 12
def create_app(test_config=None):
    app = Flask(__name__)
    app.config['SECRET_KEY'] = os.urandom(32)
jfriedli's avatar
jfriedli committed
13
    app.config['UPLOAD_FOLDER'] = os.environ.get('MAT2_WEB_DOWNLOAD_FOLDER', './uploads/')
jfriedli's avatar
jfriedli committed
14 15
    app.config['MAX_CONTENT_LENGTH'] = 16 * 1024 * 1024  # 16MB
    app.config['CUSTOM_TEMPLATES_DIR'] = 'custom_templates'
jfriedli's avatar
jfriedli committed
16 17 18 19 20
    # optionally load settings from config.py
    app.config.from_object('config')

    if test_config is not None:
        app.config.update(test_config)
jvoisin's avatar
jvoisin committed
21

jfriedli's avatar
jfriedli committed
22
    # Non JS Frontend
jfriedli's avatar
jfriedli committed
23 24 25
    app.jinja_loader = jinja2.ChoiceLoader([  # type: ignore
        jinja2.FileSystemLoader(app.config['CUSTOM_TEMPLATES_DIR']),
        app.jinja_loader,
jfriedli's avatar
jfriedli committed
26
    ])
jfriedli's avatar
jfriedli committed
27
    app.register_blueprint(frontend.routes)
jvoisin's avatar
jvoisin committed
28

jfriedli's avatar
jfriedli committed
29
    # Restful API hookup
jfriedli's avatar
jfriedli committed
30 31
    api = Api(app)
    CORS(app, resources={r"/api/*": {"origins": utils.get_allow_origin_header_value()}})
jfriedli's avatar
jfriedli committed
32 33 34 35 36 37 38
    api.add_resource(
        rest_api.APIUpload,
        '/api/upload',
        resource_class_kwargs={'upload_folder': app.config['UPLOAD_FOLDER']}
    )
    api.add_resource(
        rest_api.APIDownload,
39
        '/api/download/<string:key>/<string:secret>/<string:filename>',
jfriedli's avatar
jfriedli committed
40 41 42 43 44 45 46 47
        resource_class_kwargs={'upload_folder': app.config['UPLOAD_FOLDER']}
    )
    api.add_resource(
        rest_api.APIBulkDownloadCreator,
        '/api/download/bulk',
        resource_class_kwargs={'upload_folder': app.config['UPLOAD_FOLDER']}
    )
    api.add_resource(rest_api.APISupportedExtensions, '/api/extension')
jvoisin's avatar
jvoisin committed
48

jfriedli's avatar
jfriedli committed
49
    return app
jvoisin's avatar
jvoisin committed
50

jfriedli's avatar
jfriedli committed
51

jfriedli's avatar
jfriedli committed
52
app = create_app()
jvoisin's avatar
jvoisin committed
53

jvoisin's avatar
jvoisin committed
54
if __name__ == '__main__':  # pragma: no cover
jfriedli's avatar
jfriedli committed
55
    app.run()