607a4ea6e19ac706a2c0ea44e0223ffa12b7c18f
[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
17 Run server as FastCGI, with CherryPy config from "main.conf".
18 With `config`, run standalone with CherryPy config from `config`.
19
20 Serve the authenticated or unauthenticated site according to
21 the first argument.
22
23 Helper scripts "auth.fcgi" and "unauth.fcgi" are provided to
24 facilitate running the server with no arguments.
25 """ % (sys.argv[0], argv0_dir, argv0_dir)
26     sys.exit(2)
27
28 if __name__ == "__main__":
29     if '-h' in sys.argv or '--help' in sys.argv:
30         usage()
31     if not (2 <= len(sys.argv) <= 3):
32         usage()
33
34     mode = sys.argv[1]
35     if len(sys.argv) == 3:
36         conf_file = sys.argv[2]
37         dev = True
38     else:
39         conf_file = os.path.join(base_dir, 'main.conf')
40
41     app_config = {
42         '/': {
43             'tools.invirtwebstate.on': True,
44             },
45         }
46
47     if mode.startswith('auth'):
48         root = main.InvirtWeb()
49         app_config['/']['tools.mako.module_directory'] = "/tmp/invirt-auth-web-templatecache"
50     elif mode.startswith('unauth'):
51         root = main.InvirtUnauthWeb()
52         app_config['/']['tools.mako.module_directory'] = "/tmp/invirt-unauth-web-templatecache"
53     else:
54         usage()
55
56     app = cherrypy.tree.mount(root, '/', app_config)
57     app.merge(conf_file)
58     cherrypy.config.update(conf_file)
59
60     if dev:
61         cherrypy.server.quickstart()
62         cherrypy.engine.start()
63         cherrypy.engine.block()
64     else:
65         cherrypy.engine.start(blocking=False)
66         from flup.server.fcgi import WSGIServer
67         server = WSGIServer(cherrypy.tree)
68         server.run()