/
app.py
62 lines (47 loc) · 1.63 KB
/
app.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
#!/usr/bin/env python
import argparse
from http import HTTPStatus
import http.server
import os
import signal
import subprocess
import threading
DEFAULT_PORT = 8080
class Server:
def __init__(self, command, port):
self.command = command
self.port = port
def start(self):
handler = self._handler()
self.http_server = http.server.HTTPServer(('', self.port), handler)
try:
self.http_server.serve_forever()
except KeyboardInterrupt:
pass
def stop(self):
self.http_server.shutdown()
def _handler(self):
command = self.command
class Handler(http.server.BaseHTTPRequestHandler):
def do_GET(self):
if self.path != '/':
self.send_error(HTTPStatus.NOT_FOUND)
self.send_response(HTTPStatus.OK)
self.end_headers()
subprocess.run(command, stdout=self.wfile, stderr=self.wfile)
return Handler
def run(command, port):
server = Server(command, port)
threading.Thread(target=server.start).start()
signal.signal(signal.SIGTERM, lambda signum, frame: server.stop())
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='One-track web server.')
parser.add_argument(
'command', type=str, nargs=argparse.REMAINDER,
help='the command to run (e.g. `echo "Hello, World!"`)')
parser.add_argument(
'-p', '--port', type=int,
default=os.environ.get('PORT', DEFAULT_PORT),
help='the web server port')
args = parser.parse_args()
run(args.command, args.port)