-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathhttpserver.py
More file actions
71 lines (64 loc) · 1.88 KB
/
httpserver.py
File metadata and controls
71 lines (64 loc) · 1.88 KB
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import http.server
import http.server
import server
import db
import logging
import re
import os
import custom_exceptions
PORT = 8080
PATH_REGEX = r'^\/v1\/pages\/([^\/]*)$'
#class MyHandler(BaseHTTPServer.BaseHTTPRequestHandler):
class MyHandler(http.server.SimpleHTTPRequestHandler):
def respond(self, statusCode, message):
self.send_response(statusCode)
self.end_headers()
self.wfile.write(message)
def do_GET(self):
matches = re.match(PATH_REGEX, self.path)
if matches:
path = matches.group(1)
elif self.path == '/web/' and self.server.apiGatewayClientUrl != None:
f = open("web/index.html")
html = f.read()
f.close()
html = html.replace('src="apiGateway-js-sdk/','src="'+self.server.apiGatewayClientUrl+'/apiGateway-js-sdk/')
self.respond(200,html)
return
else:
return http.server.SimpleHTTPRequestHandler.do_GET(self)
#return super(MyHandler,self).do_GET()
#self.respond(404, "Not Found")
#return
try:
page = self.server.serverIface.getPage('<httpserver>', path)
self.respond(200, page["html"])
except custom_exceptions.NotFound:
self.respond(404, "Not Found")
except:
self.respond(500, "Internal Error")
def do_POST(self):
self.respond(501, "Not implemented")
def do_PUT(self):
self.respond(501, "Not implemented")
if __name__ == '__main__':
logger = logging.getLogger('HTTPServer')
server_class = http.server.HTTPServer
httpd = server_class(('127.0.0.1', PORT), MyHandler)
if "PAGE_BUCKET" in os.environ:
pageBucket = os.environ["PAGE_BUCKET"]
mydb = db.DBS3(pageBucket)
else:
mydb = db.DBMemory()
if "APIG_URL" in os.environ:
httpd.apiGatewayClientUrl = os.environ["APIG_URL"]
else:
httpd.apiGatewayClientUrl = None
httpd.serverIface = server.Server(mydb)
httpd.serverIface.init()
try:
httpd.serve_forever()
logger.info('Started server on port ' + PORT)
except KeyboardInterrupt:
pass
httpd.server_close()