__main__.py 1.92 KB
Newer Older
1
#!/usr/bin/env python3
2
import datetime
3
import logging
4
import os
5

Edvard Rejthar's avatar
Edvard Rejthar committed
6 7 8
#logging.basicConfig(level=logging.DEBUG, format="%(message)s")  # init before flask so that logging is shown in the Terminal

from flask import Flask
9 10
from xvfbwrapper import Xvfb

11 12
from .lib.config import Config
from .lib.controller.api import Api
13

14
# assure the logging dir
15 16 17
if not os.path.exists(Config.LOG_DIR):
    os.makedirs(Config.LOG_DIR)

Edvard Rejthar's avatar
Edvard Rejthar committed
18 19 20 21
logger = logging.getLogger("mdmaug")
logger.setLevel(logging.DEBUG)
logger.addHandler(logging.StreamHandler())

22
# server setup
23
Api.reset()
Edvard Rejthar's avatar
Edvard Rejthar committed
24 25 26 27
# address = '0.0.0.0'

app = Flask(__name__, static_url_path="/static")
app.secret_key = b'as8!r"afERaa5'
Edvard Rejthar's avatar
Edvard Rejthar committed
28 29 30 31 32 33

# app.config["preferences"] = {
#     "safebrowsing": True,
#     "pdns": True,
#     "geoip": False
# }
Edvard Rejthar's avatar
Edvard Rejthar committed
34

35 36 37 38 39 40 41 42

@app.template_filter('format_datetime')
def format_datetime(time, format='%y%m%d%H%M%S', target='%d.%m.%Y %H:%M'):
    return datetime.datetime.strptime(time, format).strftime(target)


#app.jinja_env.filters['datetime'] = format_datetime

Edvard Rejthar's avatar
Edvard Rejthar committed
43
"""
44
httpd = HTTPServer((address, Config.APP_PORT), Server)
45 46
httpd.socket = ssl.wrap_socket(httpd.socket,
                               server_side=True,
47 48
                               # together private + cert, http://stackoverflow.com/questions/19705785/python-3-https-webserver
                               certfile=Config.DIR + 'cert-mdmaug.pem',
49
                               ssl_version=ssl.PROTOCOL_TLSv1)
Edvard Rejthar's avatar
Edvard Rejthar committed
50
"""
51 52
display = Xvfb()
display.start()
Edvard Rejthar's avatar
Edvard Rejthar committed
53

54
try:
Edvard Rejthar's avatar
Edvard Rejthar committed
55 56 57 58
    # print(f'Listening at https://{address}:{Config.APP_PORT}')
    from .lib.controller.server import app as server

    app.register_blueprint(server)
Edvard Rejthar's avatar
Edvard Rejthar committed
59 60 61
    if __name__ == "__main__":
        app.run(host=Config.APP_IP, port=Config.APP_PORT, ssl_context=(Config.DIR + 'cert-mdmaug.pem', Config.DIR + 'key-mdmaug.pem'), threaded=True)
    # for _ in range(Config.profile_count):
Edvard Rejthar's avatar
Edvard Rejthar committed
62
    #    threading.Thread(target=httpd.serve_forever).start()
63
except (KeyboardInterrupt, SystemExit):
64
    display.stop()