|
|
@@ -12,13 +12,17 @@ from fastapi.templating import Jinja2Templates |
|
|
|
from fastapi.middleware.cors import CORSMiddleware |
|
|
|
|
|
|
|
import reporthook |
|
|
|
import sbenv |
|
|
|
import searchlib |
|
|
|
|
|
|
|
MAX_SEARCH_DAYS = 180 |
|
|
|
MAX_SHOW_DAYS = 20 |
|
|
|
|
|
|
|
################################ |
|
|
|
# Core configuration |
|
|
|
################################ |
|
|
|
|
|
|
|
app = fastapi.FastAPI() |
|
|
|
app = fastapi.FastAPI(docs_url=None) |
|
|
|
|
|
|
|
origins = [ |
|
|
|
'https://prograde.gg', |
|
|
@@ -41,7 +45,7 @@ app.mount('/static', StaticFiles(directory='static'), name='static') |
|
|
|
tmplts = Jinja2Templates(directory='templates') # TODO Get the path correctly. |
|
|
|
|
|
|
|
################################ |
|
|
|
# Core configuration |
|
|
|
# User-facing endpoints |
|
|
|
################################ |
|
|
|
|
|
|
|
@app.exception_handler(Exception) |
|
|
@@ -66,10 +70,47 @@ async def render_main(req: Request): |
|
|
|
|
|
|
|
return tmplts.TemplateResponse('main.htm', p) |
|
|
|
|
|
|
|
################################ |
|
|
|
# API endpoints |
|
|
|
################################ |
|
|
|
|
|
|
|
@app.post('/api/addarticle') |
|
|
|
async def handle_addarticle(req: Request): |
|
|
|
if not check_admin_token(req): |
|
|
|
return JSONResponse(status_code=403, content={'error': 'forbidden'}) |
|
|
|
|
|
|
|
body = await req.json() |
|
|
|
add_article(body) |
|
|
|
|
|
|
|
return {'status': 'OK'} |
|
|
|
|
|
|
|
def add_article(article): |
|
|
|
# TODO |
|
|
|
pass |
|
|
|
|
|
|
|
################################ |
|
|
|
# Utilities |
|
|
|
################################ |
|
|
|
|
|
|
|
def check_admin_token(req: Request): |
|
|
|
ak = sbenv.get_admin_key() |
|
|
|
if ak is None: |
|
|
|
raise RuntimeError('checked api endpoint without key loaded') |
|
|
|
|
|
|
|
if ak == 'UNSAFE_TESTING': |
|
|
|
return True |
|
|
|
|
|
|
|
if 'Authorization' in req.headers: |
|
|
|
auth = req.headers['Authorization'] |
|
|
|
|
|
|
|
if not auth.startswith('Bearer '): |
|
|
|
return False |
|
|
|
|
|
|
|
tok = auth[len('Bearer '):] |
|
|
|
return tok == sbenv.get_admin_key() |
|
|
|
else: |
|
|
|
return False |
|
|
|
|
|
|
|
async def load_days_from_file(path): |
|
|
|
async with aiofiles.open(path, mode='r') as f: |
|
|
|
contents = await f.read() |
|
|
@@ -89,6 +130,9 @@ def convert_days_from_articles(rarts): |
|
|
|
if len(day['links']) > 0: |
|
|
|
output.append(day) |
|
|
|
|
|
|
|
if len(output) > MAX_SHOW_DAYS: |
|
|
|
break |
|
|
|
|
|
|
|
return output |
|
|
|
|
|
|
|
def convert_article(a): |