initial commit, figuring out axum & sqlx

main
aprzn 1 year ago
commit 753a01a750

1
.gitignore vendored

@ -0,0 +1 @@
/target

1770
Cargo.lock generated

File diff suppressed because it is too large Load Diff

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

@ -0,0 +1,39 @@
use std::{sync::Arc, env};
use axum::{
routing::get,
Router,
extract::Path,
};
use sqlx::{SqlitePool, sqlite::SqlitePoolOptions};
struct AppState {
pub db_pool: SqlitePool,
}
async fn user_pronouns(
Path(user): Path<String>,
) -> String {
user
}
#[tokio::main]
async fn main() {
let app = Router::new()
.route("/api/ttc/hello", get(|| async { "hello world!" }))
.route("/api/ttc/pronouns/:user", get(user_pronouns))
.with_state(Arc::new(AppState {
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())
.serve(app.into_make_service())
.await
.unwrap();
}
Loading…
Cancel
Save