-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.py
More file actions
executable file
·52 lines (41 loc) · 1.67 KB
/
server.py
File metadata and controls
executable file
·52 lines (41 loc) · 1.67 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
#!/usr/bin/env python3
"""
3D Printer Management Server
"""
import os
import asyncio
from configparser import ConfigParser
from tornado.web import Application, StaticFileHandler, RequestHandler, RedirectHandler
from tornado.options import define, options, parse_command_line
from info import InfoHandler
from model import ModelHandler
from video import VideoHandler, VideoStaticFileHandler, terminate_video_streams
define("port", default=8888, help="Port to listen on")
class TemplateHandler(RequestHandler): # pylint: disable=abstract-method
"""Handles .html files that are templates as .html.template"""
def get(self, cat, name): # pylint: disable=arguments-differ
self.render(cat+"/"+cat+".html.template", name=name)
async def main():
# Get the config information
config = ConfigParser()
directory = os.path.dirname(os.path.abspath(__file__))
config.read(os.path.join(directory, 'config.ini'))
app = Application([
(r"/info/(.*)\.json", InfoHandler),
(r"/model/(.*\.(?:gcode|json|obj))", ModelHandler, {"path":"model"}),
(r"/video/(.*)\.m3u8", VideoHandler),
(r"/video/(.*\.ts)", VideoStaticFileHandler),
(r"/(model|video)/(.*)\.html", TemplateHandler),
(r"/", RedirectHandler, {"url": "/dashboard"}),
(r"/(.*)", StaticFileHandler, {"path":".", "default_filename":"index.html"}),
], debug=True, autoreload=False, config=config)
# Start
print(f"Listening on port {options.port}...")
app.listen(options.port)
await asyncio.Event().wait()
if __name__ == "__main__":
parse_command_line()
try:
asyncio.run(main())
finally:
terminate_video_streams()