-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlauncher.py
More file actions
executable file
·161 lines (129 loc) · 4.57 KB
/
launcher.py
File metadata and controls
executable file
·161 lines (129 loc) · 4.57 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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
import asyncio
import aiohttp
import datetime
import aiohttp_jinja2
import jinja2
import os
import sys
import subprocess
import markdown2
import setproctitle
import logging
import json
from aiohttp import web
if "--close" in sys.argv:
with open("config.json") as f:
data = json.load(f)
c = aiohttp.ClientSession()
asyncio.get_event_loop().run_until_complete(c.post(f"http://127.0.0.1:{data['port']}/_internal/stop", data=data['slave_key']))
asyncio.get_event_loop().run_until_complete(c.close())
with open("backup/message.json") as f:
json.dump({
"message": "The service is currently restarting.",
"status": 503
}, f)
sys.exit(0)
logger = logging.getLogger("site")
logger.setLevel(10)
handle = logging.StreamHandler(sys.stderr)
handle.setFormatter(logging.Formatter("{levelname}[{name}] : {message}", style="{"))
logger.addHandler(handle)
try:
logger.warning("Pulling rtfs repos")
dirs = os.listdir("repos")
for d in dirs:
subprocess.run(["/bin/bash", "-c", f"cd repos/{d} && git pull"])
except:
logger.warning("Failed to pull repos")
else:
logger.warning("Pulled repos")
try:
import uvloop
uvloop.install()
except:
logger.warning("Failed to use uvloop")
import endpoints
from utils import app as _app, handler
setproctitle.setproctitle("Idevision site - Master")
uptime = datetime.datetime.utcnow()
app = _app.App()
endpoints.setup(app)
router = web.RouteTableDef()
@router.post("/api/files")
@handler.ratelimit(5, 20)
async def usercontent_upload(request: _app.TypedRequest, conn):
if not request.user:
return web.Response(reason="401 Unauthorized", status=401)
auth, perms, admin = request.user['username'], request.user['permissions'], "administrator" in request.user['permissions']
if not os.path.exists(f"/var/www/idevision/containers/{auth}"):
os.makedirs(f"/var/www/idevision/containers/{auth}")
if "multipart" in request.content_type:
d = await request.multipart()
data = await d.next()
filename = data.filename
if "/" in filename:
return web.Response(text="400 Bad filename", status=400)
pth = os.path.join("/var/www/idevision/containers", auth, filename)
with open(pth, "wb") as f:
while True:
chunk = await data.read_chunk()
if not chunk:
break
f.write(chunk)
else:
filename = request.headers.get("File-Name", None)
if not filename:
return web.Response(text="400 Missing File-Name header, and no multipart filename available", status=400)
if "/" in filename:
return web.Response(text="400 Bad filename", status=400)
pth = os.path.join("/var/www/idevision/containers", auth, filename)
with open(pth, "wb") as f:
while True:
data = await request.content.read(128)
if not data:
break
f.write(data)
return web.json_response({"url": f"https://container.idevision.net/{auth}/{filename}"}, status=200)
@router.post("/_internal/stop")
async def stop(request: web.Request):
if request.remote != "127.0.0.1":
return web.Response(status=401)
if await request.text() != app.settings['slave_key']:
return web.Response(status=401)
app.stop()
@router.get("/")
async def home(_):
return web.FileResponse("static/index.html")
logger.warning("Building docs...")
with open("static/docs.md") as f:
docs = markdown2.markdown(f.read(), extras=['fenced-code-blocks', 'break-on-newline'])
with open("static/docs.html") as f:
docs = f.read().replace("{{docs}}", docs)
logger.warning("Built docs.")
@router.get("/docs")
async def _docs(_):
return web.Response(text=docs, content_type="text/html")
@router.get("/newdocs")
async def _newdocs(_):
raise web.HTTPTemporaryRedirect("/static/redoc.html")
@router.get("/robots.txt")
async def robots(_):
return web.FileResponse("static/robots.txt")
@router.get("/favicon.ico")
async def favicon(_):
return web.FileResponse("static/favicon.ico")
logger.debug("Mounting static routes")
router.static("/vendor", "static/vendor")
router.static("/images", "static/images")
router.static("/fonts", "static/fonts")
router.static("/css", "static/css")
router.static("/js", "static/js")
router.static("/static", "static")
aiohttp_jinja2.setup(app, loader=jinja2.FileSystemLoader('./'))
app.add_routes(router)
if __name__ == "__main__":
web.run_app(
app,
host="127.0.0.1",
port=app.settings['port']
)