Implemented login page and new jwt-based sessions. Watching cleanup.

This commit is contained in:
Leo Vasanko
2023-10-18 01:06:27 +03:00
committed by Leo Vasanko
parent bd680e3668
commit 429a7dfb16
6 changed files with 166 additions and 59 deletions

View File

@@ -1,18 +1,21 @@
import asyncio
from importlib.resources import files
import msgspec
from sanic import Sanic
from html5tagger import E
from sanic import Sanic, redirect
from sanic.log import logger
from sanic.response import html
from . import watching
from . import session, watching
from .auth import authbp
from .config import config
from .fileio import ROOT, FileServer
from .protocol import ErrorMsg, FileRange, StatusMsg
app = Sanic("cista")
fileserver = FileServer()
watching.register(app, "/api/watch")
app.blueprint(authbp)
def asend(ws, msg):
return ws.send(msg if isinstance(msg, bytes) else msgspec.json.encode(msg).decode())
@@ -27,7 +30,14 @@ async def stop_fileserver(app, _):
@app.get("/")
async def index_page(request):
s = config.public or session.get(request)
print("Main session", s)
if not s:
return redirect("/login")
index = files("cista").joinpath("static", "index.html").read_text()
flash = request.cookies.flash
if flash:
index += str(E.div(flash, id="flash"))
return html(index)
app.static("/files", ROOT, use_content_range=True, stream_large_files=True, directory_view=True)
@@ -59,12 +69,6 @@ async def upload(request, ws):
logger.exception(repr(res), e)
return
@app.websocket("/ws")
async def ws(request, ws):
while True:
data = await ws.recv()
await ws.send(data)
@app.websocket('/api/download')
async def download(request, ws):
alink = fileserver.alink