-
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
ns_lukyanenko
committed
Jan 22, 2024
1 parent
8e46120
commit 1a65fc4
Showing
6 changed files
with
88 additions
and
74 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
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 @@ | ||
pub mod router; |
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,50 @@ | ||
use std::sync::Arc; | ||
|
||
use axum::{ | ||
body::Body, | ||
http::{ | ||
header::{ACCEPT, AUTHORIZATION, CONTENT_TYPE}, | ||
HeaderValue, Method, | ||
}, | ||
middleware, | ||
routing::{delete, get, patch, post, put}, | ||
Router, | ||
}; | ||
use tower_http::{cors::CorsLayer, trace::TraceLayer}; | ||
|
||
use crate::{ | ||
controllers::{ | ||
auth::{login, register}, | ||
product::{create_product, delete_product, get_product_list, update_product}, | ||
}, | ||
middlewares, AppState, | ||
}; | ||
|
||
pub fn create_router(app_state: Arc<AppState>) -> Router { | ||
let user_routes = Router::new() | ||
.route("/register", post(register)) | ||
.route("/login", post(login)); | ||
|
||
let products_routes = Router::new() | ||
.route("/create", put(create_product)) | ||
.route("/get_list", get(get_product_list)) | ||
.route("/update", patch(update_product)) | ||
.route("/delete", delete(delete_product)) | ||
.route_layer(middleware::from_fn_with_state( | ||
app_state.clone(), | ||
middlewares::auth::auth::<Body>, | ||
)); | ||
|
||
let cors = CorsLayer::new() | ||
.allow_origin("http://localhost:8080".parse::<HeaderValue>().unwrap()) | ||
.allow_methods([Method::GET, Method::POST, Method::PATCH, Method::DELETE]) | ||
.allow_credentials(true) | ||
.allow_headers([AUTHORIZATION, ACCEPT, CONTENT_TYPE]); | ||
|
||
Router::new() | ||
.nest("/users", user_routes) | ||
.nest("/products", products_routes) | ||
.layer(cors) | ||
.layer(TraceLayer::new_for_http()) | ||
.with_state(app_state) | ||
} |
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,25 @@ | ||
use tokio::signal; | ||
|
||
pub async fn shutdown_signal() { | ||
let ctrl_c = async { | ||
signal::ctrl_c() | ||
.await | ||
.expect("failed to install Ctrl+C handler"); | ||
}; | ||
|
||
#[cfg(unix)] | ||
let terminate = async { | ||
signal::unix::signal(signal::unix::SignalKind::terminate()) | ||
.expect("failed to install signal handler") | ||
.recv() | ||
.await; | ||
}; | ||
|
||
#[cfg(not(unix))] | ||
let terminate = std::future::pending::<()>(); | ||
|
||
tokio::select! { | ||
_ = ctrl_c => {}, | ||
_ = terminate => {}, | ||
} | ||
} |
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 +1,2 @@ | ||
pub mod connection; | ||
pub mod graceful_shutdown; |