-
Notifications
You must be signed in to change notification settings - Fork 3
/
dockerpanel.py
48 lines (40 loc) · 1.58 KB
/
dockerpanel.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
from os import environ as env
import http.server
from http.server import HTTPServer, CGIHTTPRequestHandler
def dockerpanel():
# replace nobody_uid function that always returns 0 (root)
# hack to make CGI processes run as root, required to access docker socket
http.server.nobody_uid = lambda *args, **kwargs: 0
port = int(env['PORT']) if 'PORT' in env else 8080
server_address = ('', port)
handler = DockerPanelRequestHandler
httpd = HTTPServer(server_address, handler)
try:
httpd.serve_forever()
except KeyboardInterrupt:
httpd.socket.close()
class DockerPanelRequestHandler(CGIHTTPRequestHandler):
cgi_directories = ['/api']
def do_GET(self):
if self.path == '/index.html':
self.send_response(302)
self.send_header('Location', '/')
self.end_headers()
elif self.path == '/':
self.send_response(200)
self.end_headers()
title = env['TITLE'] if 'TITLE' in env else 'Dockerpanel'
with open('index.html', 'r') as file:
content = file.read().replace('{{TITLE}}', title)
self.wfile.write(content.encode())
elif self.path == '/theme':
self.send_response(200)
self.send_header('Content-Type', 'text/css')
self.end_headers()
theme = env['THEME'] if 'THEME' in env else 'default'
with open(f'themes/{theme}.css', 'rb') as file:
self.wfile.write(file.read())
else:
super().do_GET()
if __name__ == '__main__':
dockerpanel()