Hello
commit
dbe33ac1f3
|
@ -0,0 +1,2 @@
|
|||
*.db
|
||||
target/
|
File diff suppressed because it is too large
Load Diff
|
@ -0,0 +1,10 @@
|
|||
[package]
|
||||
name = "plane-serve"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
[dependencies]
|
||||
axum = "0.8.1"
|
||||
serde = { version = "1.0.217", features = ["derive"] }
|
||||
sqlx = { version = "0.8.3", features = ["runtime-tokio", "sqlite"] }
|
||||
tokio = { version = "1.43.0", features = ["full"] }
|
|
@ -0,0 +1,107 @@
|
|||
use axum::{
|
||||
extract::State,
|
||||
http::{HeaderMap, StatusCode},
|
||||
routing::{get, post},
|
||||
Json, Router,
|
||||
};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use sqlx::{Pool, Sqlite};
|
||||
|
||||
#[derive(Serialize, Debug, Deserialize, Clone)]
|
||||
struct Score {
|
||||
score: f64,
|
||||
nickname: String,
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
struct AppState {
|
||||
pub pool: Pool<Sqlite>,
|
||||
}
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() {
|
||||
let pool = sqlx::sqlite::SqlitePool::connect("sqlite:db.db?mode=rwc")
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
init(&pool).await;
|
||||
let app_state = AppState { pool };
|
||||
|
||||
let app = Router::new()
|
||||
.route("/", get(get_scores))
|
||||
.route("/", post(post_score))
|
||||
.with_state(app_state);
|
||||
|
||||
let listener = tokio::net::TcpListener::bind("0.0.0.0:3000").await.unwrap();
|
||||
axum::serve(listener, app).await.unwrap();
|
||||
}
|
||||
|
||||
async fn post_score(
|
||||
State(state): State<AppState>,
|
||||
headers: HeaderMap,
|
||||
Json(payload): Json<Score>,
|
||||
) -> (StatusCode, Json<Score>) {
|
||||
if headers.get("Referer").unwrap() != "https://games.home.pedr0.net/games/plane/" {
|
||||
return (
|
||||
StatusCode::IM_A_TEAPOT,
|
||||
Json(Score {
|
||||
score: 0f64,
|
||||
nickname: "kiddie".to_owned(),
|
||||
}),
|
||||
);
|
||||
}
|
||||
|
||||
let connection = &state.pool;
|
||||
let score = insert(connection, &payload.nickname, &payload.score).await;
|
||||
(StatusCode::CREATED, Json(score))
|
||||
}
|
||||
async fn get_scores(State(state): State<AppState>) -> (StatusCode, Json<Vec<Score>>) {
|
||||
let connection = &state.pool;
|
||||
let scores = select(connection).await;
|
||||
|
||||
(StatusCode::OK, Json(scores))
|
||||
}
|
||||
|
||||
async fn init(connection: &Pool<Sqlite>) {
|
||||
let query = r#"
|
||||
CREATE TABLE IF NOT EXISTS SCORES (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
score REAL,
|
||||
nickname TEXT not null,
|
||||
ts TIMESTAMP not null
|
||||
)
|
||||
"#;
|
||||
|
||||
sqlx::query(query).execute(connection).await.unwrap();
|
||||
}
|
||||
|
||||
async fn insert(connection: &Pool<Sqlite>, nickname: &str, score: &f64) -> Score {
|
||||
let query = "INSERT INTO SCORES (score, nickname, ts) VALUES (?, ?, datetime())";
|
||||
|
||||
sqlx::query(query)
|
||||
.bind(score)
|
||||
.bind(nickname)
|
||||
.execute(connection)
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
Score {
|
||||
score: *score,
|
||||
nickname: nickname.to_string(),
|
||||
}
|
||||
}
|
||||
|
||||
async fn select(connection: &Pool<Sqlite>) -> Vec<Score> {
|
||||
let query = "SELECT score, nickname FROM SCORES ORDER BY score desc LIMIT 10";
|
||||
|
||||
let mut scores = vec![];
|
||||
let scrs: Vec<(f64, String)> = sqlx::query_as(query).fetch_all(connection).await.unwrap();
|
||||
for s in scrs {
|
||||
scores.push(Score {
|
||||
score: s.0,
|
||||
nickname: s.1,
|
||||
});
|
||||
}
|
||||
|
||||
scores
|
||||
}
|
Loading…
Reference in New Issue