You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

105 rivejä
2.6 KiB

  1. # Copyright (c) 2013, Web Notes Technologies Pvt. Ltd. and Contributors
  2. # MIT License. See license.txt
  3. import sys, os
  4. import json
  5. sys.path.insert(0, '.')
  6. sys.path.insert(0, 'app')
  7. sys.path.insert(0, 'lib')
  8. from werkzeug.wrappers import Request, Response
  9. from werkzeug.local import LocalManager
  10. from werkzeug.wsgi import SharedDataMiddleware
  11. from werkzeug.exceptions import HTTPException, NotFound
  12. from werkzeug.contrib.profiler import ProfilerMiddleware
  13. import mimetypes
  14. import webnotes
  15. import webnotes.handler
  16. import webnotes.auth
  17. import webnotes.webutils
  18. from webnotes.utils import get_site_name
  19. local_manager = LocalManager([webnotes.local])
  20. _site = None
  21. def handle_session_stopped():
  22. res = Response("""<html>
  23. <body style="background-color: #EEE;">
  24. <h3 style="width: 900px; background-color: #FFF; border: 2px solid #AAA; padding: 20px; font-family: Arial; margin: 20px auto">
  25. Updating.
  26. We will be back in a few moments...
  27. </h3>
  28. </body>
  29. </html>""")
  30. res.status_code = 503
  31. res.content_type = 'text/html'
  32. return res
  33. @Request.application
  34. def application(request):
  35. webnotes.local.request = request
  36. try:
  37. site = _site or get_site_name(request.host)
  38. webnotes.init(site=site)
  39. if not webnotes.local.conf:
  40. # site does not exist
  41. raise NotFound
  42. webnotes.local.form_dict = webnotes._dict({ k:v[0] if isinstance(v, (list, tuple)) else v \
  43. for k, v in (request.form or request.args).iteritems() })
  44. webnotes.local._response = Response()
  45. try:
  46. webnotes.http_request = webnotes.auth.HTTPRequest()
  47. except webnotes.AuthenticationError, e:
  48. pass
  49. if webnotes.form_dict.cmd:
  50. webnotes.handler.handle()
  51. elif webnotes.local.request.method in ('GET', 'HEAD'):
  52. webnotes.webutils.render(webnotes.request.path[1:])
  53. else:
  54. raise NotFound
  55. except HTTPException, e:
  56. return e
  57. except webnotes.SessionStopped, e:
  58. webnotes.local._response = handle_session_stopped()
  59. finally:
  60. if webnotes.conn:
  61. webnotes.conn.close()
  62. return webnotes.local._response
  63. application = local_manager.make_middleware(application)
  64. def serve(port=8000, profile=False, site=None):
  65. global application, _site
  66. _site = site
  67. from werkzeug.serving import run_simple
  68. if profile:
  69. application = ProfilerMiddleware(application)
  70. if not os.environ.get('NO_STATICS'):
  71. application = SharedDataMiddleware(application, {
  72. '/assets': 'assets',
  73. })
  74. if site:
  75. application = SharedDataMiddleware(application, {
  76. '/files': os.path.join(site, 'public', 'files')
  77. })
  78. run_simple('0.0.0.0', int(port), application, use_reloader=True,
  79. use_debugger=True, use_evalex=True)