This repository has been archived by the owner on Mar 11, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
run_service.py
87 lines (65 loc) · 2.46 KB
/
run_service.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
import functools
import os
import aiomysql
import aioredis
import motor.motor_tornado
import tornado.httpserver
import tornado.ioloop
import tornado.platform.asyncio
import tornado.web
from service.handlers.bart_handler import BartHandler
from service.handlers.flanders_handler import FlandersHandler
from service.handlers.healthcheck_handler import HealthcheckHandler
from service.handlers.homers_handler import HomersHandler
from service.handlers.lisas_handler import LisasHandler
from service.handlers.maggie_handler import MaggieHandler
from service.handlers.marges_handler import MargesHandler
from service.s3_object import S3Object
WEATHER_URI = "https://query.yahooapis.com/v1/public/yql?q=select%20item.condition" \
"%20from%20weather.forecast%20where%20woeid%20%3D%202357536&format=json"
def make_app(config):
return tornado.web.Application([
("/api/v1/bart", BartHandler),
("/api/v1/flanders", FlandersHandler),
("/api/v1/healthcheck", HealthcheckHandler),
("/api/v1/homers/(.*)", HomersHandler),
("/api/v1/lisas/(.*)", LisasHandler),
("/api/v1/maggie", MaggieHandler),
("/api/v1/marges/(.*)", MargesHandler)
], **config)
async def connect_redis(environ):
return await aioredis.create_redis(
(
environ["REDIS_HOST"], environ["REDIS_PORT"]
))
async def connect_mysql(environ):
return await aiomysql.connect(
host=environ["MYSQL_HOST"],
port=int(environ["MYSQL_PORT"]),
db=environ["MYSQL_DATABASE"],
user=environ["MYSQL_USER"],
password=environ["MYSQL_PASSWORD"])
def main(environ):
tornado.platform.asyncio.AsyncIOMainLoop().install()
ioloop = tornado.ioloop.IOLoop.current()
motor_client = motor.motor_tornado.MotorClient(
environ["MONGODB_URI"])
mongo_db = motor_client.get_default_database()
redis = ioloop.run_sync(functools.partial(
connect_redis, environ))
mysql = ioloop.run_sync(functools.partial(
connect_mysql, environ))
s3_object = S3Object(environ["AWS_S3_BUCKET"], environ["AWS_S3_OBJECT"])
app = make_app({
"motor_client": motor_client,
"mongo_db": mongo_db,
"redis": redis,
"mysql": mysql,
"s3_object": s3_object,
"weather_uri": WEATHER_URI
})
server = tornado.httpserver.HTTPServer(app)
server.listen(int(environ["SERVER_PORT"]), "localhost")
ioloop.start()
if __name__ == "__main__":
main(os.environ)