-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathwsgi.py
executable file
·76 lines (57 loc) · 2.21 KB
/
wsgi.py
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
#!/usr/bin/env python3
from __future__ import unicode_literals
import logging
import multiprocessing
import os
import sys
from argparse import ArgumentParser
from typing import Dict
import gunicorn.app.base
from flask import Flask
from gunicorn.six import iteritems
from shop_db2.api import app, set_app
import configuration as config # isort: skip
def number_of_workers() -> int:
return (multiprocessing.cpu_count() * 2) + 1
class StandaloneApplication(gunicorn.app.base.BaseApplication):
def __init__(self, _app: Flask, _options: Dict = None) -> None:
self.options = _options or {}
self.application = _app
super(StandaloneApplication, self).__init__()
def load_config(self) -> None:
_config = dict(
[(key, value) for key, value in iteritems(self.options) if key in self.cfg.settings and value is not None]
)
for key, value in iteritems(_config):
self.cfg.set(key.lower(), value)
def load(self) -> Flask:
return self.application
if __name__ == "__main__":
# Check whether the productive database exists.
if not os.path.isfile(config.ProductiveConfig.DATABASE_PATH):
sys.exit(
"No database found. Please read the documentation and use " "the setupdb.py script to initialize shop-db."
)
parser = ArgumentParser(description="Starting shop-db2 with a gunicorn server")
parser.add_argument(
"--loglevel",
help="select the log level",
default="WARNING",
choices=["DEBUG", "INFO", "WARNING", "ERROR", "CRITICAL"],
)
args = parser.parse_args()
# Overwrite the app configuration with the productive settings.
set_app(config.ProductiveConfig)
# Logging
app.logger.handlers = logging.getLogger("gunicorn.error").handlers
app.logger.setLevel(logging.getLevelName(args.loglevel))
# Set the gunicorn options.
options = {
"bind": "%s:%s" % (app.config["HOST"], app.config["PORT"]),
# BEGIN OF WARNING
# DO NOT CHANGE THIS VALUE, THE APPLICATION IS NOT DESIGNED FOR
# MULTITHREADING AND ERRORS MAY OCCUR IN THE DATABASE!!!
"workers": 1,
# END WARNING
}
StandaloneApplication(app, options).run()