Update server app to serve static files.

This commit is contained in:
sfigato 2019-08-07 17:52:58 +02:00 committed by blallo
parent b2c0e41c79
commit 357c74ae10
Signed by: blallo
GPG Key ID: 0CBE577C9B72DC3F
2 changed files with 34 additions and 9 deletions

View File

@ -17,7 +17,7 @@ from aiohttp_session import setup, session_middleware
from aiohttp_session.cookie_storage import EncryptedCookieStorage
from bot_z.async_operator import AsyncOperator
from api.rest import routes
from api.rest import routes, add_static_routes
from api.conf import read_conf
@ -70,6 +70,7 @@ def run(
if conf["http"].get("cors_allow"):
app.on_response_prepare.append(on_prepare_cors)
setup_session(app, conf["http"]["cookie_secure"])
add_static_routes(alog)
app.add_routes(routes)
addr = []
if address is not None:

View File

@ -7,6 +7,8 @@ The REST endpoints.
from aiohttp import web
import datetime
import logging
import os
import pkg_resources
import typing as T
from aiohttp_session import get_session, Session
@ -19,6 +21,7 @@ from api import BASE_URI, DEBUG
alog = logging.getLogger("api")
routes = web.RouteTableDef()
OPERATORS = {} # type: T.Dict[T.Text, AsyncOperator]
BASE_PATH = pkg_resources.resource_filename("api", "assets")
async def get_set_operator(
@ -38,9 +41,30 @@ async def get_set_operator(
return op, session
def add_static_routes(log: logging.Logger) -> None:
static_assets = [
os.path.abspath(os.path.join(BASE_PATH, path))
for path in os.listdir(BASE_PATH)
if os.path.isdir(os.path.join(BASE_PATH, path))
]
for asset in static_assets:
asset_path = os.path.relpath(asset, BASE_PATH)
log.debug(f"Linking: {asset_path} -> {asset}")
routes.static(f"/{asset_path}", asset)
@routes.get("/")
@routes.get("/login")
@routes.get("/badge")
async def home_handle(request: web.Request) -> web.Response:
return web.FileResponse(os.path.join(BASE_PATH, "index.html"))
@routes.get("/favicon.ico")
async def favicon_handle(request: web.Request) -> web.Response:
return web.FileResponse(os.path.join(BASE_PATH, "favicon.ico"))
@routes.get("/api/login")
@routes.get("/api/badge")
async def routing_handler(request: web.Request) -> web.Response:
alog.debug("(%s) %s", request.method, request.path)
session = await get_session(request)
@ -50,7 +74,7 @@ async def routing_handler(request: web.Request) -> web.Response:
return web.json_response({"logged_in": _logged_in})
@routes.post("/login")
@routes.post("/api/login")
async def login_handler(request: web.Request) -> web.Response:
data = await request.json()
user = data.get("username")
@ -68,7 +92,7 @@ async def login_handler(request: web.Request) -> web.Response:
return web.json_response({"logged_in": res}, status=200)
@routes.post("/logout")
@routes.post("/api/logout")
async def logout_handler(request: web.Request) -> web.Response:
alog.debug("logout")
session = await get_session(request)
@ -85,7 +109,7 @@ async def logout_handler(request: web.Request) -> web.Response:
return web.json_response({"logged_in": res}, status=200)
@routes.post("/checkin")
@routes.post("/api/checkin")
async def checkin_handler(request: web.Request) -> web.Response:
alog.debug("checkin")
session = await get_session(request)
@ -99,7 +123,7 @@ async def checkin_handler(request: web.Request) -> web.Response:
return web.json_response({"checked_in": res, "logged_in": True}, status=200)
@routes.post("/checkout")
@routes.post("/api/checkout")
async def checkout_handler(request: web.Request) -> web.Response:
alog.debug("checkout")
session = await get_session(request)
@ -113,7 +137,7 @@ async def checkout_handler(request: web.Request) -> web.Response:
return web.json_response({"checked_in": res, "logged_in": True}, status=200)
@routes.get("/movements")
@routes.get("/api/movements")
async def movements_handle(request: web.Request) -> web.Response:
alog.debug("movements")
session = await get_session(request)
@ -137,7 +161,7 @@ async def movements_handle(request: web.Request) -> web.Response:
return web.json_response(resp_data, status=200)
@routes.get("/status")
@routes.get("/api/status")
async def status_handle(request: web.Request) -> web.Response:
alog.debug("status")
session = await get_session(request)