Bartosz Stebel | f5b1a21 | 2023-02-04 23:47:44 +0100 | [diff] [blame] | 1 | import os |
| 2 | import gunicorn.app.base |
| 3 | from django.core.wsgi import get_wsgi_application |
| 4 | |
| 5 | |
| 6 | class StandaloneApplication(gunicorn.app.base.BaseApplication): |
| 7 | |
| 8 | def __init__(self, app, options=None): |
| 9 | self.options = options or {} |
| 10 | self.application = app |
| 11 | super().__init__() |
| 12 | |
| 13 | def load_config(self): |
| 14 | config = {key: value for key, value in self.options.items() |
| 15 | if key in self.cfg.settings and value is not None} |
| 16 | for key, value in config.items(): |
| 17 | self.cfg.set(key.lower(), value) |
| 18 | |
| 19 | def load(self): |
| 20 | return self.application |
| 21 | |
| 22 | |
| 23 | def main(): |
| 24 | options = { |
| 25 | 'bind': os.environ.get('BIND_ADDR', '127.0.0.1:8080'), |
| 26 | 'workers': int(os.environ.get("GUNICORN_WORKERS", "4")), |
| 27 | 'capture_output': True, |
| 28 | 'disable_redirect_access_to_syslog': True, |
| 29 | 'accesslog': '-', |
| 30 | 'errorlog': '-', |
| 31 | } |
| 32 | os.environ['DJANGO_SETTINGS_MODULE'] = "settings" |
| 33 | application = get_wsgi_application() |
| 34 | StandaloneApplication(application, options).run() |
| 35 | |
| 36 | |
| 37 | if __name__ == '__main__': |
| 38 | main() |