From 9cc4db1a12dacdfc868e1d3d4bc89faadf965965 Mon Sep 17 00:00:00 2001 From: "roberto@natty32" Date: Mon, 4 Apr 2011 09:46:17 +0200 Subject: [PATCH] various fixes --- contrib/runuwsgi.py | 66 +++++++++++++++++++++++++++++++++++++++++++++ plugins.c | 21 +++++++++++++++ plugins/http/http.c | 2 ++ 3 files changed, 89 insertions(+) create mode 100644 contrib/runuwsgi.py diff --git a/contrib/runuwsgi.py b/contrib/runuwsgi.py new file mode 100644 index 00000000..88429605 --- /dev/null +++ b/contrib/runuwsgi.py @@ -0,0 +1,66 @@ +import django +from django.core.management.base import BaseCommand +from django.conf import settings +import os +import sys + +class Command(BaseCommand): + help = "Runs this project as a uWSGI application. Requires the uwsgi binary in system path." + + http_port = '8000' + socket_addr = None + + def handle(self, *args, **options): + for arg in args: + k,v = arg.split('=') + if k == 'http': + if self.http_port: + self.http_port = v + elif k == 'socket': + self.http_port = None + self.socket_addr = v + + # load http and python plugin: first the specific version, otherwise try with the generic one + if self.http_port: + os.environ['UWSGI_PLUGINS'] = 'http,python%d%d:python' % (sys.version_info[0], sys.version_info[1]) + else: + os.environ['UWSGI_PLUGINS'] = 'python%d%d:python' % (sys.version_info[0], sys.version_info[1]) + + # load the Django WSGI handler + os.environ['UWSGI_MODULE'] = 'django.core.handlers.wsgi:WSGIHandler()' + # DJANGO settings + if options['settings']: + os.environ['DJANGO_SETTINGS_MODULE'] = options['settings'] + else: + os.environ['DJANGO_SETTINGS_MODULE'] = 'settings' + + # bind the http server to the default port + if self.http_port: + os.environ['UWSGI_HTTP'] = ':%s' % self.http_port + elif self.socket_addr: + os.environ['UWSGI_SOCKET'] = self.socket_addr + + # map admin static files + os.environ['UWSGI_STATIC_MAP'] = '%s=%s' % (settings.ADMIN_MEDIA_PREFIX, os.path.join(django.__path__[0], 'contrib', 'admin', 'media')) + # remove sockets/pidfile at exit + os.environ['UWSGI_VACUUM'] = '1' + # retrieve/set the PythonHome + os.environ['UWSGI_PYHOME'] = sys.prefix + # increase buffer size a bit + os.environ['UWSGI_BUFFER_SIZE'] = '8192' + # add threads for concurrency + os.environ['UWSGI_THREADS'] = '8' + # enable the master process + os.environ['UWSGI_MASTER'] = '1' + # use uWSGI python module aliasing to fix the PYTHONPATH + os.environ['UWSGI_PYMODULE_ALIAS'] = '%s=./' % os.path.basename(os.getcwd()) + # exec the uwsgi binary + os.execvp('uwsgi', ('uwsgi',)) + + def usage(self, subcomand): + return r""" +run this project on the uWSGI server + + http=PORT run the embedded http server on port PORT + socket=ADDR bind the uwsgi server on address ADDR (this will disable the http server) + """ diff --git a/plugins.c b/plugins.c index cd187229..63c4f7cf 100644 --- a/plugins.c +++ b/plugins.c @@ -11,16 +11,26 @@ int uwsgi_load_plugin(int modifier, char *plugin, char *pargs, int absolute) { struct uwsgi_plugin *up; int i; + char *colon = strchr(plugin, ':'); + if (colon) { + colon[0] = 0; + } + +check: for (i = 0; i < 0xFF; i++) { if (uwsgi.p[i]->name) { if (!strcmp(plugin, uwsgi.p[i]->name)) { +#ifdef UWSGI_DEBUG uwsgi_log("%s plugin already available\n", plugin); +#endif return 0; } } if (uwsgi.p[i]->alias) { if (!strcmp(plugin, uwsgi.p[i]->alias)) { +#ifdef UWSGI_DEBUG uwsgi_log("%s plugin already available\n", plugin); +#endif return 0; } } @@ -30,18 +40,29 @@ int uwsgi_load_plugin(int modifier, char *plugin, char *pargs, int absolute) { if (uwsgi.gp[i]->name) { if (!strcmp(plugin, uwsgi.gp[i]->name)) { +#ifdef UWSGI_DEBUG uwsgi_log("%s plugin already available\n", plugin); +#endif return 0; } } if (uwsgi.gp[i]->alias) { if (!strcmp(plugin, uwsgi.gp[i]->alias)) { +#ifdef UWSGI_DEBUG uwsgi_log("%s plugin already available\n", plugin); +#endif return 0; } } } + if (colon) { + plugin = colon+1; + colon[0] = ':'; + colon = NULL; + goto check; + } + if (absolute) { plugin_name = malloc(strlen(plugin) + 1); memcpy(plugin_name, plugin, strlen(plugin) + 1); diff --git a/plugins/http/http.c b/plugins/http/http.c index a41530af..665ead6d 100644 --- a/plugins/http/http.c +++ b/plugins/http/http.c @@ -875,6 +875,8 @@ int http_init() { uwsgi_log("unable to register the http gateway\n"); exit(1); } + + uwsgi_log("HTTP router/proxy bound on %s\n", uhttp.socket_name); } return 0;