mirror of
https://gitlab.com/2-chainz/2chainz.git
synced 2025-06-16 09:56:40 +00:00
119 lines
2.9 KiB
Python
119 lines
2.9 KiB
Python
import random
|
|
import time
|
|
import tomllib
|
|
from datetime import datetime
|
|
from pathlib import Path
|
|
|
|
from fastapi import FastAPI
|
|
from fastapi.middleware.trustedhost import TrustedHostMiddleware
|
|
from fastapi.staticfiles import StaticFiles
|
|
from atproto import Client
|
|
|
|
app = FastAPI(
|
|
title="2 Chainz REST",
|
|
description="A simple API to get 2 Chainz quotes and aliases. See <a href=https://2chainz.ansonbiggs.com>2chainz.ansonbiggs.com</a> for a full explanation.",
|
|
)
|
|
app.add_middleware(
|
|
TrustedHostMiddleware,
|
|
allowed_hosts=["*"],
|
|
)
|
|
|
|
start_time = time.time()
|
|
|
|
def post_to_bluesky():
|
|
|
|
|
|
|
|
def read_data() -> dict[str, str]:
|
|
raw_data = tomllib.loads(Path("data.toml").read_text())
|
|
raw_data["aliases"] = [
|
|
alias["name"] for alias in raw_data["aliases"] for _ in range(alias["weight"])
|
|
]
|
|
|
|
return raw_data
|
|
|
|
|
|
data = read_data()
|
|
|
|
|
|
@app.get(
|
|
"/api/",
|
|
summary="Health Check",
|
|
description="Check API health status and uptime",
|
|
response_description="Health status with timestamp and uptime",
|
|
tags=["Health"],
|
|
responses={
|
|
200: {
|
|
"description": "Successful health check",
|
|
"content": {
|
|
"application/json": {
|
|
"example": {
|
|
"status": "ok",
|
|
"timestamp": "2025-05-24T10:30:00",
|
|
"uptime_seconds": "3600.5",
|
|
}
|
|
}
|
|
},
|
|
}
|
|
},
|
|
)
|
|
async def ping() -> dict[str, str]:
|
|
"""
|
|
Endpoint to check on the health of the API and to help diagnose issues.
|
|
|
|
Returns current status, timestamp, and uptime information.
|
|
"""
|
|
return {
|
|
"status": "ok",
|
|
"timestamp": datetime.now().isoformat(),
|
|
"uptime_seconds": str(round(time.time() - start_time, 2)),
|
|
}
|
|
|
|
|
|
@app.get(
|
|
"/api/quote",
|
|
summary="2 Chainz Quote",
|
|
description="Get a quote from 2 Chainz",
|
|
tags=["Data"],
|
|
responses={
|
|
200: {
|
|
"description": "Successful quote",
|
|
"content": {
|
|
"application/json": {
|
|
"example": {
|
|
"quote": "Wood grain chestnut, titty fuck chest nut",
|
|
}
|
|
}
|
|
},
|
|
}
|
|
},
|
|
)
|
|
async def quote():
|
|
return {"quote": random.choice(data["quotes"])}
|
|
|
|
|
|
@app.get(
|
|
"/api/alias",
|
|
summary="2 Chainz Alias",
|
|
description="Get one of 2 Chainz many aliases",
|
|
tags=["Data"],
|
|
responses={
|
|
200: {
|
|
"description": "Successful alias",
|
|
"content": {
|
|
"application/json": {
|
|
"example": {
|
|
"alias": "2 Chainz",
|
|
}
|
|
}
|
|
},
|
|
}
|
|
},
|
|
)
|
|
async def alias():
|
|
return {"alias": random.choice(data["aliases"])}
|
|
|
|
|
|
# Mount static files
|
|
app.mount("/", StaticFiles(directory="website", html=True), name="static")
|