it seems to work, tentatively

main
znpra 1 year ago
parent 753a01a750
commit e321b79b5c

1
.gitignore vendored

@ -1 +1,2 @@
/target /target
ttc_test*

@ -0,0 +1,20 @@
{
"db_name": "SQLite",
"query": "SELECT pronouns \n FROM pronouns \n WHERE username == ?",
"describe": {
"columns": [
{
"name": "pronouns",
"ordinal": 0,
"type_info": "Text"
}
],
"parameters": {
"Right": 1
},
"nullable": [
false
]
},
"hash": "f17495d023bac377869af665478b21bb962599df4bf7f59dd8336dc538f5a8fb"
}

13
Cargo.lock generated

@ -69,6 +69,7 @@ checksum = "f8175979259124331c1d7bf6586ee7e0da434155e4b2d48ec2c8386281d8df39"
dependencies = [ dependencies = [
"async-trait", "async-trait",
"axum-core", "axum-core",
"axum-macros",
"bitflags 1.3.2", "bitflags 1.3.2",
"bytes", "bytes",
"futures-util", "futures-util",
@ -110,6 +111,18 @@ dependencies = [
"tower-service", "tower-service",
] ]
[[package]]
name = "axum-macros"
version = "0.3.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cdca6a10ecad987bda04e95606ef85a5417dcaac1a78455242d72e031e2b6b62"
dependencies = [
"heck",
"proc-macro2",
"quote",
"syn 2.0.23",
]
[[package]] [[package]]
name = "backtrace" name = "backtrace"
version = "0.3.68" version = "0.3.68"

@ -6,7 +6,7 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies] [dependencies]
axum = "0.6.18" axum ={ version = "0.6.18", features = ["macros"] }
tokio = { version = "1.29.1", features = ["full"] } tokio = { version = "1.29.1", features = ["full"] }
rand = { version = "0.8.5", features = ["std"] } rand = { version = "0.8.5", features = ["std"] }
sqlx = { version = "0.7.1", features = ["runtime-tokio", "sqlite"] } sqlx = { version = "0.7.1", features = ["runtime-tokio", "sqlite"] }

@ -0,0 +1,5 @@
// generated by `sqlx migrate build-script`
fn main() {
// trigger recompilation when a new migration is added
println!("cargo:rerun-if-changed=migrations");
}

@ -0,0 +1,6 @@
CREATE TABLE pronouns (
id INTEGER PRIMARY KEY AUTOINCREMENT,
username TEXT NOT NULL,
pronouns TEXT NOT NULL
);

@ -1,36 +1,51 @@
use std::{sync::Arc, env}; use std::env;
use axum::{ use axum::{
routing::get, routing::get,
Router, Router,
extract::Path, extract::{Path, State}, http::StatusCode,
}; };
use rand::{ thread_rng, seq::IteratorRandom };
use sqlx::{SqlitePool, sqlite::SqlitePoolOptions}; use sqlx::{SqlitePool, sqlite::SqlitePoolOptions};
struct AppState {
pub db_pool: SqlitePool,
}
async fn user_pronouns( async fn user_pronouns(
State(pool): State<SqlitePool>,
Path(user): Path<String>, Path(user): Path<String>,
) -> String { ) -> Result<String, StatusCode> {
user let res = sqlx::query!("SELECT pronouns
FROM pronouns
WHERE username == ?",
user)
.fetch_one(&pool)
.await
.map_err(|err| match err {
sqlx::Error::RowNotFound => StatusCode::NOT_FOUND,
_ => StatusCode::INTERNAL_SERVER_ERROR
})?;
res.pronouns
.split(';')
.choose(&mut thread_rng())
.map(ToOwned::to_owned)
.ok_or(StatusCode::NOT_FOUND)
} }
#[tokio::main] #[tokio::main]
async fn main() { async fn main() {
let pool = SqlitePoolOptions::new()
.max_connections(5)
.connect(env::var("TTC_DATABASE_URL")
.unwrap_or("sqlite:ttc.db".into())
.as_ref())
.await
.unwrap();
sqlx::migrate!("./migrations").run(&pool).await.unwrap();
let app = Router::new() let app = Router::new()
.route("/api/ttc/hello", get(|| async { "hello world!" })) .route("/api/ttc/hello", get(|| async { "hello world!" }))
.route("/api/ttc/pronouns/:user", get(user_pronouns)) .route("/api/ttc/pronouns/:user", get(user_pronouns))
.with_state(Arc::new(AppState { .with_state(pool);
db_pool: SqlitePoolOptions::new()
.max_connections(5)
.connect(env::var("TTC_DATABASE_URL")
.unwrap_or("sqlite:ttc.db".into())
.as_ref())
.await
.unwrap()
}));
axum::Server::bind(&"0.0.0.0:3000".parse().unwrap()) axum::Server::bind(&"0.0.0.0:3000".parse().unwrap())
.serve(app.into_make_service()) .serve(app.into_make_service())

Loading…
Cancel
Save