-
Notifications
You must be signed in to change notification settings - Fork 221
/
main.py
72 lines (60 loc) · 1.69 KB
/
main.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
# encoding: utf-8
"""
--------------------------------------
@describe asyncio http server 配置
@version: 1.0
@project: CloudControl
@file: main.py.py
@author: yuanlang
@time: 2019-03-15 14:00
---------------------------------------
"""
import asyncio
import jinja2
import aiohttp_jinja2
from aiohttp import web
from resources.routes_control import setup_routes
from middlewares import setup_middlewares
from service.impl.phone_service_impl import phone_service
from config import conf
from common.logger import logger
def setup_templates(app):
aiohttp_jinja2.setup(app, loader=jinja2.FileSystemLoader('./resources/templates'))
def setup_static_routes(app):
app.router.add_static('/static/',
path='./resources/static',
name='static')
def init_db(_loop):
"""
删除表atxserver.devices
:return:
"""
task = _loop.create_task(phone_service.delect_devices())
asyncio.gather(task)
async def init(_loop):
"""
初始化
:param _loop:
:return:
"""
app = web.Application(loop=_loop)
# 初始化数据库
init_db(_loop)
# 配置路由
setup_routes(_loop, app)
# 配置页面跳转中间件
setup_middlewares(app)
# 配置静态资源
setup_static_routes(app)
# 配置静态资源模板
setup_templates(app)
# 启动服务
# noinspection PyDeprecation
srv = await _loop.create_server(app.make_handler(), '0.0.0.0', conf.server["port"])
logger.info('http://0.0.0.0:'+str(conf.server["port"]))
return srv
loop = asyncio.get_event_loop()
loop.run_until_complete(init(loop))
loop.run_forever()
# app['config'] = config
# web.run_app(app, host="0.0.0.0", port=8000)