Update server app to serve static files.
This commit is contained in:
parent
b2c0e41c79
commit
357c74ae10
|
@ -17,7 +17,7 @@ from aiohttp_session import setup, session_middleware
|
||||||
from aiohttp_session.cookie_storage import EncryptedCookieStorage
|
from aiohttp_session.cookie_storage import EncryptedCookieStorage
|
||||||
|
|
||||||
from bot_z.async_operator import AsyncOperator
|
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
|
from api.conf import read_conf
|
||||||
|
|
||||||
|
|
||||||
|
@ -70,6 +70,7 @@ def run(
|
||||||
if conf["http"].get("cors_allow"):
|
if conf["http"].get("cors_allow"):
|
||||||
app.on_response_prepare.append(on_prepare_cors)
|
app.on_response_prepare.append(on_prepare_cors)
|
||||||
setup_session(app, conf["http"]["cookie_secure"])
|
setup_session(app, conf["http"]["cookie_secure"])
|
||||||
|
add_static_routes(alog)
|
||||||
app.add_routes(routes)
|
app.add_routes(routes)
|
||||||
addr = []
|
addr = []
|
||||||
if address is not None:
|
if address is not None:
|
||||||
|
|
40
api/rest.py
40
api/rest.py
|
@ -7,6 +7,8 @@ The REST endpoints.
|
||||||
from aiohttp import web
|
from aiohttp import web
|
||||||
import datetime
|
import datetime
|
||||||
import logging
|
import logging
|
||||||
|
import os
|
||||||
|
import pkg_resources
|
||||||
import typing as T
|
import typing as T
|
||||||
|
|
||||||
from aiohttp_session import get_session, Session
|
from aiohttp_session import get_session, Session
|
||||||
|
@ -19,6 +21,7 @@ from api import BASE_URI, DEBUG
|
||||||
alog = logging.getLogger("api")
|
alog = logging.getLogger("api")
|
||||||
routes = web.RouteTableDef()
|
routes = web.RouteTableDef()
|
||||||
OPERATORS = {} # type: T.Dict[T.Text, AsyncOperator]
|
OPERATORS = {} # type: T.Dict[T.Text, AsyncOperator]
|
||||||
|
BASE_PATH = pkg_resources.resource_filename("api", "assets")
|
||||||
|
|
||||||
|
|
||||||
async def get_set_operator(
|
async def get_set_operator(
|
||||||
|
@ -38,9 +41,30 @@ async def get_set_operator(
|
||||||
return op, session
|
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("/")
|
||||||
@routes.get("/login")
|
async def home_handle(request: web.Request) -> web.Response:
|
||||||
@routes.get("/badge")
|
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:
|
async def routing_handler(request: web.Request) -> web.Response:
|
||||||
alog.debug("(%s) %s", request.method, request.path)
|
alog.debug("(%s) %s", request.method, request.path)
|
||||||
session = await get_session(request)
|
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})
|
return web.json_response({"logged_in": _logged_in})
|
||||||
|
|
||||||
|
|
||||||
@routes.post("/login")
|
@routes.post("/api/login")
|
||||||
async def login_handler(request: web.Request) -> web.Response:
|
async def login_handler(request: web.Request) -> web.Response:
|
||||||
data = await request.json()
|
data = await request.json()
|
||||||
user = data.get("username")
|
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)
|
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:
|
async def logout_handler(request: web.Request) -> web.Response:
|
||||||
alog.debug("logout")
|
alog.debug("logout")
|
||||||
session = await get_session(request)
|
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)
|
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:
|
async def checkin_handler(request: web.Request) -> web.Response:
|
||||||
alog.debug("checkin")
|
alog.debug("checkin")
|
||||||
session = await get_session(request)
|
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)
|
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:
|
async def checkout_handler(request: web.Request) -> web.Response:
|
||||||
alog.debug("checkout")
|
alog.debug("checkout")
|
||||||
session = await get_session(request)
|
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)
|
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:
|
async def movements_handle(request: web.Request) -> web.Response:
|
||||||
alog.debug("movements")
|
alog.debug("movements")
|
||||||
session = await get_session(request)
|
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)
|
return web.json_response(resp_data, status=200)
|
||||||
|
|
||||||
|
|
||||||
@routes.get("/status")
|
@routes.get("/api/status")
|
||||||
async def status_handle(request: web.Request) -> web.Response:
|
async def status_handle(request: web.Request) -> web.Response:
|
||||||
alog.debug("status")
|
alog.debug("status")
|
||||||
session = await get_session(request)
|
session = await get_session(request)
|
||||||
|
|
Loading…
Reference in New Issue
Block a user