15 lines
918 B
Python
15 lines
918 B
Python
import http.server,os,json,functools
|
|
from pathlib import Path
|
|
r=Path(__file__).resolve().parents[1]
|
|
static=Path(os.environ.get('PROGCODE_STATIC_ROOT',str(r/'web/out'))).resolve()
|
|
class Handler(http.server.SimpleHTTPRequestHandler):
|
|
def send_error(self,code,message=None,explain=None):
|
|
if code==404 and (static/'404.html').exists():
|
|
b=(static/'404.html').read_bytes();self.send_response(404);self.send_header('Content-Type','text/html; charset=utf-8');self.send_header('Content-Length',str(len(b)));self.end_headers()
|
|
if self.command!='HEAD':self.wfile.write(b)
|
|
else:super().send_error(code,message,explain)
|
|
handler=functools.partial(Handler,directory=str(static))
|
|
s=http.server.ThreadingHTTPServer(('127.0.0.1',0),handler)
|
|
(r/'.local-service.json').write_text(json.dumps(dict(pid=os.getpid(),port=s.server_port,url='http://127.0.0.1:'+str(s.server_port),staticRoot=str(static)),indent=2))
|
|
s.serve_forever()
|