e48729a5dfc37b2289863e6672599e06a9ed7cbf
[invirt/packages/invirt-web.git] / code / auth.fcgi
1 #!/usr/bin/python
2 """Main FastCGI entry point for authenticated web interface"""
3
4 import cherrypy
5 import os
6 import sys
7 from main import InvirtWeb
8
9 dev = False
10 base_dir = os.path.dirname(__file__)
11
12 def usage():
13     print >>sys.stderr, """%s [config]
14
15 Run server as FastCGI, with CherryPy config from "main.conf".
16
17 With `config`, run standalone with CherryPy config from `config`.
18 """ % sys.argv[0]
19     sys.exit(2)
20
21 if __name__ == "__main__":
22     static_dir = os.path.join(base_dir, 'static')
23
24     if len(sys.argv) > 2:
25         usage()
26     if len(sys.argv) > 1:
27         if sys.argv[1] in ('-h', '--help'):
28             usage()
29         conf_file = sys.argv[1]
30         dev = True
31     else:
32         conf_file = os.path.join(base_dir, 'main.conf')
33
34     app_config = {
35         '/': {
36             'tools.invirtwebstate.on': True,
37             },
38         '/static': {
39             'tools.staticdir.root': static_dir,
40             'tools.staticdir.on': True,
41             'tools.staticdir.dir': static_dir,
42             }
43         }
44                     
45     app = cherrypy.tree.mount(InvirtWeb(),
46                               '/',
47                               app_config)
48     app.merge(conf_file)
49     cherrypy.config.update(conf_file)
50
51     if dev:
52         cherrypy.server.quickstart()
53         cherrypy.engine.start()
54         cherrypy.engine.block()
55     else:
56         cherrypy.engine.start(blocking=False)
57         from flup.server.fcgi import WSGIServer
58         server = WSGIServer(cherrypy.tree)
59         server.run()