pasterapp.py 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. # -*- coding: utf-8 -
  2. #
  3. # This file is part of gunicorn released under the MIT license.
  4. # See the NOTICE for more information.
  5. import configparser
  6. import os
  7. from paste.deploy import loadapp
  8. from gunicorn.app.wsgiapp import WSGIApplication
  9. from gunicorn.config import get_default_config_file
  10. def get_wsgi_app(config_uri, name=None, defaults=None):
  11. if ':' not in config_uri:
  12. config_uri = "config:%s" % config_uri
  13. return loadapp(
  14. config_uri,
  15. name=name,
  16. relative_to=os.getcwd(),
  17. global_conf=defaults,
  18. )
  19. def has_logging_config(config_file):
  20. parser = configparser.ConfigParser()
  21. parser.read([config_file])
  22. return parser.has_section('loggers')
  23. def serve(app, global_conf, **local_conf):
  24. """\
  25. A Paste Deployment server runner.
  26. Example configuration:
  27. [server:main]
  28. use = egg:gunicorn#main
  29. host = 127.0.0.1
  30. port = 5000
  31. """
  32. config_file = global_conf['__file__']
  33. gunicorn_config_file = local_conf.pop('config', None)
  34. host = local_conf.pop('host', '')
  35. port = local_conf.pop('port', '')
  36. if host and port:
  37. local_conf['bind'] = '%s:%s' % (host, port)
  38. elif host:
  39. local_conf['bind'] = host.split(',')
  40. class PasterServerApplication(WSGIApplication):
  41. def load_config(self):
  42. self.cfg.set("default_proc_name", config_file)
  43. if has_logging_config(config_file):
  44. self.cfg.set("logconfig", config_file)
  45. if gunicorn_config_file:
  46. self.load_config_from_file(gunicorn_config_file)
  47. else:
  48. default_gunicorn_config_file = get_default_config_file()
  49. if default_gunicorn_config_file is not None:
  50. self.load_config_from_file(default_gunicorn_config_file)
  51. for k, v in local_conf.items():
  52. if v is not None:
  53. self.cfg.set(k.lower(), v)
  54. def load(self):
  55. return app
  56. PasterServerApplication().run()