-
Notifications
You must be signed in to change notification settings - Fork 0
/
coachwhip.py
59 lines (48 loc) · 1.67 KB
/
coachwhip.py
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
import json
from http_.server import Server
from routing.router import Router
class Coachwhip():
def __init__(self):
# Config
configuration = open('config.json')
data = json.load(configuration)
configuration.close()
self.configuration = data
self.router = Router()
# self.logger = Logger()
"""
Once an app instance has been obtained, with app = Coachwhip(),
routes can be defined like so:
@app.get('/hello_world')
def helloWorldHandler():
print('Handling /hello_world')
This is achieved using a decorator function
"""
def get(self, url, **kwargs):
def decorator(fn):
self.registerRouteHandler('get', url, fn, kwargs)
return fn
return decorator
def post(self, url, **kwargs):
def decorator(fn):
self.registerRouteHandler('post', url, fn, kwargs)
return fn
return decorator
def delete(self, url, **kwargs):
def decorator(fn):
self.registerRouteHandler('delete', url, fn, kwargs)
return fn
return decorator
def put(self, url, **kwargs):
def decorator(fn):
self.registerRouteHandler('put', url, fn, kwargs)
return fn
return decorator
def registerRouteHandler(self, verb, url, fn, kwargs):
self.router.addRoute(verb, url, fn, kwargs)
def listen(self, port=None):
print(self.configuration)
port = port if port else self.configuration['port']
self.bootServer(port)
def bootServer(self, port):
Server(port, self.router).listen();