-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #13 from uw935/feature/fastapi
v1.3: Backend rewrite to FastAPI
- Loading branch information
Showing
5 changed files
with
104 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,41 +1,116 @@ | ||
import uvicorn | ||
import requests | ||
from flask import Flask, render_template, abort, request, jsonify | ||
|
||
from fastapi import ( | ||
FastAPI, | ||
Request, | ||
HTTPException, | ||
) | ||
from fastapi.responses import FileResponse | ||
from starlette.staticfiles import StaticFiles | ||
from fastapi.templating import Jinja2Templates | ||
|
||
|
||
app = Flask(__name__) | ||
VATSIM_API_URL = "https://api.vatsim.net/v2/" | ||
|
||
app = FastAPI( | ||
title="VATSIM Fox", | ||
docs_url=None, | ||
redoc_url=None, | ||
openapi_url=None, | ||
redirect_slashes=True | ||
) | ||
templates = Jinja2Templates(directory="templates") | ||
app.mount("/static", StaticFiles(directory="static"), name="static") | ||
|
||
@app.route("/") | ||
def index_page(): | ||
return render_template("index.html") | ||
|
||
@app.get("/") | ||
async def get_index_page(request: Request): | ||
""" | ||
Index page | ||
@app.errorhandler(404) | ||
def errorhandler_page(_): | ||
return render_template("4O4.html"), 404 | ||
:param request: FastAPI request | ||
:return: HTML template | ||
""" | ||
|
||
return templates.TemplateResponse( | ||
name="index.html", | ||
context={ | ||
"request": request, | ||
} | ||
) | ||
|
||
@app.route("/api/request") | ||
def request_handler(): | ||
if request_string := request.args.get("request_string"): | ||
try: | ||
return requests.get(f"{VATSIM_API_URL}{request_string}").json() | ||
except requests.exceptions.JSONDecodeError: | ||
return jsonify({"result": "error", "message": "Something went wrong. Report that error please"}) | ||
|
||
return jsonify({"result": "not found", "message": "Not found"}) | ||
@app.get("/favicon.ico", include_in_schema=False) | ||
async def get_favicon(): | ||
""" | ||
Get favicon | ||
:return: Icon file | ||
""" | ||
|
||
return FileResponse("./static/icon.ico") | ||
|
||
@app.route("/<int:cid>/") | ||
def viewer_page(cid): | ||
user = requests.get(f"{VATSIM_API_URL}members/{cid}").json() | ||
|
||
@app.get("/{cid}/") | ||
async def get_viewer_page(request: Request, cid: str = None): | ||
""" | ||
Viewer page | ||
:param request: FastAPI request | ||
:param cid: User CID | ||
:return: HTML template | ||
""" | ||
try: | ||
user = requests.get(f"{VATSIM_API_URL}members/{cid}").json() | ||
except requests.exceptions.JSONDecodeError: | ||
raise HTTPException(404) | ||
|
||
if "detail" in user and user["detail"] == "Not Found": | ||
return abort(404) | ||
raise HTTPException(404) | ||
|
||
return templates.TemplateResponse( | ||
name="viewer.html", | ||
context={ | ||
"user": user, | ||
"request": request, | ||
} | ||
) | ||
|
||
|
||
@app.exception_handler(404) | ||
async def error_handler(request: Request, _): | ||
""" | ||
Handler to the 404 HTTP error | ||
:param request: FastAPI request | ||
:return: HTML template | ||
""" | ||
|
||
return templates.TemplateResponse( | ||
name="4O4.html", | ||
context={ | ||
"request": request, | ||
} | ||
) | ||
|
||
|
||
@app.get("/api/request") | ||
def request_handler(request_string: str): | ||
""" | ||
Wrapper to the VATSIM API functions | ||
:param request_string: String that will fetched from VATSIM API | ||
""" | ||
|
||
if request_string: | ||
try: | ||
return requests.get(f"{VATSIM_API_URL}{request_string}").json() | ||
except requests.exceptions.JSONDecodeError: | ||
return {"result": "error", "message": "Something went wrong. Report that error please"} | ||
|
||
return render_template("viewer.html", user=user) | ||
return {"result": "not found", "message": "Not found"} | ||
|
||
|
||
if __name__ == "__main__": | ||
app.run(host="0.0.0.0", port=80) | ||
uvicorn.run(app=app, host="0.0.0.0", port=80) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
Flask==2.2.5 | ||
fastapi==0.110.2 | ||
requests==2.31.0 | ||
uvicorn==0.29.0 | ||
flake8==5.0.4 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters