mirror of
https://codeberg.org/slatian/service.echoip-slatecave.git
synced 2024-12-27 12:18:20 +01:00
Working template configuration file
This commit is contained in:
parent
d4caf1a77c
commit
2abc5844ad
@ -34,6 +34,7 @@ pub struct GeoIpConfig {
|
||||
#[derive(serde::Deserialize)]
|
||||
pub struct TemplateConfig {
|
||||
pub template_location: String,
|
||||
pub extra_config: Option<String>,
|
||||
}
|
||||
|
||||
impl Default for ServerConfig {
|
||||
@ -69,6 +70,7 @@ impl Default for TemplateConfig {
|
||||
fn default() -> Self {
|
||||
TemplateConfig {
|
||||
template_location: "templates/".to_string(),
|
||||
extra_config: None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
47
src/main.rs
47
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<String>,
|
||||
@ -80,6 +80,8 @@ struct CliArgs {
|
||||
listen_on: Option<String>,
|
||||
#[arg(short, long)]
|
||||
templates: Option<String>,
|
||||
#[arg(short,long)]
|
||||
extra_config: Option<String>,
|
||||
}
|
||||
|
||||
fn match_domain_hidden_list(domain: &String, hidden_list: &Vec<String>) -> bool {
|
||||
@ -93,6 +95,23 @@ fn match_domain_hidden_list(domain: &String, hidden_list: &Vec<String>) -> bool
|
||||
return false;
|
||||
}
|
||||
|
||||
fn read_toml_from_file<T: for<'de> serde::Deserialize<'de>>(path: &String) -> Option<T> {
|
||||
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::EchoIpServiceConfig>(&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::<std::net::SocketAddr>())
|
||||
.await
|
||||
.unwrap();
|
||||
}
|
||||
|
@ -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<Table>,
|
||||
}
|
||||
|
||||
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(),
|
||||
|
1
templates/extra.toml
Normal file
1
templates/extra.toml
Normal file
@ -0,0 +1 @@
|
||||
site_name="echoip-slatecave"
|
@ -6,6 +6,11 @@
|
||||
</head>
|
||||
<body>
|
||||
{% set r = data.result %}
|
||||
<header>
|
||||
<nav>
|
||||
<a href="/" class="site-name">{{extra.site_name|default(val="echoip")}}</a>
|
||||
</nav>
|
||||
</header>
|
||||
<h1>Your IP-Address is: {{ data.query.ip }}</h1>
|
||||
<section>
|
||||
<h2>Network Information</h2>
|
||||
|
Loading…
Reference in New Issue
Block a user