Working settings file! (partially)

This commit is contained in:
Slatian
2023-02-18 22:16:09 +01:00
parent 2394d90087
commit 645a0eed69
8 changed files with 478 additions and 39 deletions

View File

@ -7,7 +7,7 @@ use axum::{
routing::get,
};
use axum_client_ip::{SecureClientIp, SecureClientIpSource};
use clap::Parser;
use tera::Tera;
use trust_dns_resolver::{
TokioAsyncResolver,
@ -15,6 +15,7 @@ use trust_dns_resolver::{
config::ResolverConfig,
};
use std::fs;
use std::net::IpAddr;
use std::sync::Arc;
use std::path::Path;
@ -64,15 +65,50 @@ struct ServiceSharedState {
dns_resolver: TokioAsyncResolver,
asn_db: geoip::MMDBCarrier,
location_db: geoip::MMDBCarrier,
config: config::EchoIpServiceConfig,
}
#[derive(Parser)]
#[command(author, version, long_about="None")]
struct CliArgs {
#[arg(short, long)]
config: Option<String>,
#[arg(short, long)]
listen_on: Option<String>,
#[arg(short, long)]
templates: Option<String>,
}
#[tokio::main]
async fn main() {
// Parse Command line arguments
let cli_args = CliArgs::parse();
// 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) => {
panic!("Unable to parse configuration file: {e}");
}
}
},
None => Default::default(),
};
// Initalize Tera templates
// TODO: don't hardcode template directory
println!("Parsing Templates ...");
let res = Tera::new("templates/*.html");
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_glob = template_base_dir+"*.html";
println!("Parsing Templates from '{}' ...", &template_glob);
let res = Tera::new((template_glob).as_str());
let tera = match res {
Ok(t) => t,
Err(e) => {
@ -87,13 +123,19 @@ async fn main() {
mmdb: None,
name: "GeoIP ASN Database".to_string(),
};
asn_db.load_database_from_path(Path::new("mmdb/GeoLite2-ASN.mmdb")).ok();
match &config.geoip.asn_database {
Some(path) => { asn_db.load_database_from_path(Path::new(&path)).ok(); },
None => {},
}
let mut location_db = geoip::MMDBCarrier {
mmdb: None,
name: "GeoIP Location Database".to_string(),
};
location_db.load_database_from_path(Path::new("mmdb/GeoLite2-City.mmdb")).ok();
match &config.geoip.location_database {
Some(path) => { location_db.load_database_from_path(Path::new(&path)).ok(); },
None => {},
}
// Initalize DNS resolver with os defaults
println!("Initalizing dns resolver ...");
@ -105,16 +147,19 @@ async fn main() {
::std::process::exit(1);
}
};
let listen_on = config.server.listen_on;
// Initialize shared state
let shared_state = Arc::new(
ServiceSharedState{
ServiceSharedState {
templating_engine: templating_engine::Engine{
tera: tera,
},
dns_resolver: dns_resolver,
asn_db: asn_db,
location_db: location_db,
config: config,
});
// Initalize axum server
@ -131,7 +176,7 @@ async fn main() {
println!("Starting Server ...");
axum::Server::bind(&"0.0.0.0:3000".parse().unwrap())
axum::Server::bind(&listen_on)
.serve(app.into_make_service())
.await
.unwrap();