-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.py
More file actions
67 lines (52 loc) · 1.8 KB
/
server.py
File metadata and controls
67 lines (52 loc) · 1.8 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
from bottle import Bottle, request, response
from telegram_controllers import TelegramController
from event_controllers import EventController
from broadcast_controllers import BroadcastController
import telegram_util
import logging
logging.basicConfig(filename='log/access-error.log',level=logging.INFO)
LOGGER = logging.getLogger(__name__)
SERVICES = {
'telegram': TelegramController,
'event': EventController,
'broadcast': BroadcastController
}
# call it once here to load it up
telegram_util.get_instance()
app = Bottle()
@app.route('/', method=['GET'],)
def health():
return 'healthy'
@app.route('/<service>', method=['GET', 'POST'])
def service_handler(service):
# print('Called {method} {url} with data = {form}, query = {query}, params = {params}'.format({
# 'method': request.method,
# 'url': request.url,
# 'form': request.forms.dict,
# 'query': request.query.dict,
# 'params': request.params
# }))
try:
response.status = 200
request_params = {}
for field_type in ['query', 'forms']:
field = getattr(request, field_type)
if field:
field_value = {k: v[0] for k, v in field.dict.items()}
print('%s: %s' % (field_type, field_value))
request_params.update(field_value)
if request.json:
print('json: %s' % request.json)
request_params.update(request.json)
print(request_params)
service = SERVICES[service]
func = getattr(service(), request.method.lower())
res = func(request_params, response)
return res
except Exception as err:
LOGGER.error(err, exc_info=1)
raise
def main(port=8080):
app.run(server='tornado', host='localhost', port=port)
if __name__ == '__main__':
main()