Add branding .env variables and remove hard coded.

New Variables (including matching commandline parameters):
* PSLINK_EMPTY_FORWARD_URL
* PSLINK_BRAND_NAME
This commit is contained in:
Dietrich 2021-03-11 12:54:09 +01:00
parent 23fa296ceb
commit 6c6d8d4d5b
Signed by: dietrich
GPG Key ID: 9F3C20C0F85DF67C
3 changed files with 48 additions and 15 deletions

View File

@ -14,6 +14,7 @@ use crate::{queries, schema};
use slog::{Drain, Logger}; use slog::{Drain, Logger};
#[allow(clippy::clippy::too_many_lines)]
fn generate_cli() -> App<'static, 'static> { fn generate_cli() -> App<'static, 'static> {
app_from_crate!() app_from_crate!()
.arg( .arg(
@ -42,6 +43,24 @@ fn generate_cli() -> App<'static, 'static> {
.default_value("localhost:8080") .default_value("localhost:8080")
.global(true), .global(true),
) )
.arg(
Arg::with_name("empty_forward_url")
.long("empty-url")
.short("e")
.help("The the url that / will redirect to. Usually your homepage.")
.env("PSLINK_EMPTY_FORWARD_URL")
.default_value("https://github.com/enaut/pslink")
.global(true),
)
.arg(
Arg::with_name("brand_name")
.long("brand-name")
.short("b")
.help("The Brandname that will apper in various places.")
.env("PSLINK_BRAND_NAME")
.default_value("Pslink")
.global(true),
)
.arg( .arg(
Arg::with_name("internal_ip") Arg::with_name("internal_ip")
.long("hostip") .long("hostip")
@ -148,6 +167,14 @@ fn parse_args_to_config(config: &ArgMatches, log: &Logger) -> ServerConfig {
.value_of("public_url") .value_of("public_url")
.expect("Failed to read the host value") .expect("Failed to read the host value")
.to_owned(); .to_owned();
let empty_forward_url = config
.value_of("empty_forward_url")
.expect("Failed to read the empty_forward_url value")
.to_owned();
let brand_name = config
.value_of("brand_name")
.expect("Failed to read the brand_name value")
.to_owned();
let internal_ip = config let internal_ip = config
.value_of("internal_ip") .value_of("internal_ip")
.expect("Failed to read the host value") .expect("Failed to read the host value")
@ -173,6 +200,8 @@ fn parse_args_to_config(config: &ArgMatches, log: &Logger) -> ServerConfig {
port, port,
protocol, protocol,
log, log,
empty_forward_url,
brand_name,
} }
} }

View File

@ -163,6 +163,8 @@ pub(crate) struct ServerConfig {
port: u32, port: u32,
protocol: Protocol, protocol: Protocol,
log: slog::Logger, log: slog::Logger,
empty_forward_url: String,
brand_name: String,
} }
impl ServerConfig { impl ServerConfig {
@ -171,6 +173,8 @@ impl ServerConfig {
format!("PSLINK_DATABASE=\"{}\"\n", self.db.display()), format!("PSLINK_DATABASE=\"{}\"\n", self.db.display()),
format!("PSLINK_PORT={}\n", self.port), format!("PSLINK_PORT={}\n", self.port),
format!("PSLINK_PUBLIC_URL=\"{}\"\n", self.public_url), format!("PSLINK_PUBLIC_URL=\"{}\"\n", self.public_url),
format!("PSLINK_EMPTY_FORWARD_URL=\"{}\"\n", self.empty_forward_url),
format!("PSLINK_BRAND_NAME=\"{}\"\n", self.brand_name),
format!("PSLINK_IP=\"{}\"\n", self.internal_ip), format!("PSLINK_IP=\"{}\"\n", self.internal_ip),
format!("PSLINK_PROTOCOL=\"{}\"\n", self.protocol), format!("PSLINK_PROTOCOL=\"{}\"\n", self.protocol),
concat!( concat!(
@ -262,8 +266,8 @@ async fn webservice(server_config: ServerConfig) -> std::io::Result<()> {
.service(actix_web_static_files::ResourceFiles::new( .service(actix_web_static_files::ResourceFiles::new(
"/static", generated, "/static", generated,
)) ))
// directly go to the main page of Freie-Hochschule-Stuttgart // directly go to the main page set the target with the environment variable.
.route("/", web::get().to(views::redirect_fhs)) .route("/", web::get().to(views::redirect_empty))
// admin block // admin block
.service( .service(
web::scope("/admin") web::scope("/admin")
@ -283,7 +287,7 @@ async fn webservice(server_config: ServerConfig) -> std::io::Result<()> {
.service( .service(
web::scope("/link") web::scope("/link")
.route("/{redirect_id}", web::get().to(views::view_link)) .route("/{redirect_id}", web::get().to(views::view_link))
.route("/", web::get().to(views::view_link_fhs)), .route("/", web::get().to(views::view_link_empty)),
) )
.service( .service(
web::scope("/profile") web::scope("/profile")

View File

@ -36,7 +36,7 @@ pub(crate) async fn index(
if let Ok(links) = queries::list_all_allowed(&id, &config) { if let Ok(links) = queries::list_all_allowed(&id, &config) {
let mut data = Context::new(); let mut data = Context::new();
data.insert("user", &links.user); data.insert("user", &links.user);
data.insert("title", "Links der Freien Hochschule Stuttgart"); data.insert("title", &format!("Links der {}", &config.brand_name,));
data.insert("links_per_users", &links.list); data.insert("links_per_users", &links.list);
let rendered = tera.render("index.html", &data)?; let rendered = tera.render("index.html", &data)?;
Ok(HttpResponse::Ok().body(rendered)) Ok(HttpResponse::Ok().body(rendered))
@ -54,7 +54,7 @@ pub(crate) async fn index_users(
if let Ok(users) = queries::list_users(&id, &config) { if let Ok(users) = queries::list_users(&id, &config) {
let mut data = Context::new(); let mut data = Context::new();
data.insert("user", &users.user); data.insert("user", &users.user);
data.insert("title", "Benutzer der Freien Hochschule Stuttgart"); data.insert("title", &format!("Benutzer der {}", &config.brand_name,));
data.insert("users", &users.list); data.insert("users", &users.list);
let rendered = tera.render("index_users.html", &data)?; let rendered = tera.render("index_users.html", &data)?;
@ -63,7 +63,7 @@ pub(crate) async fn index_users(
Ok(redirect_builder("/admin/login")) Ok(redirect_builder("/admin/login"))
} }
} }
pub(crate) async fn view_link_fhs( pub(crate) async fn view_link_empty(
tera: web::Data<Tera>, tera: web::Data<Tera>,
config: web::Data<crate::ServerConfig>, config: web::Data<crate::ServerConfig>,
id: Identity, id: Identity,
@ -96,7 +96,7 @@ pub(crate) async fn view_link(
data.insert("user", &link.user); data.insert("user", &link.user);
data.insert( data.insert(
"title", "title",
&format!("Links {} der Freien Hochschule Stuttgart", &link.item.code), &format!("Links {} der {}", &link.item.code, &config.brand_name,),
); );
data.insert("link", &link.item); data.insert("link", &link.item);
data.insert("qr", &svg); data.insert("qr", &svg);
@ -123,8 +123,8 @@ pub(crate) async fn view_profile(
data.insert( data.insert(
"title", "title",
&format!( &format!(
"Benutzer {} der Freien Hochschule Stuttgart", "Benutzer {} der {}",
&query.item.username &query.item.username, &config.brand_name,
), ),
); );
data.insert("viewed_user", &query.item); data.insert("viewed_user", &query.item);
@ -150,8 +150,8 @@ pub(crate) async fn edit_profile(
data.insert( data.insert(
"title", "title",
&format!( &format!(
"Benutzer {} der Freien Hochschule Stuttgart", "Benutzer {} der {}",
&query.user.username &query.user.username, &config.brand_name,
), ),
); );
data.insert("user", &query.user); data.insert("user", &query.user);
@ -330,10 +330,10 @@ pub(crate) async fn redirect(
} }
} }
pub(crate) async fn redirect_fhs() -> Result<HttpResponse, ServerError> { pub(crate) async fn redirect_empty(
Ok(redirect_builder( config: web::Data<crate::ServerConfig>,
"https://www.freie-hochschule-stuttgart.de", ) -> Result<HttpResponse, ServerError> {
)) Ok(redirect_builder(&config.empty_forward_url))
} }
pub(crate) async fn create_link( pub(crate) async fn create_link(