25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.
 
 
 
 
 
 

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