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