Skip to content
This repository has been archived by the owner on Jun 18, 2024. It is now read-only.

Commit

Permalink
Move app to its own module
Browse files Browse the repository at this point in the history
  • Loading branch information
adrianEffe committed Aug 20, 2023
1 parent 0522847 commit af13334
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 49 deletions.
2 changes: 1 addition & 1 deletion Tests/shared/test_app.rs
Original file line number Diff line number Diff line change
@@ -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;

Expand Down
45 changes: 45 additions & 0 deletions src/app.rs
Original file line number Diff line number Diff line change
@@ -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<Postgres>,
}

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<AppState>) -> 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<Pool<Postgres>> {
PgPoolOptions::new()
.max_connections(10)
.connect(connection_string)
.await
.ok()
}
45 changes: 1 addition & 44 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,51 +1,8 @@
pub mod app;
pub mod app_error;
pub mod configuration;
pub mod key_generator;
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<Postgres>,
}

pub fn app(app_state: Arc<AppState>) -> 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<Pool<Postgres>> {
PgPoolOptions::new()
.max_connections(10)
.connect(connection_string)
.await
.ok()
}
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
@@ -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};

Expand Down
2 changes: 1 addition & 1 deletion src/routes/redirect.rs
Original file line number Diff line number Diff line change
@@ -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,
Expand Down
4 changes: 2 additions & 2 deletions src/routes/shorten.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand Down

0 comments on commit af13334

Please sign in to comment.