~sircmpwn/core.sr.ht

core.sr.ht/srht/debug.py -rw-r--r-- 1.7 KiB
b695e020Drew DeVault Add "internal_anon" path for internal auth tokens 3 days ago
                                                                                
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
from srht.config import cfg, cfgi
import os.path
import sys
import argparse
import importlib

_auto_set_static_folder = object()

def configure_static_folder(app, static_folder=_auto_set_static_folder):
    if static_folder:
        if static_folder is _auto_set_static_folder:
            mod = sys.modules[app.__module__]
            app.static_folder = os.path.join(
                os.path.dirname(os.path.dirname(mod.__file__)),
                "static")
        else:
            app.static_folder = static_folder

def configure_static_arguments(parser):
    parser.add_argument(
        '--static',
        action='store_true',
        help="Serve static assets through the development server.")

def configure_static_serving(app, args):
    if args.static and app.static_folder:
        try:
            from werkzeug.middleware.shared_data import SharedDataMiddleware
        except ImportError:
            from werkzeug.wsgi import SharedDataMiddleware

        print("Serving static assets from: {}".format(app.static_folder))
        app.wsgi_app = SharedDataMiddleware(app.wsgi_app, {
            '/static': app.static_folder
        })

def build_parser(app):
    parser = argparse.ArgumentParser(
        description='Development server for %s' % app.site)
    return parser

def run_app(app):
    cfg_section = app.site
    app.run(host=cfg(cfg_section, "debug-host"),
            port=cfgi(cfg_section, "debug-port"),
            debug=True)

def run_service(app, *, static_folder=_auto_set_static_folder):
    configure_static_folder(app, static_folder)
    parser = build_parser(app)
    configure_static_arguments(parser)
    args = parser.parse_args()
    configure_static_serving(app, args)
    run_app(app)