Created
June 9, 2014 18:49
-
-
Save wuub/814018aa3cdc06fcadd2 to your computer and use it in GitHub Desktop.
gunicorn configurable app
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
import os | |
import multiprocessing | |
from itertools import chain | |
from gunicorn import util | |
from gunicorn.config import Config | |
from gunicorn.app.base import Application | |
class ConfigurableApplication(Application): | |
@staticmethod | |
def default_cfg(): | |
return { | |
"worker_class": "sync", | |
"workers": multiprocessing.cpu_count() * 2 + 1, | |
"secure_scheme_headers": { | |
'X-FORWARDED-PROTO': 'https', | |
}, | |
"user": 'nobody' if os.getuid() == 0 else os.getuid() | |
} | |
def __init__(self, app_uri, **config): | |
self.app_uri = app_uri | |
self._config = config | |
super(ConfigurableApplication, self).__init__() | |
def load(self): | |
app = util.import_app(self.app_uri) | |
print("init here?") | |
return app | |
def init(self, parser, opts, args): | |
self.cfg.set("default_proc_name", self.app_uri) | |
def load_config(self): | |
self.cfg = Config() | |
for k, v in chain(self.default_cfg().items(), self._config.items()): | |
self.cfg.set(k.lower(), v) | |
if __name__ == '__main__': | |
ConfigurableApplication( | |
"main:app", | |
bind="0.0.0.0:5321", | |
preload_app=True | |
).run() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment