Read config (ip/port) from file.

This commit is contained in:
Viyurz 2024-03-03 14:12:55 +01:00
parent eb5ab35d50
commit ae6ab53520
Signed by: Viyurz
SSH key fingerprint: SHA256:IskOHTmhHSJIvAt04N6aaxd5SZCVWW1Guf9tEcxIMj8
2 changed files with 33 additions and 11 deletions

2
config.toml Normal file
View file

@ -0,0 +1,2 @@
ip = "0.0.0.0"
port = 8080

View file

@ -18,12 +18,19 @@ struct AppState {
services: Vec<Service>, services: Vec<Service>,
} }
#[derive(Deserialize, Debug)]
struct Config {
ip: Option<String>,
port: Option<u16>,
services: Option<Vec<Service>>,
}
#[derive(Deserialize)] #[derive(Deserialize)]
struct ServiceVec { struct ServiceVec {
services: Vec<Service>, services: Vec<Service>,
} }
#[derive(Deserialize, Serialize)] #[derive(Deserialize, Serialize, Debug)]
struct Service { struct Service {
name: String, name: String,
description: String, description: String,
@ -33,8 +40,21 @@ struct Service {
repository_url: String, repository_url: String,
} }
fn load_config() -> Config {
let config_str = read_to_string("config.toml").expect("Failed to read config.toml file.");
let services_str = read_to_string("services.toml").expect("Failed to read services.toml file.");
let mut config: Config = toml::from_str(&config_str).unwrap();
let service_vec: ServiceVec = toml::from_str(&services_str).unwrap();
config.services = Option::from(service_vec.services);
return config;
}
// Minify templates before loading them // Minify templates before loading them
fn load_templates(env: &mut Environment) { fn load_templates() -> Environment<'static> {
let mut env = Environment::new();
let cfg = minify_html::Cfg::spec_compliant(); let cfg = minify_html::Cfg::spec_compliant();
for template_entry in fs::read_dir("./templates").expect("Failed to read directory ./templates.") { for template_entry in fs::read_dir("./templates").expect("Failed to read directory ./templates.") {
@ -46,10 +66,14 @@ fn load_templates(env: &mut Environment) {
env.add_template_owned(template_name, String::from_utf8(template_code_minified).unwrap()).unwrap(); env.add_template_owned(template_name, String::from_utf8(template_code_minified).unwrap()).unwrap();
} }
return env;
} }
#[tokio::main] #[tokio::main]
async fn main() { async fn main() {
let config: Config = load_config();
tracing_subscriber::registry() tracing_subscriber::registry()
.with( .with(
tracing_subscriber::EnvFilter::try_from_default_env().unwrap_or_else(|_| { tracing_subscriber::EnvFilter::try_from_default_env().unwrap_or_else(|_| {
@ -61,17 +85,11 @@ async fn main() {
.with(tracing_subscriber::fmt::layer()) .with(tracing_subscriber::fmt::layer())
.init(); .init();
// Read services from file
let services_str = read_to_string("services.toml")
.expect("Failed to read services.toml file.");
let service_vec: ServiceVec = toml::from_str::<ServiceVec>(&services_str).unwrap();
// init template engine and add templates // init template engine and add templates
let mut env = Environment::new(); let env: Environment = load_templates();
load_templates(&mut env);
// pass env & services to handlers via state // pass env & services to handlers via state
let app_state = Arc::new(AppState { env, services: service_vec.services }); let app_state = Arc::new(AppState { env, services: config.services.unwrap() });
// define routes // define routes
let app = Router::new() let app = Router::new()
@ -86,7 +104,9 @@ async fn main() {
.with_state(app_state); .with_state(app_state);
// run it // run it
let listener = tokio::net::TcpListener::bind("0.0.0.0:8080") let listener = tokio::net::TcpListener::bind(
config.ip.expect("Missing 'ip' config parameter.") +
":" + &config.port.expect("Missing 'port' config parameter.").to_string())
.await .await
.unwrap(); .unwrap();
println!("listening on {}", listener.local_addr().unwrap()); println!("listening on {}", listener.local_addr().unwrap());