1
0
mirror of https://gitlab.com/2-chainz/2chainz.git synced 2025-06-16 01:46:39 +00:00

get everything working together kinda

This commit is contained in:
Anson 2025-05-23 13:53:40 -06:00
parent 5687871b86
commit a683fceb10
4 changed files with 43 additions and 32 deletions

View File

@ -3,30 +3,19 @@ name = "2chainz"
version = "0.1.0" version = "0.1.0"
description = "Add your description here" description = "Add your description here"
readme = "README.md" readme = "README.md"
authors = [ authors = [{ name = "Anson", email = "anson@ansonbiggs.com" }]
{ name = "Anson", email = "anson@ansonbiggs.com" }
]
requires-python = ">=3.13" requires-python = ">=3.13"
dependencies = [ dependencies = ["fastapi[standard]>=0.115.12"]
"fastapi[standard]>=0.115.12",
]
[project.scripts] [project.scripts]
two_chainz = "two_chainz:main" two_chainz = "two_chainz:main"
[build-system] [build-system]
requires = ["hatchling"] requires = ["hatchling"]
build-backend = "hatchling.build" build-backend = "hatchling.build"
[tool.hatch.build.targets.wheel] [tool.hatch.build.targets.wheel]
packages = ["src/two_chainz"] packages = ["src/two_chainz"]
[dependency-groups] [dependency-groups]
dev = [ dev = ["httpx>=0.28.1", "pytest>=8.3.5", "ruff>=0.11.11"]
"httpx>=0.28.1",
"pytest>=8.3.5",
"ruff>=0.11.11",
]

View File

@ -8,8 +8,8 @@ from two_chainz import app
client = TestClient(app) client = TestClient(app)
class TestApi:
class TestApi:
def test_api_endpoint(self): def test_api_endpoint(self):
response = client.get("/api/") response = client.get("/api/")
assert response.status_code == 200 assert response.status_code == 200
@ -24,12 +24,15 @@ class TestApi:
# Validate timestamp format (ISO format) # Validate timestamp format (ISO format)
assert datetime.fromisoformat(data["timestamp"]) assert datetime.fromisoformat(data["timestamp"])
@pytest.mark.parametrize("mocked_time,start_time,expected", [ @pytest.mark.parametrize(
(100, 50, 50), # 100 - 50 = 50 seconds uptime "mocked_time,start_time,expected",
(200, 100, 100), # 200 - 100 = 100 seconds uptime [
]) (100, 50, 50), # 100 - 50 = 50 seconds uptime
(200, 100, 100), # 200 - 100 = 100 seconds uptime
],
)
def test_api_uptime_calculation(self, mocked_time, start_time, expected): def test_api_uptime_calculation(self, mocked_time, start_time, expected):
with patch('time.time', return_value=mocked_time): with patch("time.time", return_value=mocked_time):
with patch('two_chainz.start_time', start_time): with patch("two_chainz.start_time", start_time):
response = client.get("/api/") response = client.get("/api/")
assert response.json()["uptime_seconds"] == expected assert response.json()["uptime_seconds"] == expected

View File

@ -2,13 +2,22 @@ import time
from datetime import datetime from datetime import datetime
from fastapi import FastAPI from fastapi import FastAPI
from fastapi.staticfiles import StaticFiles from fastapi.staticfiles import StaticFiles
import tomllib
from pathlib import Path
import random
app = FastAPI() app = FastAPI()
start_time = time.time() start_time = time.time()
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/") @app.get("/api/")
async def ping(): async def ping():
return { return {
@ -18,5 +27,15 @@ async def ping():
} }
@app.get("/api/quote")
async def quote():
return {"quote": random.choice(data['quotes'])}
@app.get("/api/alias")
async def alias():
return {"alias": random.choice(data['aliases'])}
# Mount static files # Mount static files
app.mount("/", StaticFiles(directory="website", html=True), name="static") app.mount("/", StaticFiles(directory="website", html=True), name="static")

View File

@ -109,8 +109,8 @@
<p> <p>
send a <code>get</code> request to send a <code>get</code> request to
<code <code
><a href="https://chainz-rest.azurewebsites.net/quote" ><a href="https://chainz.ansonbiggs.com/api/quote"
>https://chainz-rest.azurewebsites.net/quote</a >https://chainz.ansonbiggs.com/api/quote</a
></code ></code
> >
</p> </p>
@ -132,8 +132,8 @@
> >
and is subject to change. An example return from and is subject to change. An example return from
<code> <code>
<a href="https://chainz-rest.azurewebsites.net/quote?batch=2" <a href="https://chainz.ansonbiggs.com/api/quote?batch=2"
>https://chainz-rest.azurewebsites.net/quote?batch=2</a >https://chainz.ansonbiggs.com/api/quote?batch=2</a
></code ></code
> >
</p> </p>
@ -160,8 +160,8 @@
<p> <p>
send a <code>get</code> request to send a <code>get</code> request to
<code <code
><a href="https://chainz-rest.azurewebsites.net/alias" ><a href="https://chainz.ansonbiggs.com/api/alias"
>https://chainz-rest.azurewebsites.net/alias</a >https://chainz.ansonbiggs.com/api/alias</a
></code ></code
> >
</p> </p>
@ -275,7 +275,7 @@
getQuote(); getQuote();
function getQuote() { function getQuote() {
fetch("https://chainz-rest.azurewebsites.net/quote", { method: "GET" }) fetch("/api/quote", { method: "GET" })
.then((resp) => resp.json()) .then((resp) => resp.json())
.then(function (data) { .then(function (data) {
document.getElementById("quote").innerHTML = data.quote; document.getElementById("quote").innerHTML = data.quote;
@ -288,7 +288,7 @@
).href = `https://twitter.com/intent/tweet?text=${tweet}`; ).href = `https://twitter.com/intent/tweet?text=${tweet}`;
}); });
fetch("https://chainz-rest.azurewebsites.net/alias", { method: "GET" }) fetch("/api/alias", { method: "GET" })
.then((resp) => resp.json()) .then((resp) => resp.json())
.then(function (data) { .then(function (data) {
document.getElementById("alias").innerHTML = "- " + data.alias; document.getElementById("alias").innerHTML = "- " + data.alias;