From 2500fdc79bd6bc2eddb310e82ef5643778f1a8aa Mon Sep 17 00:00:00 2001 From: AINDUSTRIES Date: Mon, 6 Oct 2025 22:32:59 +0200 Subject: [PATCH] Added configuration file --- Cargo.toml | 1 + config.toml | 5 +++++ src/main.rs | 56 +++++++++++++++++++++++++++++++++++++++++------------ 3 files changed, 50 insertions(+), 12 deletions(-) create mode 100644 config.toml diff --git a/Cargo.toml b/Cargo.toml index aadad43..73cd65e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -7,5 +7,6 @@ edition = "2024" actix-web = {version = "4.11.0"} serde = "1.0.228" serde_json = "1.0.145" +toml = "0.9.7" sqlx = {version = "0.8.6", features = ["postgres"]} futures-util = "0.3.31" \ No newline at end of file diff --git a/config.toml b/config.toml new file mode 100644 index 0000000..3596b76 --- /dev/null +++ b/config.toml @@ -0,0 +1,5 @@ +[application] +# Change with actual location of assets +assets="" +# Set bind address +bind="127.0.0.1:8080" \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index f65aba0..dcb548f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,25 +1,57 @@ mod html; -mod pages; mod middleware; +mod pages; -use actix_web::{web, App, HttpServer}; +use actix_web::{App, HttpServer, web}; +use serde::Deserialize; +use std::fs::File; +use std::io::Read; +use toml::from_slice; -use pages::{files::file, index, projects::{projects, project}}; use middleware::MimeType; +use pages::{ + files::file, + index, + projects::{project, projects}, +}; + +#[derive(Deserialize)] +struct Config { + server: ServerConfig, +} + +#[derive(Deserialize)] +struct ServerConfig { + assets: String, + bind: String, +} + +struct AppState { + assets: String, +} #[actix_web::main] async fn main() { - let bind_address = "0.0.0.0:8080"; - if let Ok(server) = - HttpServer::new(|| App::new() - .service(file) - .service(web::scope("") - .wrap(MimeType::new("text/html".to_string())) + let mut config_file = + File::open("/etc/aindustries-be/config.toml").expect("Could not open config file"); + let mut buf = vec![]; + config_file + .read_to_end(&mut buf) + .expect("Could not read config file"); + let config: Config = from_slice(&buf).expect("Could not parse config"); + let bind_address = config.server.bind; + let assets = config.server.assets; + let data = web::Data::new(AppState { assets }); + if let Ok(server) = HttpServer::new(move || { + App::new().app_data(data.clone()).service(file).service( + web::scope("") + .wrap(MimeType::new("text/html")) .service(index) .service(projects) - .service(project) - ) - ).bind(bind_address) + .service(project), + ) + }) + .bind(bind_address) { match server.run().await { Ok(_) => {}