From ae818f6a9ac9e33e400fd4f4906ba52dd95d70ed Mon Sep 17 00:00:00 2001 From: Thomas L Date: Sat, 20 Jun 2020 14:32:06 +0200 Subject: [PATCH] serve frontend --- README.md | 19 +++++++++++++++++++ pizzatool/__init__.py | 18 ++++++++++++++++-- 2 files changed, 35 insertions(+), 2 deletions(-) create mode 100644 README.md diff --git a/README.md b/README.md new file mode 100644 index 0000000..6925ddf --- /dev/null +++ b/README.md @@ -0,0 +1,19 @@ +# Pizzatool +``` +# create virtualenv +virtualenv-3 venv +# activate virtualenv +. venv/bin/activate +# install pizzatool +pip install . +# run pizzatool with frontend in frontend//dist +pizzatool frontend//dist +``` + +# Frontend Showdown +For the showdown create your frontend somewhere in `frontend/`. +Running `pizzatool frontend//dist` will serve +`frontend//dist` at `http://localhost:8000/` and the REST API at +`http://localhost:8000/api`. You can inspect and test the API in +`http://localhost:8000/api/docs`. +Happy Hacking! diff --git a/pizzatool/__init__.py b/pizzatool/__init__.py index ce2c4ec..27882a9 100644 --- a/pizzatool/__init__.py +++ b/pizzatool/__init__.py @@ -1,10 +1,24 @@ from asyncio import run as asyncio_run +from fastapi.staticfiles import StaticFiles +from fastapi import FastAPI from hypercorn.config import Config from hypercorn.asyncio import serve from logging import basicConfig, DEBUG -from pizzatool.api import app +from pizzatool.api import app as api +from sys import argv +class SinglePageApplication(StaticFiles): + async def get_response(self, path, scope): + response = await super().get_response(path, scope) + if response.status_code == 404: + response = await super().get_response('.', scope) + return response def main(): basicConfig(level=DEBUG, format="%(levelname)s %(name)s %(message)s") - asyncio_run(serve(app, Config())) + app = FastAPI() + config = Config() + config.accesslog = '-' + app.mount('/api', app=api) + app.mount('/', app=SinglePageApplication(directory=argv[1], html=True)) + asyncio_run(serve(app, config))