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

from fastapi.middleware.cors import CORSMiddleware from fastapi.middleware.cors import CORSMiddleware


import reporthook import reporthook
import sbenv
import searchlib import searchlib


MAX_SEARCH_DAYS = 180
MAX_SHOW_DAYS = 20

################################ ################################
# Core configuration # Core configuration
################################ ################################


app = fastapi.FastAPI()
app = fastapi.FastAPI(docs_url=None)


origins = [ origins = [
'https://prograde.gg', 'https://prograde.gg',
tmplts = Jinja2Templates(directory='templates') # TODO Get the path correctly. tmplts = Jinja2Templates(directory='templates') # TODO Get the path correctly.


################################ ################################
# Core configuration
# User-facing endpoints
################################ ################################


@app.exception_handler(Exception) @app.exception_handler(Exception)


return tmplts.TemplateResponse('main.htm', p) 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 # 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 def load_days_from_file(path):
async with aiofiles.open(path, mode='r') as f: async with aiofiles.open(path, mode='r') as f:
contents = await f.read() contents = await f.read()
if len(day['links']) > 0: if len(day['links']) > 0:
output.append(day) output.append(day)


if len(output) > MAX_SHOW_DAYS:
break

return output return output


def convert_article(a): def convert_article(a):

+ 6
- 0
sbenv.py View File

if hu is not None: if hu is not None:
hu = hu.strip() hu = hu.strip()
return hu 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

mkdir -p $workdir/datadir mkdir -p $workdir/datadir


export SB_DATADIR=$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_COOKIES_TXT=$workdir/cookies.txt
export SB_GOOGABUSE=$(cat $workdir/googabuse.txt) export SB_GOOGABUSE=$(cat $workdir/googabuse.txt)
export SB_REPORT_WH=$(cat $workdir/webhook.txt) export SB_REPORT_WH=$(cat $workdir/webhook.txt)

Loading…
Cancel
Save