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 tower_http::services::ServeDir;
|
||||
|
||||
use crate::models::config::Config;
|
||||
use crate::models::state::AppState;
|
||||
use crate::{error::AppError, routes};
|
||||
|
||||
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));
|
||||
|
||||
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 error;
|
||||
@@ -8,11 +10,14 @@ mod util;
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() {
|
||||
// Read once
|
||||
let config: Config = get_app_config().expect("Unable to read app config");
|
||||
let config = get_app_config().expect("Unable to read app config");
|
||||
|
||||
let state = Arc::new(SharedState {
|
||||
config: config.clone(),
|
||||
});
|
||||
|
||||
// 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 listener = tokio::net::TcpListener::bind(&app_ip).await.unwrap();
|
||||
|
||||
@@ -1 +1,2 @@
|
||||
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;
|
||||
|
||||
mod root;
|
||||
|
||||
pub fn router() -> Router<Config> {
|
||||
pub fn router() -> Router<AppState> {
|
||||
Router::new().merge(root::router())
|
||||
}
|
||||
+4
-4
@@ -3,7 +3,7 @@ use axum::{Router, extract::State, response::Html, routing::get};
|
||||
|
||||
use crate::{
|
||||
error::AppError,
|
||||
models::config::{Config, SiteMetadata},
|
||||
models::{config::SiteMetadata, state::AppState},
|
||||
};
|
||||
|
||||
#[derive(Template)]
|
||||
@@ -13,9 +13,9 @@ struct HomePage {
|
||||
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 {
|
||||
metadata: config.metadata.clone(),
|
||||
metadata: state.config.metadata.clone(),
|
||||
title: String::from("Home"),
|
||||
};
|
||||
|
||||
@@ -25,6 +25,6 @@ async fn home(State(config): State<Config>) -> Result<Html<String>, AppError> {
|
||||
.map_err(|_| AppError::internal())
|
||||
}
|
||||
|
||||
pub fn router() -> Router<Config> {
|
||||
pub fn router() -> Router<AppState> {
|
||||
Router::new().route("/", get(home))
|
||||
}
|
||||
Reference in new issue
Block a user