-
Notifications
You must be signed in to change notification settings - Fork 57
/
Copy pathserver.py
46 lines (34 loc) · 1.7 KB
/
server.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
#!/usr/bin/env python
# coding: utf-8
# A restful HTTP API for ansible
# Base on ansible-runner and sanic
# Github <https://github.com/lfbear/ansible-api>
# Author: lfbear
import json
from sanic import Sanic
from sanic.response import text
from .tool import Tool
from .config import Config
from . import controller
class Server(object):
def __init__(self, daemon):
app = Sanic('ansible-api')
app.add_route(controller.Main.as_view(), '/')
app.add_route(controller.NonBlockTest.as_view(), '/test')
app.add_route(controller.Command.as_view(), '/command')
app.add_route(controller.Playbook.as_view(), '/playbook')
app.add_route(controller.FileList.as_view(), '/filelist')
app.add_route(controller.FileReadWrite.as_view(), '/fileitem')
app.add_route(controller.FileExist.as_view(), '/filexist')
app.add_route(controller.ParseVarsFromFile.as_view(), '/parsevars')
app.add_websocket_route(controller.Message.websocket, '/message', subprotocols=Config.get('ws_sub'))
app.config.update(dict(RESPONSE_TIMEOUT=Config.get('timeout'))) # timeout for waiting response
@app.middleware('request')
async def ip_ban(request):
if len(Config.get('allow_ip')) and request.ip not in Config.get('allow_ip'):
return text('Your IP (%s) is not allowed!' % request.ip, status=403)
# print config contents
config = Config().__dict__
config['sign_key'] = len(config['sign_key']) * '*' # mask signature key
Tool.LOGGER.debug("Config at start: %s" % json.dumps(config))
app.run(host=Config.get('host'), port=Config.get('port'), workers=Config.get('workers'), debug=not daemon)