/
server.py
63 lines (53 loc) · 2.24 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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
from aiohttp import web
from guillotina.commands import Command
import cProfile
try:
import aiomonitor
HAS_AIOMONITOR = True
except ImportError:
HAS_AIOMONITOR = False
try:
import aiohttp_autoreload
HAS_AUTORELOAD = True
except ImportError:
HAS_AUTORELOAD = False
class ServerCommand(Command):
description = 'Guillotina server runner'
def get_parser(self):
parser = super(ServerCommand, self).get_parser()
parser.add_argument('-m', '--monitor', action='store_true',
dest='monitor', help='Monitor', default=False)
parser.add_argument('-r', '--reload', action='store_true',
dest='reload', help='Auto reload on code changes',
default=False)
parser.add_argument('-p', '--profile', action='store_true',
dest='profile', help='Profile execution',
default=False)
return parser
def _run(self, arguments, settings, app):
port = settings.get('address', settings.get('port'))
if arguments.profile:
cProfile.runctx("web.run_app(app, host=settings.get('host', '0.0.0.0'), port=port)", {
'web': web
}, {
'port': port,
'settings': settings,
'app': app
})
else:
web.run_app(app, host=settings.get('host', '0.0.0.0'), port=port)
def run(self, arguments, settings, app):
if arguments.monitor:
if not HAS_AIOMONITOR:
return print('You must install aiomonitor for the --monitor option to work'
'Use `pip install aiomonitor` to install aiomonitor.')
# init monitor just before run_app
loop = self.get_loop()
with aiomonitor.start_monitor(loop=loop):
self._run(arguments, settings, app)
if arguments.reload:
if not HAS_AUTORELOAD:
return print('You must install aiohttp_autoreload for the --reload option to work'
'Use `pip install aiohttp_autoreload` to install aiohttp_autoreload.')
aiohttp_autoreload.start()
self._run(arguments, settings, app)