-
Notifications
You must be signed in to change notification settings - Fork 147
Expand file tree
/
Copy pathwebapp.py
More file actions
executable file
·92 lines (71 loc) · 3.29 KB
/
Copy pathwebapp.py
File metadata and controls
executable file
·92 lines (71 loc) · 3.29 KB
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
92
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#
# This file is part of INGInious. See the LICENSE and the COPYRIGHTS files for
# more information about the licensing of this file.
#
""" Starts the webapp """
import argparse
import logging
import os
import signal
import sys
from werkzeug.serving import run_simple
from werkzeug.middleware.proxy_fix import ProxyFix
from werkzeug.middleware.shared_data import SharedDataMiddleware
# If INGInious files are not installed in Python path
sys.path.append(os.path.dirname(__file__))
from inginious.common.log import init_logging
from inginious.common.base import load_json_or_yaml
import inginious.frontend.app
def main():
# Parse the paramaters from command line arguments
parser = argparse.ArgumentParser()
parser.add_argument("--config",
help="Path to configuration file. By default: configuration.yaml or configuration.json", default=os.environ.get("INGINIOUS_WEBAPP_CONFIG", ""))
parser.add_argument("--host", help="Host to bind to. Default is localhost.", default=os.environ.get("INGINIOUS_WEBAPP_HOST", "localhost"))
parser.add_argument("--port", help="Port to listen to. Default is 8080.", type=int, default=os.environ.get("INGINIOUS_WEBAPP_PORT", "8080"))
args = parser.parse_args()
host = args.host
port = args.port
configfile = args.config
if not configfile:
if os.path.isfile("./configuration.yaml"):
configfile = "./configuration.yaml"
elif os.path.isfile("./configuration.json"):
configfile = "./configuration.json"
else:
raise Exception("No configuration file found")
# Load configuration and application (!!! For mod_wsgi, application identifier must be present)
config = load_json_or_yaml(configfile)
# Init logging
init_logging(config.get('log_level', 'INFO'))
logging.getLogger("inginious.webapp").info("http://%s:%d/" % (host, int(port)))
application, close_app_func = inginious.frontend.app.get_app(config)
if 'SERVER_SOFTWARE' in os.environ: # cgi
os.environ['FCGI_FORCE_CGI'] = 'Y'
if 'PHP_FCGI_CHILDREN' in os.environ or 'SERVER_SOFTWARE' in os.environ: # lighttpd fastcgi
import flup.server.fcgi as flups
flups.WSGIServer(application, multiplexed=True, bindAddress=None, debug=False).run()
# Fix Reverse Proxy
reverse_proxy_config = config.get('reverse-proxy-config', {})
reverse_proxy_enable = reverse_proxy_config.get('enable', False)
x_for = reverse_proxy_config.get('x-for', 1)
x_host = reverse_proxy_config.get('x-host', 1)
if reverse_proxy_enable:
application = ProxyFix(application, x_for=x_for, x_host=x_host)
# Close the client when interrupting the app
def close_app_signal():
close_app_func()
raise KeyboardInterrupt()
signal.signal(signal.SIGINT, lambda _, _2: close_app_signal())
signal.signal(signal.SIGTERM, lambda _, _2: close_app_signal())
# Add static redirection and request log
root_path = inginious.get_root_path()
application = SharedDataMiddleware(application, [
('/static/', os.path.join(root_path, 'frontend', 'static'))
])
# Launch the app
run_simple(host, port, application, use_debugger=config.get("web_debug", False), threaded=True)
if __name__ == "__main__":
main()