Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.
 
 
 
 
 
 

107 řádky
2.7 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.api
  18. import webnotes.webutils
  19. from webnotes.utils import get_site_name
  20. local_manager = LocalManager([webnotes.local])
  21. _site = None
  22. def handle_session_stopped():
  23. res = Response("""<html>
  24. <body style="background-color: #EEE;">
  25. <h3 style="width: 900px; background-color: #FFF; border: 2px solid #AAA; padding: 20px; font-family: Arial; margin: 20px auto">
  26. Updating.
  27. We will be back in a few moments...
  28. </h3>
  29. </body>
  30. </html>""")
  31. res.status_code = 503
  32. res.content_type = 'text/html'
  33. return res
  34. @Request.application
  35. def application(request):
  36. webnotes.local.request = request
  37. try:
  38. site = _site or get_site_name(request.host)
  39. webnotes.init(site=site)
  40. if not webnotes.local.conf:
  41. # site does not exist
  42. raise NotFound
  43. webnotes.local.form_dict = webnotes._dict({ k:v[0] if isinstance(v, (list, tuple)) else v \
  44. for k, v in (request.form or request.args).iteritems() })
  45. webnotes.local._response = Response()
  46. webnotes.http_request = webnotes.auth.HTTPRequest()
  47. if webnotes.local.form_dict.cmd:
  48. webnotes.handler.handle()
  49. elif webnotes.request.path.startswith("/api/"):
  50. webnotes.api.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.AuthenticationError, e:
  58. webnotes._response.status_code=401
  59. except webnotes.SessionStopped, e:
  60. webnotes.local._response = handle_session_stopped()
  61. finally:
  62. if webnotes.conn:
  63. webnotes.conn.close()
  64. return webnotes.local._response
  65. application = local_manager.make_middleware(application)
  66. def serve(port=8000, profile=False, site=None):
  67. global application, _site
  68. _site = site
  69. from werkzeug.serving import run_simple
  70. if profile:
  71. application = ProfilerMiddleware(application)
  72. if not os.environ.get('NO_STATICS'):
  73. application = SharedDataMiddleware(application, {
  74. '/assets': 'assets',
  75. })
  76. if site:
  77. application = SharedDataMiddleware(application, {
  78. '/files': os.path.join(site, 'public', 'files')
  79. })
  80. run_simple('0.0.0.0', int(port), application, use_reloader=True,
  81. use_debugger=True, use_evalex=True)