anonymous group photoblog software
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.
 
 
 
 
 
 

99 lines
2.7 KiB

  1. #!/usr/bin/python2
  2. import os
  3. import sys
  4. import traceback
  5. import fcgi
  6. import werkzeug
  7. import config, config_defaults
  8. import app
  9. import cli
  10. import util
  11. import model
  12. import interboard
  13. from board import Board, NoBoard
  14. from util import WakaError, local
  15. @util.headers
  16. def application(environ, start_response):
  17. '''Main routing application'''
  18. local.environ = environ
  19. request = werkzeug.BaseRequest(environ)
  20. task = request.values.get('task', request.values.get('action', ''))
  21. boardname = request.values.get('board', '')
  22. environ['waka.task'] = task
  23. environ['waka.boardname'] = boardname
  24. # Indicate "pop-up window" UI style.
  25. environ['waka.fromwindow'] = False
  26. environ['waka.rootpath'] = os.path.join('/', config.BOARD_DIR, '')
  27. if not task and not boardname:
  28. environ['waka.board'] = NoBoard()
  29. return app.check_setup(environ, start_response)
  30. environ['waka.board'] = NoBoard()
  31. try:
  32. if boardname:
  33. environ['waka.board'] = Board(boardname)
  34. elif task not in ('entersetup', 'setup', 'loginpanel'):
  35. raise WakaError("No board parameter set")
  36. elif task == 'loginpanel':
  37. raise WakaError("No board parameter set. "
  38. "If you haven't created boards yet, do it now.")
  39. except WakaError, e:
  40. return app.error(environ, start_response, e)
  41. # the task function if it exists, otherwise no_task()
  42. function = getattr(app, 'task_%s' % task.lower(), app.no_task)
  43. try:
  44. interboard.remove_old_bans()
  45. interboard.remove_old_backups()
  46. except model.OperationalError, e:
  47. return ["Error initializing database: %s" % e.args[0]]
  48. try:
  49. # wrap with list() to run inside this try..except
  50. return list(function(environ, start_response))
  51. except WakaError, e:
  52. return app.error(environ, start_response, e)
  53. except:
  54. environ['waka.status'] = '503 Service unavailable'
  55. traceback.print_exc()
  56. return app.error(environ, start_response)
  57. def cleanup(*args, **kwargs):
  58. '''Destroy the thread-local session and environ'''
  59. session = model.Session()
  60. session.commit()
  61. session.transaction = None # fix for a circular reference
  62. model.Session.remove()
  63. local.environ = {}
  64. application = util.cleanup(application, cleanup)
  65. def main():
  66. try:
  67. app.init_database()
  68. except model.OperationalError, e:
  69. # CGI-friendly error message
  70. print "Content-Type: text/plain\n"
  71. print "Error initializing database: %s" % e.args[0]
  72. return
  73. if not sys.argv[1:] or sys.argv[1] == 'fcgi':
  74. fcgi.WSGIServer(application).run()
  75. else:
  76. cli.handle_command(sys.argv[1:], application)
  77. cleanup()
  78. if __name__ == '__main__':
  79. main()