summary refs log tree commit diff
path: root/demo/webserver.py
blob: 78f3213540ab6e2fb23f948872f91daa5dbc9573 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import argparse
import BaseHTTPServer
import os
import SimpleHTTPServer

from daemonize import Daemonize


def setup():
    parser = argparse.ArgumentParser()
    parser.add_argument("directory")
    parser.add_argument("-p", "--port", dest="port", type=int, default=8080)
    parser.add_argument('-P', "--pid-file", dest="pid", default="web.pid")
    args = parser.parse_args()

    # Get absolute path to directory to serve, as daemonize changes to '/'
    os.chdir(args.directory)
    dr = os.getcwd()

    httpd = BaseHTTPServer.HTTPServer(
        ('', args.port),
        SimpleHTTPServer.SimpleHTTPRequestHandler
    )

    def run():
        os.chdir(dr)
        httpd.serve_forever()

    daemon = Daemonize(
            app="synapse-webclient",
            pid=args.pid,
            action=run,
            auto_close_fds=False,
        )

    daemon.start()

if __name__ == '__main__':
    setup()