Read config (ip/port) from file.
This commit is contained in:
parent
eb5ab35d50
commit
ae6ab53520
2 changed files with 33 additions and 11 deletions
2
config.toml
Normal file
2
config.toml
Normal file
|
@ -0,0 +1,2 @@
|
|||
ip = "0.0.0.0"
|
||||
port = 8080
|
42
src/main.rs
42
src/main.rs
|
@ -18,12 +18,19 @@ struct AppState {
|
|||
services: Vec<Service>,
|
||||
}
|
||||
|
||||
#[derive(Deserialize, Debug)]
|
||||
struct Config {
|
||||
ip: Option<String>,
|
||||
port: Option<u16>,
|
||||
services: Option<Vec<Service>>,
|
||||
}
|
||||
|
||||
#[derive(Deserialize)]
|
||||
struct ServiceVec {
|
||||
services: Vec<Service>,
|
||||
}
|
||||
|
||||
#[derive(Deserialize, Serialize)]
|
||||
#[derive(Deserialize, Serialize, Debug)]
|
||||
struct Service {
|
||||
name: String,
|
||||
description: String,
|
||||
|
@ -33,8 +40,21 @@ struct Service {
|
|||
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
|
||||
fn load_templates(env: &mut Environment) {
|
||||
fn load_templates() -> Environment<'static> {
|
||||
let mut env = Environment::new();
|
||||
let cfg = minify_html::Cfg::spec_compliant();
|
||||
|
||||
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();
|
||||
}
|
||||
|
||||
return env;
|
||||
}
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() {
|
||||
let config: Config = load_config();
|
||||
|
||||
tracing_subscriber::registry()
|
||||
.with(
|
||||
tracing_subscriber::EnvFilter::try_from_default_env().unwrap_or_else(|_| {
|
||||
|
@ -61,17 +85,11 @@ async fn main() {
|
|||
.with(tracing_subscriber::fmt::layer())
|
||||
.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
|
||||
let mut env = Environment::new();
|
||||
load_templates(&mut env);
|
||||
let env: Environment = load_templates();
|
||||
|
||||
// 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
|
||||
let app = Router::new()
|
||||
|
@ -86,7 +104,9 @@ async fn main() {
|
|||
.with_state(app_state);
|
||||
|
||||
// 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
|
||||
.unwrap();
|
||||
println!("listening on {}", listener.local_addr().unwrap());
|
||||
|
|
Loading…
Reference in a new issue