|
1 | | -from pathlib import Path |
2 | | -from redis import Redis |
3 | | - |
4 | | -from flask import Flask, redirect, send_from_directory |
5 | | -from flask_cors import CORS |
6 | | - |
7 | | -import json |
8 | | -from os import getenv |
9 | | - |
10 | | - |
11 | | -def create_app(test_config: dict = None) -> Flask: |
12 | | - """Create the main Flask application |
13 | | -
|
14 | | - Args: |
15 | | - test_config (dict): A dictionry containing a configuration during tests |
16 | | -
|
17 | | - Returns: |
18 | | - Flask: The application |
19 | | - """ |
20 | | - |
21 | | - redis_host = getenv("REDIS_HOST", "localhost") |
22 | | - redis_port = getenv("REDIS_PORT", 6379) |
23 | | - |
24 | | - app = Flask(__name__, instance_relative_config=True) |
25 | | - app.config.from_mapping( |
26 | | - CA_PUBKEY=None, |
27 | | - STORE_PATH=app.instance_path + "/public/store", |
28 | | - JSON_PATH=app.instance_path + "/public/json", |
29 | | - CACHE_PATH=app.instance_path + "/cache/", |
30 | | - REDIS_CONN=Redis(host=redis_host, port=redis_port), |
31 | | - TESTING=False, |
32 | | - DEBUG=False, |
33 | | - UPSTREAM_URL="https://downloads.cdn.openwrt.org", |
34 | | - BRANCHES={}, |
35 | | - ) |
36 | | - |
37 | | - if not test_config: |
38 | | - for config_file in [ |
39 | | - "./config.py", |
40 | | - app.instance_path + "/config.py", |
41 | | - "/etc/asu/config.py", |
42 | | - ]: |
43 | | - if Path(config_file).exists(): |
44 | | - app.config.from_pyfile(config_file, silent=True) |
45 | | - else: |
46 | | - app.config.from_mapping(test_config) |
47 | | - |
48 | | - for option, value in app.config.items(): |
49 | | - if option.endswith("_PATH") and isinstance(value, str): |
50 | | - app.config[option] = Path(value) |
51 | | - app.config[option].mkdir(parents=True, exist_ok=True) |
52 | | - |
53 | | - Path(app.instance_path).mkdir(exist_ok=True, parents=True) |
54 | | - |
55 | | - CORS(app, resources={r"/api/*": {"origins": "*"}}) |
56 | | - |
57 | | - # only serve files in DEBUG/TESTING mode |
58 | | - # production should use nginx for static files |
59 | | - if app.config["DEBUG"] or app.config["TESTING"]: |
60 | | - |
61 | | - @app.route("/") |
62 | | - @app.route("/<path:path>") |
63 | | - def root(path="index.html"): |
64 | | - return send_from_directory(Path(app.instance_path) / "public", path) |
65 | | - |
66 | | - else: |
67 | | - |
68 | | - @app.route("/") |
69 | | - def root(path="index.html"): |
70 | | - return redirect("https://github.com/aparcar/asu/#api") |
71 | | - |
72 | | - from . import janitor |
73 | | - |
74 | | - app.register_blueprint(janitor.bp) |
75 | | - |
76 | | - from . import api |
77 | | - |
78 | | - app.register_blueprint(api.bp) |
79 | | - |
80 | | - (app.config["JSON_PATH"] / "branches.json").write_text( |
81 | | - json.dumps(app.config["BRANCHES"]) |
82 | | - ) |
83 | | - |
84 | | - return app |
| 1 | +__version__ = "0.5.0" |
0 commit comments