35 lines
1.6 KiB
Python
35 lines
1.6 KiB
Python
"""Serve the built static portfolio locally, without application access."""
|
|
from http.server import SimpleHTTPRequestHandler, ThreadingHTTPServer
|
|
from pathlib import Path
|
|
from urllib.request import Request, urlopen
|
|
from urllib.error import URLError
|
|
import json
|
|
|
|
ROOT = Path(__file__).resolve().parents[1] / "dist"
|
|
class Handler(SimpleHTTPRequestHandler):
|
|
def __init__(self, *args, **kwargs):
|
|
super().__init__(*args, directory=str(ROOT), **kwargs)
|
|
def do_GET(self):
|
|
if self.path.startswith('/api/'):
|
|
if self.path != '/api/v1/users/publish-bot/repos?limit=50&sort=updated':
|
|
self.send_error(404)
|
|
return
|
|
try:
|
|
request = Request('https://code.knickqueue.win'+self.path, headers={'Accept':'application/json','User-Agent':'Portfolio-local-preview'})
|
|
with urlopen(request, timeout=8) as response:
|
|
data = response.read(512_000)
|
|
if not isinstance(json.loads(data), list): raise ValueError('Invalid public listing')
|
|
self.send_response(200)
|
|
self.send_header('Content-Type','application/json')
|
|
self.send_header('Content-Length',str(len(data)))
|
|
self.end_headers()
|
|
self.wfile.write(data)
|
|
except (URLError, ValueError, TimeoutError):
|
|
self.send_error(503, 'Public listing unavailable')
|
|
return
|
|
super().do_GET()
|
|
|
|
if __name__ == '__main__':
|
|
print('Portfolio preview: http://127.0.0.1:4387/', flush=True)
|
|
ThreadingHTTPServer(('127.0.0.1',4387),Handler).serve_forever()
|