forked from microsoft/aerial_wildlife_detection
-
Notifications
You must be signed in to change notification settings - Fork 0
/
application.py
77 lines (55 loc) · 2.2 KB
/
application.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
73
74
75
76
77
'''
Wrapper/entry point for WSGI servers like Gunicorn.
Can launch multiple modules at once,
but requires environment variables to be set to do so.
2019 Benjamin Kellenberger
'''
''' import resources and initialize app '''
import os
from bottle import Bottle
from util.configDef import Config
from modules import REGISTERED_MODULES
def _verify_unique(instances, moduleClass):
'''
Compares the newly requested module, address and port against
already launched modules on this instance.
Raises an Exception if another module from the same type has already been launched on this instance
'''
for i in instances:
if moduleClass.__class__.__name__ == i.__class__.__name__:
raise Exception('Module {} already launched on this server.'.format(moduleClass.__class__.__name__))
# load configuration
config = Config()
demoMode = config.getProperty('Project', 'demoMode', type=bool, fallback=False)
# prepare bottle
app = Bottle()
# parse requested instances
instance_args = os.environ['AIDE_MODULES'].split(',')
instances = []
# create user handler
userHandler = REGISTERED_MODULES['UserHandler'](config, app)
for i in instance_args:
moduleName = i.strip()
if moduleName == 'UserHandler':
continue
if demoMode and moduleName == 'AIController':
print('WARNING: AIController module not allowed in demo mode. Skipping...')
continue
moduleClass = REGISTERED_MODULES[moduleName]
# verify
_verify_unique(instances, moduleClass)
# create instance
instance = moduleClass(config, app)
instances.append(instance)
# add authentication functionality
if hasattr(instance, 'addLoginCheckFun'):
instance.addLoginCheckFun(userHandler.checkAuthenticated)
if moduleName == 'LabelUI':
# also launch configurator
configurator = REGISTERED_MODULES['ProjectConfigurator'](config, app)
configurator.addLoginCheckFun(userHandler.checkAuthenticated)
if __name__ == '__main__':
# run using server selected by Bottle
host = config.getProperty('Server', 'host')
port = config.getProperty('Server', 'port')
app.run(host=host, port=port)