Adding title to config and utilizing it in layout
This commit is contained in:
parent
effccfdbbc
commit
f4da3f3ab3
@ -28,11 +28,12 @@ postgres=# ALTER DATABASE crawlnicle OWNER TO crawlnicle;
|
||||
directory with the contents:
|
||||
|
||||
```
|
||||
RUST_LOG=crawlnicle=debug,cli=debug,lib=debug,tower_http=debug,sqlx=debug
|
||||
HOST=127.0.0.1
|
||||
PORT=3000
|
||||
DATABASE_URL=postgresql://crawlnicle:<password>@localhost/crawlnicle
|
||||
DATABASE_MAX_CONNECTIONS=5
|
||||
RUST_LOG=crawlnicle=debug,cli=debug,lib=debug,tower_http=debug,sqlx=debug
|
||||
TITLE=crawlnicle
|
||||
```
|
||||
|
||||
4. Install
|
||||
|
@ -6,6 +6,7 @@ pub struct Config {
|
||||
pub database_max_connections: u32,
|
||||
pub host: String,
|
||||
pub port: u16,
|
||||
pub title: String,
|
||||
}
|
||||
|
||||
impl Config {
|
||||
@ -14,12 +15,14 @@ impl Config {
|
||||
let database_max_connections = std::env::var("DATABASE_MAX_CONNECTIONS").context("DATABASE_MAX_CONNECTIONS not set")?.parse()?;
|
||||
let host = std::env::var("HOST").context("HOST not set")?;
|
||||
let port = std::env::var("PORT").context("PORT not set")?.parse()?;
|
||||
let title = std::env::var("TITLE").context("TITLE not set")?;
|
||||
|
||||
Ok(Config {
|
||||
database_url,
|
||||
database_max_connections,
|
||||
host,
|
||||
port,
|
||||
title,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
@ -4,3 +4,4 @@ pub mod handlers;
|
||||
pub mod jobs;
|
||||
pub mod models;
|
||||
pub mod partials;
|
||||
pub mod state;
|
||||
|
11
src/main.rs
11
src/main.rs
@ -3,7 +3,7 @@ use std::path::Path;
|
||||
use anyhow::Result;
|
||||
use axum::{
|
||||
routing::{get, post},
|
||||
Router, Extension,
|
||||
Router,
|
||||
};
|
||||
use dotenvy::dotenv;
|
||||
use notify::Watcher;
|
||||
@ -15,6 +15,7 @@ use tracing::debug;
|
||||
|
||||
use lib::config;
|
||||
use lib::handlers;
|
||||
use lib::state::AppState;
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() -> Result<()> {
|
||||
@ -41,9 +42,11 @@ async fn main() -> Result<()> {
|
||||
.route("/api/v1/entry/:id", get(handlers::api::entry::get))
|
||||
.route("/", get(handlers::home::get))
|
||||
.route("/feeds", get(handlers::feeds::get))
|
||||
.with_state(pool)
|
||||
.layer(ServiceBuilder::new().layer(TraceLayer::new_for_http()))
|
||||
.layer(Extension(config));
|
||||
.with_state(AppState {
|
||||
pool,
|
||||
config,
|
||||
})
|
||||
.layer(ServiceBuilder::new().layer(TraceLayer::new_for_http()));
|
||||
|
||||
#[cfg(debug_assertions)]
|
||||
{
|
||||
|
@ -1,25 +1,33 @@
|
||||
use axum::{
|
||||
async_trait,
|
||||
extract::FromRequestParts,
|
||||
extract::{FromRef, FromRequestParts, State},
|
||||
http::request::Parts,
|
||||
response::{Html, IntoResponse, Response},
|
||||
};
|
||||
use maud::{DOCTYPE, html, Markup};
|
||||
use maud::{html, Markup, DOCTYPE};
|
||||
|
||||
use crate::config::Config;
|
||||
use crate::partials::header::header;
|
||||
|
||||
pub struct Layout;
|
||||
pub struct Layout {
|
||||
pub title: String,
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
impl<S> FromRequestParts<S> for Layout
|
||||
where
|
||||
S: Send + Sync,
|
||||
Config: FromRef<S>,
|
||||
{
|
||||
type Rejection = std::convert::Infallible;
|
||||
type Rejection = Response;
|
||||
|
||||
async fn from_request_parts(_parts: &mut Parts, _state: &S) -> Result<Self, Self::Rejection> {
|
||||
// extract whatever your layout needs
|
||||
Ok(Self {})
|
||||
async fn from_request_parts(parts: &mut Parts, state: &S) -> Result<Self, Self::Rejection> {
|
||||
let State(config) = State::<Config>::from_request_parts(parts, state)
|
||||
.await
|
||||
.map_err(|err| err.into_response())?;
|
||||
Ok(Self {
|
||||
title: config.title,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
@ -40,7 +48,8 @@ impl Layout {
|
||||
(template)
|
||||
}
|
||||
}
|
||||
}.into_string();
|
||||
}
|
||||
.into_string();
|
||||
|
||||
Html(with_layout).into_response()
|
||||
}
|
||||
|
22
src/state.rs
Normal file
22
src/state.rs
Normal file
@ -0,0 +1,22 @@
|
||||
use axum::extract::FromRef;
|
||||
use sqlx::PgPool;
|
||||
|
||||
use crate::config::Config;
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct AppState {
|
||||
pub pool: PgPool,
|
||||
pub config: Config,
|
||||
}
|
||||
|
||||
impl FromRef<AppState> for PgPool {
|
||||
fn from_ref(state: &AppState) -> Self {
|
||||
state.pool.clone()
|
||||
}
|
||||
}
|
||||
|
||||
impl FromRef<AppState> for Config {
|
||||
fn from_ref(state: &AppState) -> Self {
|
||||
state.config.clone()
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user