From af13334071ab327d5109c844c0e002c36c438c75 Mon Sep 17 00:00:00 2001 From: Adrian Date: Sun, 20 Aug 2023 16:51:16 +0300 Subject: [PATCH] Move app to its own module --- Tests/shared/test_app.rs | 2 +- src/app.rs | 45 ++++++++++++++++++++++++++++++++++++++++ src/lib.rs | 45 +--------------------------------------- src/main.rs | 2 +- src/routes/redirect.rs | 2 +- src/routes/shorten.rs | 4 ++-- 6 files changed, 51 insertions(+), 49 deletions(-) create mode 100644 src/app.rs diff --git a/Tests/shared/test_app.rs b/Tests/shared/test_app.rs index ae22047..d602ed7 100644 --- a/Tests/shared/test_app.rs +++ b/Tests/shared/test_app.rs @@ -1,4 +1,4 @@ -use briefly::{configuration::get_configuration, run}; +use briefly::{app::run, configuration::get_configuration}; use std::net::TcpListener; use std::sync::Arc; diff --git a/src/app.rs b/src/app.rs new file mode 100644 index 0000000..d886a73 --- /dev/null +++ b/src/app.rs @@ -0,0 +1,45 @@ +use crate::{ + request_tracing::RequestSpan, + routes::{health_check, redirect, shorten}, +}; +use axum::routing::{get, post}; +use axum::Router; +use sqlx::{postgres::PgPoolOptions, Pool, Postgres}; +use std::net::TcpListener; +use std::sync::Arc; +use tower_http::trace::TraceLayer; + +pub struct AppState { + pub db: Pool, +} + +pub async fn run(listener: TcpListener, db_connection: &str) { + let pool = connect_to_database(db_connection) + .await + .expect("Failed to conect to the database"); + let app_state = Arc::new(AppState { db: pool.clone() }); + + let app = app(app_state); + axum::Server::from_tcp(listener) + .unwrap() + .serve(app.into_make_service()) + .await + .unwrap() +} + +pub fn app(app_state: Arc) -> Router { + Router::new() + .route("/health_check", get(health_check)) + .route("/shorten", post(shorten)) + .route("/:extension", get(redirect)) + .layer(TraceLayer::new_for_http().make_span_with(RequestSpan)) + .with_state(app_state) +} + +async fn connect_to_database(connection_string: &str) -> Option> { + PgPoolOptions::new() + .max_connections(10) + .connect(connection_string) + .await + .ok() +} diff --git a/src/lib.rs b/src/lib.rs index e0166cb..efd0da9 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,3 +1,4 @@ +pub mod app; pub mod app_error; pub mod configuration; pub mod key_generator; @@ -5,47 +6,3 @@ pub mod model; pub mod request_tracing; pub mod routes; pub mod schema; - -use axum::routing::{get, post}; -use axum::Router; -use request_tracing::RequestSpan; -use routes::{health_check, redirect, shorten}; -use sqlx::{postgres::PgPoolOptions, Pool, Postgres}; -use std::net::TcpListener; -use std::sync::Arc; -use tower_http::trace::TraceLayer; - -pub async fn run(listener: TcpListener, db_connection: &str) { - let pool = connect_to_database(db_connection) - .await - .expect("Failed to conect to the database"); - let app_state = Arc::new(AppState { db: pool.clone() }); - - let app = app(app_state); - axum::Server::from_tcp(listener) - .unwrap() - .serve(app.into_make_service()) - .await - .unwrap() -} - -pub struct AppState { - db: Pool, -} - -pub fn app(app_state: Arc) -> Router { - Router::new() - .route("/health_check", get(health_check)) - .route("/shorten", post(shorten)) - .route("/:extension", get(redirect)) - .layer(TraceLayer::new_for_http().make_span_with(RequestSpan)) - .with_state(app_state) -} - -async fn connect_to_database(connection_string: &str) -> Option> { - PgPoolOptions::new() - .max_connections(10) - .connect(connection_string) - .await - .ok() -} diff --git a/src/main.rs b/src/main.rs index 3f7d6df..9169d03 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,4 +1,4 @@ -use briefly::{configuration::get_configuration, run}; +use briefly::{app::run, configuration::get_configuration}; use std::net::TcpListener; use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt}; diff --git a/src/routes/redirect.rs b/src/routes/redirect.rs index 64d9838..47d3c60 100644 --- a/src/routes/redirect.rs +++ b/src/routes/redirect.rs @@ -1,4 +1,4 @@ -use crate::{app_error::AppError, model::UrlRequestModel, AppState}; +use crate::{app::AppState, app_error::AppError, model::UrlRequestModel}; use axum::{ extract::{Path, State}, response::Redirect, diff --git a/src/routes/shorten.rs b/src/routes/shorten.rs index 5446d9c..1f94632 100644 --- a/src/routes/shorten.rs +++ b/src/routes/shorten.rs @@ -1,6 +1,6 @@ use crate::{ - app_error::AppError, key_generator::generate, model::UrlRequestModel, - schema::CreateShortUrlSchema, AppState, + app::AppState, app_error::AppError, key_generator::generate, model::UrlRequestModel, + schema::CreateShortUrlSchema, }; use anyhow::anyhow; use axum::{extract::State, Json};