You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

app.py 1.4KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. import traceback
  2. import fastapi
  3. from fastapi import Cookie, File, Form, Request, UploadFile, WebSocket, WebSocketDisconnect
  4. from fastapi.responses import FileResponse, HTMLResponse, JSONResponse, RedirectResponse, PlainTextResponse
  5. from fastapi.staticfiles import StaticFiles
  6. from fastapi.templating import Jinja2Templates
  7. from fastapi.middleware.cors import CORSMiddleware
  8. import reporthook
  9. ################################
  10. # Core configuration
  11. ################################
  12. app = fastapi.FastAPI()
  13. origins = [
  14. 'https://prograde.gg',
  15. 'http://localhost',
  16. 'http://localhost:5000',
  17. 'http://localhost:8080',
  18. 'http://localhost:8000'
  19. ]
  20. app.add_middleware(
  21. CORSMiddleware,
  22. allow_origins=origins,
  23. allow_credentials=True,
  24. allow_methods=['*'],
  25. allow_headers=['*']
  26. )
  27. app.mount('/static', StaticFiles(directory='static'), name='static')
  28. tmplts = Jinja2Templates(directory='templates') # TODO Get the path correctly.
  29. ################################
  30. # Core configuration
  31. ################################
  32. @app.exception_handler(Exception)
  33. async def handle_exception(req: Request, exc: Exception):
  34. tb = traceback.format_exc()
  35. await reporthook.send_report(tb)
  36. return PlainTextResponse('error', status_code=500)
  37. @app.get('/')
  38. def render_main(req: Request):
  39. p = {
  40. 'num_days': 5,
  41. 'request': req,
  42. }
  43. return tmplts.TemplateResponse('main.htm', p)