Rift.IO OSM R1 Initial Submission
[osm/UI.git] / skyquake / plugins / composer / scripts / server_composer_ui.py
1 #!/usr/bin/env python3
2
3 #
4 # Copyright 2016 RIFT.IO Inc
5 #
6 # Licensed under the Apache License, Version 2.0 (the "License");
7 # you may not use this file except in compliance with the License.
8 # You may obtain a copy of the License at
9 #
10 # http://www.apache.org/licenses/LICENSE-2.0
11 #
12 # Unless required by applicable law or agreed to in writing, software
13 # distributed under the License is distributed on an "AS IS" BASIS,
14 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 # See the License for the specific language governing permissions and
16 # limitations under the License.
17 #
18
19 from http.server import BaseHTTPRequestHandler, HTTPServer, SimpleHTTPRequestHandler
20 import socketserver
21 import mimetypes
22 import argparse
23 import sys
24 import os
25 import ssl
26
27 PORT = 9000
28
29 enable_https = False
30 keyfile_path = None
31 certfile_path = None
32
33 DEFAULT_ENABLE_HTTPS = False
34 DEFAULT_KEYFILE_PATH = None
35 DEFAULT_CERTFILE_PATH = None
36
37 def start_server(
38 enable_https=DEFAULT_ENABLE_HTTPS,
39 keyfile_path=DEFAULT_KEYFILE_PATH,
40 certfile_path=DEFAULT_CERTFILE_PATH):
41 Handler = SimpleHTTPRequestHandler
42 Handler.extensions_map['.svg'] = 'image/svg+xml'
43 httpd = socketserver.TCPServer(('', PORT), Handler)
44
45 if enable_https:
46
47 httpd.socket = ssl.wrap_socket(httpd.socket,
48 server_side=True,
49 certfile=certfile_path,
50 keyfile=keyfile_path)
51
52 print("Serving at port: {}. HTTPS Enabled: {}".format(PORT, enable_https))
53 httpd.serve_forever()
54
55
56 def main(argv=sys.argv[1:]):
57 parser = argparse.ArgumentParser()
58 parser.add_argument("-p", "--port",
59 default=PORT,
60 help="Run on the given port")
61 parser.add_argument("-e", "--enable-https",
62 action="store_true",
63 default=False,
64 help="Enable HTTPS. Make sure certfile-path and keyfile-path are also specified")
65 parser.add_argument("-k", "--keyfile-path",
66 default=DEFAULT_KEYFILE_PATH,
67 help="Path to the key file")
68 parser.add_argument("-c", "--certfile-path",
69 default=DEFAULT_CERTFILE_PATH,
70 help="Path to the cert file")
71
72 args = parser.parse_args()
73
74 # When you want to use the debugger, unremark this before the line you want
75 #import pdb; pdb.set_trace()
76
77 if args.enable_https:
78 if not (args.keyfile_path and args.certfile_path):
79 parser.print_help()
80 sys.exit(2)
81
82 start_server(args.enable_https, args.keyfile_path, args.certfile_path)
83
84
85 if __name__ == '__main__':
86 main()
87