-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathcontrollers.py
More file actions
102 lines (79 loc) · 2.93 KB
/
Copy pathcontrollers.py
File metadata and controls
102 lines (79 loc) · 2.93 KB
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
import importlib
import os
from tornado.escape import url_unescape
from tornado.web import HTTPError
from tornado_prometheus import MetricsHandler
from digi_server.logger import get_logger
from utils.pkg_utils import find_end_modules
from utils.web.base_controller import BaseController, BaseAPIController
from utils.web.route import ApiRoute, ApiVersion, Route
IMPORTED_CONTROLLERS = {}
def import_all_controllers():
controllers = find_end_modules('.', prefix='controllers')
for controller in controllers:
if controller != __name__:
get_logger().debug(f'Importing controller module {controller}')
mod = importlib.import_module(controller)
IMPORTED_CONTROLLERS[controller] = mod
class RootController(BaseController):
def get(self, path):
file_path = os.path.join(
os.path.abspath(
os.path.dirname(__file__)),
"..",
"static")
full_path = os.path.join(file_path, "index.html")
if not os.path.isfile(full_path):
raise HTTPError(404)
with open(full_path, 'r') as file:
self.write(file.read())
class StaticController(BaseController):
def get(self):
self.set_header('Content-Type', '')
full_path = os.path.join(
os.path.abspath(
os.path.dirname(__file__)), "..", "static", url_unescape(
self.request.uri).strip(
os.path.sep))
if not os.path.isfile(full_path):
raise HTTPError(404)
try:
with open(full_path, 'r') as file:
self.write(file.read())
except UnicodeDecodeError:
with open(full_path, 'rb') as file:
self.write(file.read())
except Exception as exc:
raise HTTPError(500) from exc
class ApiFallback(BaseAPIController):
def get(self):
self.set_status(404)
self.write({'message': '404 not found'})
def post(self):
self.set_status(404)
self.write({'message': '404 not found'})
def patch(self):
self.set_status(404)
self.write({'message': '404 not found'})
def delete(self):
self.set_status(404)
self.write({'message': '404 not found'})
@Route('/debug')
class DebugController(BaseController):
def get(self):
self.set_status(200)
self.set_header('Content-Type', 'application/json')
self.write({
'status': 'OK',
'imported_controllers': list(IMPORTED_CONTROLLERS),
'imported_plugins': list(self.application.plugin_manager.IMPORTED_PLUGINS)
})
@ApiRoute('debug', ApiVersion.V1)
class ApiDebugController(BaseAPIController):
def get(self):
self.set_status(200)
self.set_header('Content-Type', 'application/json')
self.write({'status': 'OK', 'api_version': 1})
@Route('/debug/metrics')
class DebugMetricsController(MetricsHandler, BaseController):
pass