Added command line argument for the config file location
This commit is contained in:
@@ -10,3 +10,4 @@ serde_json = "1.0.145"
|
|||||||
toml = "0.9.7"
|
toml = "0.9.7"
|
||||||
sqlx = {version = "0.8.6", features = ["postgres"]}
|
sqlx = {version = "0.8.6", features = ["postgres"]}
|
||||||
futures-util = "0.3.31"
|
futures-util = "0.3.31"
|
||||||
|
clap = { version = "4.5.50", features = ["derive"] }
|
||||||
39
src/main.rs
39
src/main.rs
@@ -3,9 +3,11 @@ mod middleware;
|
|||||||
mod pages;
|
mod pages;
|
||||||
|
|
||||||
use actix_web::{App, HttpServer, web};
|
use actix_web::{App, HttpServer, web};
|
||||||
|
use clap::Parser;
|
||||||
use serde::Deserialize;
|
use serde::Deserialize;
|
||||||
use std::fs::File;
|
use std::fs::File;
|
||||||
use std::io::Read;
|
use std::io::Read;
|
||||||
|
use std::process::exit;
|
||||||
use toml::from_slice;
|
use toml::from_slice;
|
||||||
|
|
||||||
use middleware::MimeType;
|
use middleware::MimeType;
|
||||||
@@ -15,6 +17,13 @@ use pages::{
|
|||||||
projects::{project, projects},
|
projects::{project, projects},
|
||||||
};
|
};
|
||||||
|
|
||||||
|
#[derive(Parser)]
|
||||||
|
#[command(name = "aindustries-be", version, about = "This is the amazing webserver for aindustries.be!", long_about = None)]
|
||||||
|
struct Cli {
|
||||||
|
#[arg(short, long, value_name = "CONFIG", help = "Path to config file, defaults to /etc/aindustries-be/config.toml")]
|
||||||
|
config: Option<String>,
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Deserialize)]
|
#[derive(Deserialize)]
|
||||||
struct Config {
|
struct Config {
|
||||||
server: ServerConfig,
|
server: ServerConfig,
|
||||||
@@ -32,13 +41,29 @@ struct AppState {
|
|||||||
|
|
||||||
#[actix_web::main]
|
#[actix_web::main]
|
||||||
async fn main() {
|
async fn main() {
|
||||||
let mut config_file =
|
let cli = Cli::parse();
|
||||||
File::open("/etc/aindustries-be/config.toml").expect("Could not open config file");
|
let config_file_path = cli
|
||||||
|
.config
|
||||||
|
.unwrap_or("/etc/aindustries-be/config.toml".to_string());
|
||||||
|
let mut config_file = match File::open(&config_file_path) {
|
||||||
|
Ok(config_file) => config_file,
|
||||||
|
Err(e) => {
|
||||||
|
eprintln!("Could not open config file ({config_file_path}): {e}");
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
};
|
||||||
let mut buf = vec![];
|
let mut buf = vec![];
|
||||||
config_file
|
if let Err(e) = config_file.read_to_end(&mut buf) {
|
||||||
.read_to_end(&mut buf)
|
eprintln!("Could not read config file: {e}");
|
||||||
.expect("Could not read config file");
|
exit(1);
|
||||||
let config: Config = from_slice(&buf).expect("Could not parse config");
|
}
|
||||||
|
let config: Config = match from_slice(&buf) {
|
||||||
|
Ok(config) => config,
|
||||||
|
Err(e) => {
|
||||||
|
eprintln!("Could not parse config file {e}");
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
|
};
|
||||||
let bind_address = config.server.bind;
|
let bind_address = config.server.bind;
|
||||||
let assets = config.server.assets;
|
let assets = config.server.assets;
|
||||||
let data = web::Data::new(AppState { assets });
|
let data = web::Data::new(AppState { assets });
|
||||||
@@ -56,7 +81,7 @@ async fn main() {
|
|||||||
match server.run().await {
|
match server.run().await {
|
||||||
Ok(_) => {}
|
Ok(_) => {}
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
eprintln!("An error occurred: {}", e);
|
eprintln!("An error occurred: {e}");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user