Browse Source

Getting ready to start adding article storage code.

master
Trey Del Bonis 2 years ago
parent
commit
d3b5421d1a
3 changed files with 53 additions and 3 deletions
  1. 46
    2
      app.py
  2. 6
    0
      sbenv.py
  3. 1
    1
      venv-run.sh

+ 46
- 2
app.py View File

@@ -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):

+ 6
- 0
sbenv.py View File

@@ -20,3 +20,9 @@ def get_report_webhook():
if hu is not None:
hu = hu.strip()
return hu

def get_admin_key():
ak = os.getenv('SB_ADMIN_KEY')
if ak is not None:
ak = ak.strip()
return ak

+ 1
- 1
venv-run.sh View File

@@ -7,7 +7,7 @@ workdir=$(realpath _test)
mkdir -p $workdir/datadir

export SB_DATADIR=$workdir/datadir
export SB_ADMIN_KEY=$(cat $workdir/adminkey)
export SB_ADMIN_KEY=$(cat $workdir/adminkey.txt)
export SB_COOKIES_TXT=$workdir/cookies.txt
export SB_GOOGABUSE=$(cat $workdir/googabuse.txt)
export SB_REPORT_WH=$(cat $workdir/webhook.txt)

Loading…
Cancel
Save