Commit 8697a4f0 authored by Eelco van der Wel's avatar Eelco van der Wel :speech_balloon:
Browse files

Merge branch 'eelco/serve-qr-string' into 'dev'

Serve QR as json string and json svg

See merge request !232
parents ab10073a a195264e
Pipeline #9989 passed with stages
in 1 minute and 27 seconds
Showing with 58 additions and 5 deletions
+58 -5
import os
from .helpers import get_root_path, DictProxy
from fastapi import APIRouter, Request
from fastapi.responses import HTMLResponse, JSONResponse
from fastapi.templating import Jinja2Templates
from fastapi.responses import HTMLResponse
from .helpers import DictProxy, get_root_path
qr_code_dict = DictProxy()
......@@ -20,6 +22,7 @@ templates = Jinja2Templates(directory=os.path.join(get_root_path(__name__), "tem
router = APIRouter()
QR_CODE_KEY = "qr_code"
QR_STRING_KEY = "qr_string"
AUTHENTICATED = "authenticated"
......@@ -37,3 +40,35 @@ def qr(request: Request):
return templates.TemplateResponse("success.html", {"request": request})
else:
return templates.TemplateResponse("images.html", {"request": request, "chart_output": qr_code_data})
@router.get("/qr_svg", response_class=JSONResponse)
def qr_svg():
"""Returns the QR svg as json"""
global qr_code_dict
content = {
"qr": qr_code_dict.get(QR_CODE_KEY, None),
"authenticated": qr_code_dict.get(AUTHENTICATED, False),
}
return JSONResponse(
content=content,
headers={'Access-Control-Allow-Origin': '*'}
)
@router.get("/qr_string", response_class=JSONResponse)
def qr_string():
"""Returns the QR svg as json"""
global qr_code_dict
content = {
"qr": qr_code_dict.get(QR_STRING_KEY, None).decode("utf-8"),
"authenticated": qr_code_dict.get(AUTHENTICATED, False),
}
return JSONResponse(
content=content,
headers={'Access-Control-Allow-Origin': '*'}
)
import threading
from uvicorn import Server, Config
from fastapi import FastAPI
from starlette.middleware.cors import CORSMiddleware
from uvicorn import Config, Server
class WebServer:
def __init__(self, port: int):
self._app = FastAPI(title="Plugin Webserver", redoc_url=None,
swagger_ui_oauth2_redirect_url=None)
self._app = self._setup_app()
self._server_handle = None
self._uvicorn = None
self._port = port
def _setup_app(self) -> FastAPI:
app = FastAPI(
title="Plugin Webserver",
redoc_url=None,
swagger_ui_oauth2_redirect_url=None)
# TODO setup allow_origin_regex for *.memri.io and localhost
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_methods=["*"],
allow_headers=["*"],
)
return app
@property
def app(self) -> FastAPI:
return self._app
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment