1 | # The following code is not part of Rocket but was added to |
---|
2 | # web2py for testing purposes. |
---|
3 | |
---|
4 | |
---|
5 | def demo_app(environ, start_response): |
---|
6 | global static_folder |
---|
7 | import os |
---|
8 | types = {'htm': 'text/html', 'html': 'text/html', |
---|
9 | 'gif': 'image/gif', 'jpg': 'image/jpeg', |
---|
10 | 'png': 'image/png', 'pdf': 'applications/pdf'} |
---|
11 | if static_folder: |
---|
12 | if not static_folder.startswith('/'): |
---|
13 | static_folder = os.path.join(os.getcwd(), static_folder) |
---|
14 | path = os.path.join( |
---|
15 | static_folder, environ['PATH_INFO'][1:] or 'index.html') |
---|
16 | type = types.get(path.split('.')[-1], 'text') |
---|
17 | if os.path.exists(path): |
---|
18 | try: |
---|
19 | data = open(path, 'rb').read() |
---|
20 | start_response('200 OK', [('Content-Type', type)]) |
---|
21 | except IOError: |
---|
22 | start_response('404 NOT FOUND', []) |
---|
23 | data = '404 NOT FOUND' |
---|
24 | else: |
---|
25 | start_response('500 INTERNAL SERVER ERROR', []) |
---|
26 | data = '500 INTERNAL SERVER ERROR' |
---|
27 | else: |
---|
28 | start_response('200 OK', [('Content-Type', 'text/html')]) |
---|
29 | data = '<html><body><h1>Hello from Rocket</h1></body></html>' |
---|
30 | return [data] |
---|
31 | |
---|
32 | |
---|
33 | def demo(): |
---|
34 | from optparse import OptionParser |
---|
35 | parser = OptionParser() |
---|
36 | parser.add_option("-i", "--ip", dest="ip", default="127.0.0.1", |
---|
37 | help="ip address of the network interface") |
---|
38 | parser.add_option("-p", "--port", dest="port", default="8000", |
---|
39 | help="post where to run web server") |
---|
40 | parser.add_option("-s", "--static", dest="static", default=None, |
---|
41 | help="folder containing static files") |
---|
42 | (options, args) = parser.parse_args() |
---|
43 | global static_folder |
---|
44 | static_folder = options.static |
---|
45 | print('Rocket running on %s:%s' % (options.ip, options.port)) |
---|
46 | r = Rocket((options.ip, int(options.port)), 'wsgi', {'wsgi_app': demo_app}) |
---|
47 | r.start() |
---|
48 | |
---|
49 | |
---|
50 | if __name__ == '__main__': |
---|
51 | demo() |
---|