unbit / uwsgi

uWSGI application server container
http://projects.unbit.it/uwsgi
Other
3.46k stars 691 forks source link

config from plugins (eg. python:// for statically link python) #564

Open anthonyrisinger opened 10 years ago

anthonyrisinger commented 10 years ago

i think it would be REALLY useful to activate plugins as a config source, specifically:

--json python://package.module:callable()

...would use import package.module; return package.module.callable() as JSON config, whereas:

--ini python://package.module:object

...would use import package.module; return package.module.object as INI config (note: this mirrors the --module, --callable, and --mount syntax)

this would allow me to move our entire config to python (with access to uwsgi.opt hopefully!)... combined with a writable uwsgi.opt (that future config can see!) i'd be ecstatic!

unbit commented 10 years ago

There are lot of implications in this, as it would means initializing languages vm before the user has configured the environment and so on. There are really a lot of corner cases for it. What about this trick:

UWSGI_AS_LIB=libuwsgi.so make
#!/usr/bin/python

import sys
import ctypes

def application(e, sr):
    sr('200 OK',[('Content-Type','text/html')])
    return ["Hello World"]

def uwsgi_run(uwsgi_args):
    # load the uwsgi library in the global namespace
    uwsgi = ctypes.CDLL('./libuwsgi.so',mode=ctypes.RTLD_GLOBAL)

    uwsgi_args.insert(0, sys.argv[0])
    uwsgi_args.insert(1, '--binary-path')
    uwsgi_args.insert(2, sys.argv[0])

    # build command line args
    argv = (ctypes.c_char_p * (len(uwsgi_args)+1))()
    for pos,arg in enumerate(uwsgi_args):
        argv[pos] = arg
    # inform the uwsgi engine, the passed environ is not safe to overwrite
    envs = (ctypes.c_char_p * 1)()
    # enter into uWSGI !!!
    uwsgi.uwsgi_init(len(uwsgi_args), argv, envs)

if __name__ == "__main__":
    uwsgi_run(['--http-socket', ':9090', '--master', '--processes', '8', '--wsgi-file', sys.argv[0]])
./u.py

you need to use latest code from github as it ensure python is not re-initialized if a vm is already available

unbit commented 10 years ago

Oh, and do not forget "pluggable configurators": http://uwsgi-docs.readthedocs.org/en/latest/Changelog-1.9.3.html#pluggable-configuration-system-with-lua-support