-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi.py
56 lines (46 loc) · 1.56 KB
/
api.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
import json
from aiohttp import web
from utils.gTime import getTime
from utils.myLog import _log
from utils.apiHandler import afd_request
# 初始化节点
routes = web.RouteTableDef()
# 基础返回
@routes.get('/')
async def hello_world(request): # put application's code here
_log.info(f"request | root-url")
return web.Response(body=json.dumps(
{
'code': 0,
'message': f'Hello! Get recv at {getTime()}'
},
indent=2,
sort_keys=True,
ensure_ascii=False),
status=200,
content_type='application/json')
# 爱发电的wh
@routes.post('/afd')
async def aifadian_webhook(request):
_log.info(f"request | /afd")
try:
ret = await afd_request(request)
return web.Response(body=json.dumps(ret, indent=2, sort_keys=True, ensure_ascii=False),
content_type='application/json')
except:
_log.exception("Exception in /afd")
return web.Response(body=json.dumps({
"ec": 0,
"em": "err ouccer"
}, indent=2, sort_keys=True, ensure_ascii=False),
status=503,
content_type='application/json')
app = web.Application()
app.add_routes(routes)
if __name__ == '__main__':
try: # host需要设置成0.0.0.0,否则只有本地才能访问
HOST,PORT = '0.0.0.0',14726
_log.info(f"API Service Start at {HOST}:{PORT}")
web.run_app(app, host=HOST, port=PORT)
except:
_log.exception("Exception occur")