Switch to FastAPI

Signed-off-by: Odyssey <hi@odyssey346.dev>
This commit is contained in:
Odyssey
2023-04-03 22:20:12 +02:00
parent 2e96148246
commit 6c88de4aa4
5 changed files with 54 additions and 49 deletions
+1
View File
@@ -0,0 +1 @@
__pycache__/
+6 -11
View File
@@ -1,16 +1,11 @@
# pull down the pode image FROM python:3.9
FROM badgerati/pode:latest-alpine
# or use the following for GitHub WORKDIR /code
# FROM docker.pkg.github.com/badgerati/pode/pode:latest-alpine
# copy over the local files to the container COPY ./requirements.txt /code/requirements.txt
COPY . /usr/src/app/
# expose the port RUN pip install --no-cache-dir --upgrade -r /code/requirements.txt
EXPOSE 8080
ENV DOCKER=true COPY ./app /code/app
# run the server CMD ["uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "8080"]
CMD [ "pwsh", "-c", "cd /usr/src/app; ./server.ps1" ]
+45
View File
@@ -0,0 +1,45 @@
from typing import Union
from fastapi import FastAPI
from fastapi.responses import RedirectResponse
import requests
import random
app = FastAPI()
@app.get("/")
def read_root():
return {"Response": "Hello World"}
@app.get("/redirect")
def redirect():
# get GotHub instances json from codeberg
r = requests.get("https://codeberg.org/gothub/gothub-instances/raw/branch/master/instances.json")
# get random instance
instance = random.choice(r.json())
# redirect to random instance
return RedirectResponse(url=instance["link"])
@app.get("/redirect/dev")
def redirect_dev():
# get GotHub instances json from codeberg
r = requests.get("https://codeberg.org/gothub/gothub-instances/raw/branch/master/instances.json")
# get random instance, branch = dev
instance = random.choice([i for i in r.json() if i["branch"] == "dev"])
# redirect to random instance
return RedirectResponse(url=instance["link"])
@app.get("/redirect/master")
def redirect_master():
# get GotHub instances json from codeberg
r = requests.get("https://codeberg.org/gothub/gothub-instances/raw/branch/master/instances.json")
# get random instance, branch = master
instance = random.choice([i for i in r.json() if i["branch" == "master"]])
# redirect to random instance
return RedirectResponse(url=instance["link"])
+2
View File
@@ -0,0 +1,2 @@
fastapi==0.95.0
requests==2.28.2
-38
View File
@@ -1,38 +0,0 @@
Start-PodeServer {
# get docker environment variable
$docker = $env:DOCKER
if ($docker) {
Add-PodeEndpoint -Address * -Port 8080 -Protocol Http
} else {
Add-PodeEndpoint -Address localhost -Port 8080 -Protocol Http
}
Add-PodeRoute -Method Get -Path '/' -ScriptBlock {
Write-PodeJsonResponse -Value @{ Response = 'Hello World' }
}
Add-PodeRoute -Method Get -Path '/random' -ScriptBlock {
$instances = Invoke-WebRequest https://codeberg.org/gothub/gothub-instances/raw/branch/master/instances.json | ConvertFrom-Json
$instance = $instances | Get-Random
Move-PodeResponseUrl -Url $instance.link
}
Add-PodeRoute -Method Get -Path '/random/master' -ScriptBlock {
$instances = Invoke-WebRequest https://codeberg.org/gothub/gothub-instances/raw/branch/master/instances.json | ConvertFrom-Json
$instance = $instances | Where-Object { $_.branch -eq 'master' } | Get-Random
Move-PodeResponseUrl -Url $instance.link
}
Add-PodeRoute -Method Get -Path '/random/dev' -ScriptBlock {
$instances = Invoke-WebRequest https://codeberg.org/gothub/gothub-instances/raw/branch/master/instances.json | ConvertFrom-Json
$instance = $instances | Where-Object { $_.branch -eq 'dev' } | Get-Random
Move-PodeResponseUrl -Url $instance.link
}
}