mirror of
https://codeberg.org/gothub/RandomInstance
synced 2024-12-06 19:16:49 +01:00
6c88de4aa4
Signed-off-by: Odyssey <hi@odyssey346.dev>
45 lines
1.3 KiB
Python
45 lines
1.3 KiB
Python
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"]) |