feat: provide connection pool to routes
This commit is contained in:
parent
f4deaceb27
commit
68e825c942
5 changed files with 43 additions and 20 deletions
9
Cargo.lock
generated
9
Cargo.lock
generated
|
|
@ -249,7 +249,10 @@ checksum = "7f2c685bad3eb3d45a01354cedb7d5faa66194d1d58ba6e267a8de788f79db38"
|
|||
dependencies = [
|
||||
"android-tzdata",
|
||||
"iana-time-zone",
|
||||
"js-sys",
|
||||
"num-traits",
|
||||
"serde",
|
||||
"wasm-bindgen",
|
||||
"windows-targets 0.48.1",
|
||||
]
|
||||
|
||||
|
|
@ -986,6 +989,7 @@ name = "learn_axum"
|
|||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"axum",
|
||||
"chrono",
|
||||
"config",
|
||||
"hyper 1.1.0",
|
||||
"reqwest",
|
||||
|
|
@ -993,6 +997,7 @@ dependencies = [
|
|||
"serde_json",
|
||||
"sqlx",
|
||||
"tokio",
|
||||
"uuid",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
|
|
@ -2365,6 +2370,10 @@ name = "uuid"
|
|||
version = "1.6.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "5e395fcf16a7a3d8127ec99782007af141946b4795001f876d54fb0d55978560"
|
||||
dependencies = [
|
||||
"getrandom",
|
||||
"rand",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "vcpkg"
|
||||
|
|
|
|||
15
Cargo.toml
15
Cargo.toml
|
|
@ -28,18 +28,19 @@ config = "0.13"
|
|||
# lazy-regex = "3"
|
||||
# async-trait = "0.1"
|
||||
# strum_macros = "0.25"
|
||||
# uuid = { version = "1", features = ["v4", "fast-rng"] }
|
||||
uuid = { version = "1", features = ["v4", "fast-rng"] }
|
||||
chrono = { version = "0.4", features = ["serde"] }
|
||||
|
||||
[dependencies.sqlx]
|
||||
version = "0.7"
|
||||
default-features = false
|
||||
features = [
|
||||
"runtime-tokio-rustls",
|
||||
"macros",
|
||||
"postgres",
|
||||
"uuid",
|
||||
"chrono",
|
||||
"migrate"
|
||||
"runtime-tokio-rustls",
|
||||
"macros",
|
||||
"postgres",
|
||||
"uuid",
|
||||
"chrono",
|
||||
"migrate",
|
||||
]
|
||||
|
||||
[dev-dependencies]
|
||||
|
|
|
|||
|
|
@ -1,8 +1,11 @@
|
|||
use axum::Form;
|
||||
use axum::http::StatusCode;
|
||||
use axum::extract::State;
|
||||
use axum::routing::post;
|
||||
use axum::Form;
|
||||
use axum::Router;
|
||||
use chrono::Utc;
|
||||
use serde::Deserialize;
|
||||
use sqlx::PgPool;
|
||||
use uuid::Uuid;
|
||||
|
||||
#[derive(Debug, Deserialize)]
|
||||
struct FormData {
|
||||
|
|
@ -10,14 +13,25 @@ struct FormData {
|
|||
name: String,
|
||||
}
|
||||
|
||||
async fn subscribe(Form(params): Form<FormData>) {
|
||||
// println!("Params are: {params:?}");
|
||||
// if params.name.is_empty() || params.email.is_empty() {
|
||||
// return StatusCode::NOT_ACCEPTABLE;
|
||||
// }
|
||||
// StatusCode::OK
|
||||
async fn subscribe(State(pool): State<PgPool>, Form(form): Form<FormData>) {
|
||||
let _ = sqlx::query!(
|
||||
r#"
|
||||
INSERT INTO subscriptions (id, email, name, subscribed_at)
|
||||
VALUES ($1, $2, $3, $4)
|
||||
"#,
|
||||
Uuid::new_v4(),
|
||||
form.email,
|
||||
form.name,
|
||||
Utc::now()
|
||||
)
|
||||
// We use `get_ref` to get an immutable reference to the `PgConnection`
|
||||
// wrapped by `web::Data`.
|
||||
.execute(&pool)
|
||||
.await;
|
||||
}
|
||||
|
||||
pub fn routes_subscriptions() -> Router {
|
||||
Router::new().route("/subscriptions", post(subscribe))
|
||||
pub fn routes_subscriptions(pool: PgPool) -> Router {
|
||||
Router::new()
|
||||
.route("/subscriptions", post(subscribe))
|
||||
.with_state(pool)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -7,9 +7,8 @@ use tokio::net::TcpListener;
|
|||
/// API routing
|
||||
pub fn app(connection: PgPool) -> Router {
|
||||
Router::new()
|
||||
.with_state(connection)
|
||||
.merge(crate::routes::routes_health_check())
|
||||
.merge(crate::routes::routes_subscriptions())
|
||||
.merge(crate::routes::routes_subscriptions(connection))
|
||||
}
|
||||
|
||||
/// Start the server
|
||||
|
|
|
|||
|
|
@ -1,7 +1,7 @@
|
|||
use std::time::Duration;
|
||||
|
||||
use learn_axum::configuration::get_configuration;
|
||||
use sqlx::{Connection, PgConnection, postgres::PgPoolOptions};
|
||||
use sqlx::{postgres::PgPoolOptions, Connection, PgConnection};
|
||||
use tokio::net::TcpListener;
|
||||
|
||||
struct TestApp {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue