Merge auth.fcgi and unauth.fcgi fixes from the old branch
[invirt/packages/invirt-web.git] / code / invirt.fcgi
1 #!/usr/bin/python
2 """Main FastCGI entry point for web interface"""
3
4 import cherrypy
5 import os
6 import sys
7
8 import main
9
10 dev = False
11 base_dir = os.path.dirname(__file__)
12
13 def usage():
14     argv0_dir = os.path.dirname(sys.argv[0])
15     print >>sys.stderr, """%s <unauth|auth> [config]
16 %s/auth.fcgi [config]
17 %s/unauth.fcgi [config]
18
19 Run server as FastCGI, with CherryPy config from "main.conf".
20
21 With `config`, run standalone with CherryPy config from `config`.
22
23 Run this script as either 'auth.fcgi' or 'unauth.fcgi', to get
24 the authenticated or unauthenticated site respectively, or pass
25 'auth' or 'unauth' as the first parameter.
26 """ % (sys.argv[0], argv0_dir, argv0_dir)
27     sys.exit(2)
28
29 if __name__ == "__main__":
30     if len(sys.argv) > 3 or len(sys.argv) == 1 or '-h' in sys.argv or '--help' in sys.argv:
31         usage()
32     if len(sys.argv) == 3:
33         mode = sys.argv[1]
34         conf_file = sys.argv[2]
35         dev = True
36     else:
37         mode = sys.argv[1]
38         conf_file = os.path.join(base_dir, 'main.conf')
39
40     app_config = {
41         '/': {
42             'tools.invirtwebstate.on': True,
43             },
44         }
45
46     if mode.startswith('auth'):
47         root = main.InvirtWeb()
48         app_config['/']['tools.mako.module_directory'] = "/tmp/invirt-auth-web-templatecache"
49     elif mode.startswith('unauth'):
50         root = main.InvirtUnauthWeb()
51         app_config['/']['tools.mako.module_directory'] = "/tmp/invirt-unauth-web-templatecache"
52     else:
53         usage()
54
55     app = cherrypy.tree.mount(root, '/', app_config)
56     app.merge(conf_file)
57     cherrypy.config.update(conf_file)
58
59     if dev:
60         cherrypy.server.quickstart()
61         cherrypy.engine.start()
62         cherrypy.engine.block()
63     else:
64         cherrypy.engine.start(blocking=False)
65         from flup.server.fcgi import WSGIServer
66         server = WSGIServer(cherrypy.tree)
67         server.run()