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:
|
directory with the contents:
|
||||||
|
|
||||||
```
|
```
|
||||||
|
RUST_LOG=crawlnicle=debug,cli=debug,lib=debug,tower_http=debug,sqlx=debug
|
||||||
HOST=127.0.0.1
|
HOST=127.0.0.1
|
||||||
PORT=3000
|
PORT=3000
|
||||||
DATABASE_URL=postgresql://crawlnicle:<password>@localhost/crawlnicle
|
DATABASE_URL=postgresql://crawlnicle:<password>@localhost/crawlnicle
|
||||||
DATABASE_MAX_CONNECTIONS=5
|
DATABASE_MAX_CONNECTIONS=5
|
||||||
RUST_LOG=crawlnicle=debug,cli=debug,lib=debug,tower_http=debug,sqlx=debug
|
TITLE=crawlnicle
|
||||||
```
|
```
|
||||||
|
|
||||||
4. Install
|
4. Install
|
||||||
|
@ -6,6 +6,7 @@ pub struct Config {
|
|||||||
pub database_max_connections: u32,
|
pub database_max_connections: u32,
|
||||||
pub host: String,
|
pub host: String,
|
||||||
pub port: u16,
|
pub port: u16,
|
||||||
|
pub title: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Config {
|
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 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 host = std::env::var("HOST").context("HOST not set")?;
|
||||||
let port = std::env::var("PORT").context("PORT not set")?.parse()?;
|
let port = std::env::var("PORT").context("PORT not set")?.parse()?;
|
||||||
|
let title = std::env::var("TITLE").context("TITLE not set")?;
|
||||||
|
|
||||||
Ok(Config {
|
Ok(Config {
|
||||||
database_url,
|
database_url,
|
||||||
database_max_connections,
|
database_max_connections,
|
||||||
host,
|
host,
|
||||||
port,
|
port,
|
||||||
|
title,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -4,3 +4,4 @@ pub mod handlers;
|
|||||||
pub mod jobs;
|
pub mod jobs;
|
||||||
pub mod models;
|
pub mod models;
|
||||||
pub mod partials;
|
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 anyhow::Result;
|
||||||
use axum::{
|
use axum::{
|
||||||
routing::{get, post},
|
routing::{get, post},
|
||||||
Router, Extension,
|
Router,
|
||||||
};
|
};
|
||||||
use dotenvy::dotenv;
|
use dotenvy::dotenv;
|
||||||
use notify::Watcher;
|
use notify::Watcher;
|
||||||
@ -15,6 +15,7 @@ use tracing::debug;
|
|||||||
|
|
||||||
use lib::config;
|
use lib::config;
|
||||||
use lib::handlers;
|
use lib::handlers;
|
||||||
|
use lib::state::AppState;
|
||||||
|
|
||||||
#[tokio::main]
|
#[tokio::main]
|
||||||
async fn main() -> Result<()> {
|
async fn main() -> Result<()> {
|
||||||
@ -41,9 +42,11 @@ async fn main() -> Result<()> {
|
|||||||
.route("/api/v1/entry/:id", get(handlers::api::entry::get))
|
.route("/api/v1/entry/:id", get(handlers::api::entry::get))
|
||||||
.route("/", get(handlers::home::get))
|
.route("/", get(handlers::home::get))
|
||||||
.route("/feeds", get(handlers::feeds::get))
|
.route("/feeds", get(handlers::feeds::get))
|
||||||
.with_state(pool)
|
.with_state(AppState {
|
||||||
.layer(ServiceBuilder::new().layer(TraceLayer::new_for_http()))
|
pool,
|
||||||
.layer(Extension(config));
|
config,
|
||||||
|
})
|
||||||
|
.layer(ServiceBuilder::new().layer(TraceLayer::new_for_http()));
|
||||||
|
|
||||||
#[cfg(debug_assertions)]
|
#[cfg(debug_assertions)]
|
||||||
{
|
{
|
||||||
|
@ -1,25 +1,33 @@
|
|||||||
use axum::{
|
use axum::{
|
||||||
async_trait,
|
async_trait,
|
||||||
extract::FromRequestParts,
|
extract::{FromRef, FromRequestParts, State},
|
||||||
http::request::Parts,
|
http::request::Parts,
|
||||||
response::{Html, IntoResponse, Response},
|
response::{Html, IntoResponse, Response},
|
||||||
};
|
};
|
||||||
use maud::{DOCTYPE, html, Markup};
|
use maud::{html, Markup, DOCTYPE};
|
||||||
|
|
||||||
|
use crate::config::Config;
|
||||||
use crate::partials::header::header;
|
use crate::partials::header::header;
|
||||||
|
|
||||||
pub struct Layout;
|
pub struct Layout {
|
||||||
|
pub title: String,
|
||||||
|
}
|
||||||
|
|
||||||
#[async_trait]
|
#[async_trait]
|
||||||
impl<S> FromRequestParts<S> for Layout
|
impl<S> FromRequestParts<S> for Layout
|
||||||
where
|
where
|
||||||
S: Send + Sync,
|
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> {
|
async fn from_request_parts(parts: &mut Parts, state: &S) -> Result<Self, Self::Rejection> {
|
||||||
// extract whatever your layout needs
|
let State(config) = State::<Config>::from_request_parts(parts, state)
|
||||||
Ok(Self {})
|
.await
|
||||||
|
.map_err(|err| err.into_response())?;
|
||||||
|
Ok(Self {
|
||||||
|
title: config.title,
|
||||||
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -40,7 +48,8 @@ impl Layout {
|
|||||||
(template)
|
(template)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}.into_string();
|
}
|
||||||
|
.into_string();
|
||||||
|
|
||||||
Html(with_layout).into_response()
|
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