Added configuration file

This commit is contained in:
2025-10-06 22:32:59 +02:00
parent 99ebee7f35
commit 2500fdc79b
3 changed files with 50 additions and 12 deletions

View File

@@ -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(_) => {}