diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ba0430d --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +__pycache__/ \ No newline at end of file diff --git a/Dockerfile b/Dockerfile index d4b4d32..1719c2b 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,16 +1,11 @@ -# pull down the pode image -FROM badgerati/pode:latest-alpine +FROM python:3.9 -# or use the following for GitHub -# FROM docker.pkg.github.com/badgerati/pode/pode:latest-alpine +WORKDIR /code -# copy over the local files to the container -COPY . /usr/src/app/ +COPY ./requirements.txt /code/requirements.txt -# expose the port -EXPOSE 8080 +RUN pip install --no-cache-dir --upgrade -r /code/requirements.txt -ENV DOCKER=true +COPY ./app /code/app -# run the server -CMD [ "pwsh", "-c", "cd /usr/src/app; ./server.ps1" ] \ No newline at end of file +CMD ["uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "8080"] \ No newline at end of file diff --git a/main.py b/main.py new file mode 100644 index 0000000..a161c2d --- /dev/null +++ b/main.py @@ -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"]) \ No newline at end of file diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..95b163c --- /dev/null +++ b/requirements.txt @@ -0,0 +1,2 @@ +fastapi==0.95.0 +requests==2.28.2 diff --git a/server.ps1 b/server.ps1 deleted file mode 100644 index 599bfbf..0000000 --- a/server.ps1 +++ /dev/null @@ -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 - } -} \ No newline at end of file