wip: adding public_url config key to get a canonical url into the code

This commit is contained in:
Artemis 2025-02-07 18:00:04 +01:00
parent dd26eed497
commit c183ccaf8b
4 changed files with 18 additions and 2 deletions

View file

@ -1,4 +1,6 @@
[default]
secret_key = "8STDFCStGMYGoOq8RJf3JJXsg4p6wZVAph50R3Fbq6U="
public_url = "http://localhost:8000/"
[default.databases.dolltags]
url = "postgres://postgres:woofwoof@localhost/dolltags"

7
src/config.rs Normal file
View file

@ -0,0 +1,7 @@
use rocket::serde::Deserialize;
#[derive(Debug, Deserialize)]
#[serde(crate = "rocket::serde")]
pub struct Config {
pub public_url: String,
}

View file

@ -6,6 +6,7 @@ use std::path::PathBuf;
use auth::session;
use cli::check_cli_invocation;
use config::Config;
use db::migrate::run_migrations;
use db::schema::DollTags;
use rocket::fairing::AdHoc;
@ -16,6 +17,7 @@ use routes::{account, admin, error_handlers, form, public};
pub mod auth;
pub mod cli;
pub mod config;
pub mod db;
pub mod ids;
pub mod pages;
@ -49,6 +51,7 @@ fn rocket() -> _ {
rocket::build()
.attach(pages::init_templates())
.attach(DollTags::init())
.attach(AdHoc::config::<Config>())
.attach(AdHoc::try_on_ignite("SQLx migrations", run_migrations))
.attach(AdHoc::on_liftoff("CLI invocation hack", |rocket| {
Box::pin(async move { check_cli_invocation(rocket).await })

View file

@ -5,12 +5,12 @@ use rocket::{
outcome::try_outcome,
request::{FromRequest, Outcome},
serde::Serialize,
Request,
Request, State,
};
use rocket_dyn_templates::{tera::try_get_value, Template};
use serde_json::{to_value, Value};
use crate::{auth::session::Session, db::schema::User, ids};
use crate::{auth::session::Session, config::Config, db::schema::User, ids};
pub fn init_templates() -> impl Fairing {
Template::custom(|engines| {
@ -36,6 +36,8 @@ pub struct CommonTemplateState {
pub is_admin: bool,
/// feature flag - disables the UI for it since it's not implemeted yet.
pub forgot_password_implemented: bool,
/// the website's public base URL (used for canonical URLs)
pub public_url: String,
}
impl CommonTemplateState {
@ -55,11 +57,13 @@ impl<'r> FromRequest<'r> for CommonTemplateState {
async fn from_request(request: &'r Request<'_>) -> Outcome<CommonTemplateState, ()> {
let session_state = try_outcome!(request.guard::<Session>().await);
let config_state = try_outcome!(request.guard::<&State<Config>>().await);
Outcome::Success(CommonTemplateState {
logged_in: session_state.0.is_some(),
is_admin: false,
forgot_password_implemented: false,
public_url: config_state.public_url.clone(),
})
}
}