From 2abc5844ad2d610e17898fc652a308bf5dca6434 Mon Sep 17 00:00:00 2001 From: Slatian Date: Sun, 19 Feb 2023 23:12:43 +0100 Subject: [PATCH] Working template configuration file --- src/config.rs | 2 ++ src/main.rs | 47 +++++++++++++++++++++++++++++++--------- src/templating_engine.rs | 3 +++ templates/extra.toml | 1 + templates/index.html | 5 +++++ 5 files changed, 48 insertions(+), 10 deletions(-) create mode 100644 templates/extra.toml diff --git a/src/config.rs b/src/config.rs index 318d9a7..312454a 100644 --- a/src/config.rs +++ b/src/config.rs @@ -34,6 +34,7 @@ pub struct GeoIpConfig { #[derive(serde::Deserialize)] pub struct TemplateConfig { pub template_location: String, + pub extra_config: Option, } impl Default for ServerConfig { @@ -69,6 +70,7 @@ impl Default for TemplateConfig { fn default() -> Self { TemplateConfig { template_location: "templates/".to_string(), + extra_config: None, } } } diff --git a/src/main.rs b/src/main.rs index 1514661..7e801f9 100644 --- a/src/main.rs +++ b/src/main.rs @@ -72,7 +72,7 @@ struct ServiceSharedState { } #[derive(Parser)] -#[command(author, version, long_about="None")] +#[command(author, version, long_about="A web service that tells you your ip-address and more …")] struct CliArgs { #[arg(short, long)] config: Option, @@ -80,6 +80,8 @@ struct CliArgs { listen_on: Option, #[arg(short, long)] templates: Option, + #[arg(short,long)] + extra_config: Option, } fn match_domain_hidden_list(domain: &String, hidden_list: &Vec) -> bool { @@ -93,6 +95,23 @@ fn match_domain_hidden_list(domain: &String, hidden_list: &Vec) -> bool return false; } +fn read_toml_from_file serde::Deserialize<'de>>(path: &String) -> Option { + let text = match fs::read_to_string(path) { + Ok(t) => t, + Err(e) => { + println!("Error while reading file '{path}': {e}"); + return None; + } + }; + match toml::from_str(&text) { + Ok(t) => Some(t), + Err(e) => { + println!("Unable to parse file '{path}':\n{e}"); + return None; + } + } +} + #[tokio::main] async fn main() { // Parse Command line arguments @@ -101,26 +120,33 @@ async fn main() { // Read configuration file let config: config::EchoIpServiceConfig = match cli_args.config { Some(config_path) => { - let config_text = fs::read_to_string(config_path) - .expect("Can't read configuration file!"); - match toml::from_str(&config_text) { - Ok(c) => c, - Err(e) => { - println!("Unable to parse configuration file:\n{e}"); + match read_toml_from_file::(&config_path) { + Some(c) => c, + None => { + println!("Could not read confuration file, exiting."); ::std::process::exit(1); } } }, None => Default::default(), }; - - // Initalize Tera templates let mut template_base_dir = (&config.template.template_location).to_owned(); if !template_base_dir.ends_with("/") { template_base_dir = template_base_dir + "/"; } + let template_extra_config = match &cli_args.extra_config { + Some(path) => read_toml_from_file(path), + None => match &config.template.extra_config { + Some(path) => read_toml_from_file(path), + None => { + println!("Trying to read default template configuration ..."); + println!("(If this fails that may be ok, depending on your template)"); + read_toml_from_file(&(template_base_dir.clone()+"extra.toml")) + }, + }, + }; let template_glob = template_base_dir+"*.html"; println!("Parsing Templates from '{}' ...", &template_glob); let res = Tera::new((template_glob).as_str()); @@ -174,6 +200,7 @@ async fn main() { ServiceSharedState { templating_engine: templating_engine::Engine{ tera: tera, + template_config: template_extra_config, }, dns_resolver: dns_resolver, asn_db: asn_db, @@ -196,7 +223,7 @@ async fn main() { println!("Starting Server ..."); axum::Server::bind(&listen_on) - .serve(app.into_make_service()) + .serve(app.into_make_service_with_connect_info::()) .await .unwrap(); } diff --git a/src/templating_engine.rs b/src/templating_engine.rs index b30d046..5ecc39e 100644 --- a/src/templating_engine.rs +++ b/src/templating_engine.rs @@ -11,6 +11,7 @@ use axum::{ response::Json, }; use tera::Tera; +use toml::Table; use crate::simple_dns; use crate::IpResult; @@ -68,6 +69,7 @@ impl View { pub struct Engine { pub tera: Tera, + pub template_config: Option, } impl Engine { @@ -85,6 +87,7 @@ impl Engine { //intented for shared macros context.insert("format", &format.to_string()); context.insert("data", &view); + context.insert("extra", &self.template_config); match self.tera.render(&(template_name+".html"), &context) { Ok(html) => Html(html).into_response(), diff --git a/templates/extra.toml b/templates/extra.toml new file mode 100644 index 0000000..09c2a13 --- /dev/null +++ b/templates/extra.toml @@ -0,0 +1 @@ +site_name="echoip-slatecave" diff --git a/templates/index.html b/templates/index.html index 3e793e2..409ea82 100644 --- a/templates/index.html +++ b/templates/index.html @@ -6,6 +6,11 @@ {% set r = data.result %} +
+ +

Your IP-Address is: {{ data.query.ip }}

Network Information