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ů.
 
 
 
 
 
 

104 řádky
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. webnotes.http_request = webnotes.auth.HTTPRequest()
  46. if webnotes.form_dict.cmd:
  47. webnotes.handler.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)