mat2-web/main.py

74 lines
2.4 KiB
Python
Raw Normal View History

2018-11-11 23:25:40 +01:00
import os
import jinja2
2020-07-14 15:28:08 +02:00
import yaml
2018-11-11 23:25:40 +01:00
2020-04-23 10:39:35 -07:00
from matweb import utils, rest_api, frontend
2020-07-13 16:01:32 +02:00
from flask import Flask, request
from flask_cors import CORS
2020-07-13 16:01:32 +02:00
from flasgger import Swagger, LazyString, LazyJSONEncoder
from flask_assets import Bundle, Environment
2018-11-11 23:25:40 +01:00
def create_app(test_config=None):
app = Flask(__name__)
app.config['SECRET_KEY'] = os.urandom(32)
2020-03-31 20:33:06 +02:00
app.config['UPLOAD_FOLDER'] = os.environ.get('MAT2_WEB_DOWNLOAD_FOLDER', './uploads/')
app.config['MAX_CONTENT_LENGTH'] = 16 * 1024 * 1024 # 16MB
app.config['CUSTOM_TEMPLATES_DIR'] = 'custom_templates'
2020-03-27 10:59:16 -07:00
# optionally load settings from config.py
app.config.from_object('config')
if test_config is not None:
app.config.update(test_config)
2018-11-11 23:25:40 +01:00
2020-04-23 10:39:35 -07:00
# Non JS Frontend
assets = Environment(app)
2021-03-21 15:31:51 +01:00
css = Bundle("src/main.css", output="dist/main.css")
assets.register("css", css)
css.build()
app.jinja_loader = jinja2.ChoiceLoader([ # type: ignore
jinja2.FileSystemLoader(app.config['CUSTOM_TEMPLATES_DIR']),
app.jinja_loader,
2020-03-27 10:59:16 -07:00
])
2020-04-23 10:39:35 -07:00
app.register_blueprint(frontend.routes)
2018-11-11 23:25:40 +01:00
2020-04-23 10:39:35 -07:00
# Restful API hookup
app.register_blueprint(rest_api.api_bp)
2023-06-04 16:44:53 +02:00
app.json_provider_class = LazyJSONEncoder
app.json = LazyJSONEncoder(app)
2020-07-13 16:01:32 +02:00
2020-07-14 15:28:08 +02:00
dirname = os.path.dirname(__file__)
with open(os.path.join(dirname, 'matweb/oas/components.yml')) as file:
components = yaml.full_load(file)
2020-07-13 16:01:32 +02:00
template = dict(
2020-07-14 15:28:08 +02:00
servers=[
{
'url': LazyString(lambda: request.host_url),
'description': 'References the current running server'
}
],
2020-07-13 16:01:32 +02:00
info={
'title': 'Mat2 Web API',
'version': '1',
'description': 'Mat2 Web RESTful API documentation',
2020-07-14 15:28:08 +02:00
},
components=components
2020-07-13 16:01:32 +02:00
)
2020-07-14 15:28:08 +02:00
swagger_config = Swagger.DEFAULT_CONFIG
swagger_config['swagger_ui_bundle_js'] = '//unpkg.com/swagger-ui-dist@3/swagger-ui-bundle.js'
swagger_config['swagger_ui_standalone_preset_js'] = '//unpkg.com/swagger-ui-dist@3/swagger-ui-standalone-preset.js'
swagger_config['swagger_ui_css'] = '//unpkg.com/swagger-ui-dist@3/swagger-ui.css'
swagger_config['openapi'] = "3.0.3"
Swagger(app, template=template, config=swagger_config)
CORS(app, resources={r"/api/*": {"origins": utils.get_allow_origin_header_value()}})
app.logger.info('mat2-web started')
return app
2018-11-11 23:25:40 +01:00
2019-09-21 05:58:05 -07:00
2019-08-28 08:33:28 -07:00
app = create_app()
2018-11-11 23:25:40 +01:00
2018-12-16 21:41:23 +01:00
if __name__ == '__main__': # pragma: no cover
2019-08-28 08:33:28 -07:00
app.run()