Skip to content

Commit

Permalink
Add branding .env variables and remove hard coded.
Browse files Browse the repository at this point in the history
New Variables (including matching commandline parameters):
* PSLINK_EMPTY_FORWARD_URL
* PSLINK_BRAND_NAME
  • Loading branch information
enaut committed Mar 11, 2021
1 parent 23fa296 commit 6c6d8d4
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 15 deletions.
29 changes: 29 additions & 0 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use crate::{queries, schema};

use slog::{Drain, Logger};

#[allow(clippy::clippy::too_many_lines)]
fn generate_cli() -> App<'static, 'static> {
app_from_crate!()
.arg(
Expand Down Expand Up @@ -42,6 +43,24 @@ fn generate_cli() -> App<'static, 'static> {
.default_value("localhost:8080")
.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::with_name("internal_ip")
.long("hostip")
Expand Down Expand Up @@ -148,6 +167,14 @@ fn parse_args_to_config(config: &ArgMatches, log: &Logger) -> ServerConfig {
.value_of("public_url")
.expect("Failed to read the host value")
.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
.value_of("internal_ip")
.expect("Failed to read the host value")
Expand All @@ -173,6 +200,8 @@ fn parse_args_to_config(config: &ArgMatches, log: &Logger) -> ServerConfig {
port,
protocol,
log,
empty_forward_url,
brand_name,
}
}

Expand Down
10 changes: 7 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,8 @@ pub(crate) struct ServerConfig {
port: u32,
protocol: Protocol,
log: slog::Logger,
empty_forward_url: String,
brand_name: String,
}

impl ServerConfig {
Expand All @@ -171,6 +173,8 @@ impl ServerConfig {
format!("PSLINK_DATABASE=\"{}\"\n", self.db.display()),
format!("PSLINK_PORT={}\n", self.port),
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_PROTOCOL=\"{}\"\n", self.protocol),
concat!(
Expand Down Expand Up @@ -262,8 +266,8 @@ async fn webservice(server_config: ServerConfig) -> std::io::Result<()> {
.service(actix_web_static_files::ResourceFiles::new(
"/static", generated,
))
// directly go to the main page of Freie-Hochschule-Stuttgart
.route("/", web::get().to(views::redirect_fhs))
// directly go to the main page set the target with the environment variable.
.route("/", web::get().to(views::redirect_empty))
// admin block
.service(
web::scope("/admin")
Expand All @@ -283,7 +287,7 @@ async fn webservice(server_config: ServerConfig) -> std::io::Result<()> {
.service(
web::scope("/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(
web::scope("/profile")
Expand Down
24 changes: 12 additions & 12 deletions src/views.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ pub(crate) async fn index(
if let Ok(links) = queries::list_all_allowed(&id, &config) {
let mut data = Context::new();
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);
let rendered = tera.render("index.html", &data)?;
Ok(HttpResponse::Ok().body(rendered))
Expand All @@ -54,7 +54,7 @@ pub(crate) async fn index_users(
if let Ok(users) = queries::list_users(&id, &config) {
let mut data = Context::new();
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);

let rendered = tera.render("index_users.html", &data)?;
Expand All @@ -63,7 +63,7 @@ pub(crate) async fn index_users(
Ok(redirect_builder("/admin/login"))
}
}
pub(crate) async fn view_link_fhs(
pub(crate) async fn view_link_empty(
tera: web::Data<Tera>,
config: web::Data<crate::ServerConfig>,
id: Identity,
Expand Down Expand Up @@ -96,7 +96,7 @@ pub(crate) async fn view_link(
data.insert("user", &link.user);
data.insert(
"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("qr", &svg);
Expand All @@ -123,8 +123,8 @@ pub(crate) async fn view_profile(
data.insert(
"title",
&format!(
"Benutzer {} der Freien Hochschule Stuttgart",
&query.item.username
"Benutzer {} der {}",
&query.item.username, &config.brand_name,
),
);
data.insert("viewed_user", &query.item);
Expand All @@ -150,8 +150,8 @@ pub(crate) async fn edit_profile(
data.insert(
"title",
&format!(
"Benutzer {} der Freien Hochschule Stuttgart",
&query.user.username
"Benutzer {} der {}",
&query.user.username, &config.brand_name,
),
);
data.insert("user", &query.user);
Expand Down Expand Up @@ -330,10 +330,10 @@ pub(crate) async fn redirect(
}
}

pub(crate) async fn redirect_fhs() -> Result<HttpResponse, ServerError> {
Ok(redirect_builder(
"https://www.freie-hochschule-stuttgart.de",
))
pub(crate) async fn redirect_empty(
config: web::Data<crate::ServerConfig>,
) -> Result<HttpResponse, ServerError> {
Ok(redirect_builder(&config.empty_forward_url))
}

pub(crate) async fn create_link(
Expand Down

0 comments on commit 6c6d8d4

Please sign in to comment.