This repository has been archived by the owner on May 22, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathserve.py
230 lines (195 loc) · 7.04 KB
/
serve.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
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
import asyncio
import re
import os
from sanic import Sanic
from sanic.log import logger
from sanic import response
from sanic_cors import CORS
from sanic.response import HTTPResponse
import requests
from datetime import datetime, timedelta, timezone
from pathlib import Path
API_URL = os.environ.get("API_URL", "https://api.dandiarchive.org/api").rstrip("/")
API_LOCAL_URL = os.environ.get("API_LOCAL_URL", API_URL).rstrip("/")
GUI_URL = os.environ.get("GUI_URL", "https://gui.dandiarchive.org").rstrip("/")
ABOUT_URL = os.environ.get("ABOUT_URL", "https://www.dandiarchive.org").rstrip("/")
JUPYTERHUB_URL = os.environ.get(
"JUPYTERHUB_URL", "https://hub.dandiarchive.org"
).rstrip("/")
dandiset_identifier_regex = "^[0-9]{6}$"
production = "DEV628cc89a6444" not in os.environ
sem = None
basedir = os.environ["HOME"] if production else os.getcwd()
logdir = os.path.join(basedir, "redirector")
if not os.path.exists(logdir):
os.makedirs(logdir, exist_ok=True)
handler_dict = {
"class": "logging.handlers.TimedRotatingFileHandler",
"when": "D",
"interval": 7,
"backupCount": 140,
"formatter": "generic",
}
LOG_SETTINGS = dict(
version=1,
disable_existing_loggers=False,
loggers={
"sanic.root": {"level": "INFO", "handlers": ["consolefile"]},
"sanic.error": {
"level": "INFO",
"handlers": ["error_consolefile"],
"propagate": True,
"qualname": "sanic.error",
},
"sanic.access": {
"level": "INFO",
"handlers": ["access_consolefile"],
"propagate": True,
"qualname": "sanic.access",
},
},
handlers={
"consolefile": {
**handler_dict,
**{"filename": os.path.join(logdir, "console.log")},
},
"error_consolefile": {
**handler_dict,
**{"filename": os.path.join(logdir, "error.log")},
},
"access_consolefile": {
**handler_dict,
**{"filename": os.path.join(logdir, "access.log"), "formatter": "access"},
},
},
formatters={
"generic": {
"format": "%(asctime)s [%(process)d] [%(levelname)s] %(message)s",
"datefmt": "[%Y-%m-%d %H:%M:%S %z]",
"class": "logging.Formatter",
},
"access": {
"format": "%(asctime)s - (%(name)s)[%(levelname)s][%(host)s]: "
+ "%(request)s %(message)s %(status)d %(byte)d",
"datefmt": "[%Y-%m-%d %H:%M:%S %z]",
"class": "logging.Formatter",
},
},
)
if production:
app = Sanic("redirector", log_config=LOG_SETTINGS)
else:
app = Sanic("redirector")
CORS(app)
def make_header(url):
import time
gmtnow = time.strftime("%a, %d %b %Y %I:%M:%S %p %Z", time.gmtime())
header = {
"location": url,
"content-type": "text/plain;charset=UTF-8",
"content-length": 0,
"date": gmtnow,
"alt-svc": "clear",
}
return header
@app.listener("before_server_start")
async def init(app, loop):
global sem
sem = asyncio.Semaphore(100)
@app.route("/", methods=["GET"])
async def main(request):
return response.redirect(GUI_URL + "/")
@app.route("/about", methods=["GET"])
async def about(request):
return response.redirect(ABOUT_URL)
@app.route("/dandiset", methods=["GET"])
async def goto_public_dashboard(request):
"""Redirect to GUI public dandisets
"""
return response.redirect(f"{GUI_URL}/#/dandiset")
@app.route("/dandiset/<dataset>", methods=["GET", "HEAD"])
async def goto_dandiset(request, dataset):
"""Redirect to GUI with dandiset identifier
"""
if not re.fullmatch(dandiset_identifier_regex, dataset):
return response.text(f"{dataset}: invalid Dandiset ID", status=400)
req = requests.get(f"{API_LOCAL_URL}/dandisets/{dataset}")
if req.reason == "OK":
url = f"{GUI_URL}/#/dandiset/{dataset}/draft"
if request.method == "HEAD":
return response.html(None, status=302, headers=make_header(url))
return response.redirect(url)
return response.text(f"dandi:{dataset} not found.", status=404)
@app.route("/dandiset/<dataset>/<version>", methods=["GET", "HEAD"])
async def goto_dandiset_version(request, dataset, version):
"""Redirect to GUI with dandiset identifier and version
"""
if not re.fullmatch(dandiset_identifier_regex, dataset):
return response.text(f"{dataset}: invalid Dandiset ID", status=400)
req = requests.get(f"{API_LOCAL_URL}/dandisets/{dataset}/versions/{version}")
if req.reason == "OK":
url = f"{GUI_URL}/#/dandiset/{dataset}/{version}"
if request.method == "HEAD":
return response.html(None, status=302, headers=make_header(url))
return response.redirect(url)
return response.text(f"dandi:{dataset}/{version} not found.", status=404)
@app.route("/server-info", methods=["GET"])
async def server_info(request):
return response.json(
{
"version": "1.2.0",
"cli-minimal-version": "0.14.2",
"cli-bad-versions": [],
"services": {
"girder": {"url": None},
"api": {"url": API_LOCAL_URL},
"webui": {"url": GUI_URL},
"jupyterhub": {"url": JUPYTERHUB_URL},
},
},
indent=4,
)
async def _fetch(url):
querystring = {"page_size": "100"}
headers = {"Accept": "application/json", "Content-Type": "application/json"}
resp = requests.request("GET", url, headers=headers, params=querystring).json()
results = resp["results"]
while resp["next"]:
resp = requests.request("GET", resp["next"], headers=headers).json()
results.extend(resp["results"])
return results
async def _sitemap():
sitemapfile = Path("sitemap.xml")
if sitemapfile.exists():
modified = datetime.fromtimestamp(sitemapfile.stat().st_mtime, tz=timezone.utc)
if (datetime.now(tz=timezone.utc) - modified) < timedelta(days=1):
return sitemapfile.read_text()
sitemap = [
"""<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">"""
]
for ds in await _fetch("https://api.dandiarchive.org/api/dandisets"):
versions = await _fetch(
f"https://api.dandiarchive.org/api/dandisets/"
f"{ds['identifier']}/versions"
)
for version in versions:
url = (
f"https://dandiarchive.org/dandiset/{ds['identifier']}/"
f"{version['version']}"
)
sitemap.append(
f"""<url><loc>{url}</loc><lastmod>{version['modified']}</lastmod></url>"""
)
sitemap.append("</urlset>")
sitemap = "\n".join(sitemap)
sitemapfile.write_text(sitemap)
return sitemap
@app.route("sitemap.xml", methods=["GET"])
async def sitemap(request):
return HTTPResponse(
await _sitemap(), status=200, headers=None, content_type="text/xml"
)
if __name__ == "__main__":
logger.info("Starting backend")
app.run(host="0.0.0.0", port=8080)