This repository has been archived by the owner on Jun 18, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0522847
commit af13334
Showing
6 changed files
with
51 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters