forked from techbanca/coinai
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun_web.py
51 lines (40 loc) · 1.39 KB
/
run_web.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
# -*- coding: utf-8 -*-
import asyncio
import json, sys
import os
import uvloop
from aiohttp import web
from lib import orm
from lib.config import configs
from lib.baseweb import add_routes, add_static
from lib.factory import logger_factory, response_factory
from lib.redis import Redis
from logger.client import info
from settings import module_list, host, root_path
async def init_db(loop):
await orm.create_pool(loop=loop, **configs.db)
redis_obj = Redis.getInstance()
data_path = os.path.join(root_path, "data/dbData/data.json")
with open(data_path,"r") as load_f:
load_data = json.load(load_f)
hotCoins = load_data.get("hotCoins")
update_report = load_data.get("update_report")
redis_obj.set("hotCoins", json.dumps(hotCoins))
redis_obj.set("update_report", json.dumps(update_report))
async def init(loop, init_port=6000):
await init_db(loop)
app = web.Application(loop=loop, middlewares=[
logger_factory, response_factory
])
add_routes(app, module_list=module_list)
add_static(app)
srv = await loop.create_server(app.make_handler(), host, init_port)
info('server started at http://%s:%s...'%(host, init_port))
return srv
if __name__ == '__main__':
params = sys.argv
init_port = int(params[1]) if len(params) > 1 else 5000
asyncio.set_event_loop_policy(uvloop.EventLoopPolicy())
loop = asyncio.get_event_loop()
loop.run_until_complete(init(loop, init_port))
loop.run_forever()