Template
fix: Updated app to use ArcState
This commit is contained in:
1 parent
dd22946dad
commit
dc47f52e2e
6 files changed
+27
-12
No files matched your search
+2
-2
@@ -1,7 +1,7 @@
|
|||||||
use axum::{Router, extract::OriginalUri, routing::get};
|
use axum::{Router, extract::OriginalUri, routing::get};
|
||||||
use tower_http::services::ServeDir;
|
use tower_http::services::ServeDir;
|
||||||
|
|
||||||
use crate::models::config::Config;
|
use crate::models::state::AppState;
|
||||||
use crate::{error::AppError, routes};
|
use crate::{error::AppError, routes};
|
||||||
|
|
||||||
async fn not_found(OriginalUri(uri): OriginalUri) -> AppError {
|
async fn not_found(OriginalUri(uri): OriginalUri) -> AppError {
|
||||||
@@ -10,7 +10,7 @@ async fn not_found(OriginalUri(uri): OriginalUri) -> AppError {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn app() -> Router<Config> {
|
pub fn app() -> Router<AppState> {
|
||||||
let static_files = ServeDir::new("public").not_found_service(get(not_found));
|
let static_files = ServeDir::new("public").not_found_service(get(not_found));
|
||||||
|
|
||||||
Router::new()
|
Router::new()
|
||||||
|
|||||||
+9
-4
@@ -1,4 +1,6 @@
|
|||||||
use crate::{models::config::Config, util::config::get_app_config};
|
use std::sync::Arc;
|
||||||
|
|
||||||
|
use crate::{models::state::SharedState, util::config::get_app_config};
|
||||||
|
|
||||||
mod app;
|
mod app;
|
||||||
mod error;
|
mod error;
|
||||||
@@ -8,11 +10,14 @@ mod util;
|
|||||||
|
|
||||||
#[tokio::main]
|
#[tokio::main]
|
||||||
async fn main() {
|
async fn main() {
|
||||||
// Read once
|
let config = get_app_config().expect("Unable to read app config");
|
||||||
let config: Config = get_app_config().expect("Unable to read app config");
|
|
||||||
|
let state = Arc::new(SharedState {
|
||||||
|
config: config.clone(),
|
||||||
|
});
|
||||||
|
|
||||||
// Build app with shared state
|
// Build app with shared state
|
||||||
let app = app::app().with_state(config.clone());
|
let app = app::app().with_state(state);
|
||||||
|
|
||||||
let app_ip = format!("0.0.0.0:{}", config.port);
|
let app_ip = format!("0.0.0.0:{}", config.port);
|
||||||
let listener = tokio::net::TcpListener::bind(&app_ip).await.unwrap();
|
let listener = tokio::net::TcpListener::bind(&app_ip).await.unwrap();
|
||||||
|
|||||||
@@ -1 +1,2 @@
|
|||||||
pub mod config;
|
pub mod config;
|
||||||
|
pub mod state;
|
||||||
@@ -0,0 +1,9 @@
|
|||||||
|
use std::sync::Arc;
|
||||||
|
|
||||||
|
use crate::models::config::Config;
|
||||||
|
|
||||||
|
pub struct SharedState {
|
||||||
|
pub config: Config,
|
||||||
|
}
|
||||||
|
|
||||||
|
pub type AppState = Arc<SharedState>;
|
||||||
+2
-2
@@ -1,8 +1,8 @@
|
|||||||
use crate::models::config::Config;
|
use crate::models::state::AppState;
|
||||||
use axum::Router;
|
use axum::Router;
|
||||||
|
|
||||||
mod root;
|
mod root;
|
||||||
|
|
||||||
pub fn router() -> Router<Config> {
|
pub fn router() -> Router<AppState> {
|
||||||
Router::new().merge(root::router())
|
Router::new().merge(root::router())
|
||||||
}
|
}
|
||||||
+4
-4
@@ -3,7 +3,7 @@ use axum::{Router, extract::State, response::Html, routing::get};
|
|||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
error::AppError,
|
error::AppError,
|
||||||
models::config::{Config, SiteMetadata},
|
models::{config::SiteMetadata, state::AppState},
|
||||||
};
|
};
|
||||||
|
|
||||||
#[derive(Template)]
|
#[derive(Template)]
|
||||||
@@ -13,9 +13,9 @@ struct HomePage {
|
|||||||
title: String,
|
title: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn home(State(config): State<Config>) -> Result<Html<String>, AppError> {
|
async fn home(State(state): State<AppState>) -> Result<Html<String>, AppError> {
|
||||||
let template = HomePage {
|
let template = HomePage {
|
||||||
metadata: config.metadata.clone(),
|
metadata: state.config.metadata.clone(),
|
||||||
title: String::from("Home"),
|
title: String::from("Home"),
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -25,6 +25,6 @@ async fn home(State(config): State<Config>) -> Result<Html<String>, AppError> {
|
|||||||
.map_err(|_| AppError::internal())
|
.map_err(|_| AppError::internal())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn router() -> Router<Config> {
|
pub fn router() -> Router<AppState> {
|
||||||
Router::new().route("/", get(home))
|
Router::new().route("/", get(home))
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user