mirror of
https://codeberg.org/slatian/service.echoip-slatecave.git
synced 2025-07-16 14:03:28 +02:00
Working template configuration file
This commit is contained in:
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();
|
||||
}
|
||||
|
Reference in New Issue
Block a user