feat: load config from proper location
also adds a way to change load location using cmdline arguments
This commit is contained in:
parent
7eea0cd4fe
commit
026d48c6e7
3 changed files with 15 additions and 6 deletions
|
@ -11,6 +11,7 @@ codegen-units = 1
|
||||||
[dependencies]
|
[dependencies]
|
||||||
actix-web = "4.10"
|
actix-web = "4.10"
|
||||||
argon2 = { version = "0.5.3", features = ["std"] }
|
argon2 = { version = "0.5.3", features = ["std"] }
|
||||||
|
clap = { version = "4.5.37", features = ["derive"] }
|
||||||
futures = "0.3"
|
futures = "0.3"
|
||||||
regex = "1.11"
|
regex = "1.11"
|
||||||
serde = { version = "1.0", features = ["derive"] }
|
serde = { version = "1.0", features = ["derive"] }
|
||||||
|
|
|
@ -26,8 +26,8 @@ struct WebBuilder {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ConfigBuilder {
|
impl ConfigBuilder {
|
||||||
pub async fn load() -> Result<Self, Error> {
|
pub async fn load(path: String) -> Result<Self, Error> {
|
||||||
let raw = read_to_string("./config.toml").await?;
|
let raw = read_to_string(path).await?;
|
||||||
|
|
||||||
let config = toml::from_str(&raw)?;
|
let config = toml::from_str(&raw)?;
|
||||||
|
|
||||||
|
@ -39,13 +39,11 @@ impl ConfigBuilder {
|
||||||
Web {
|
Web {
|
||||||
url: web.url.unwrap_or(String::from("0.0.0.0")),
|
url: web.url.unwrap_or(String::from("0.0.0.0")),
|
||||||
port: web.port.unwrap_or(8080),
|
port: web.port.unwrap_or(8080),
|
||||||
ssl: web.ssl.unwrap_or_default(),
|
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
Web {
|
Web {
|
||||||
url: String::from("0.0.0.0"),
|
url: String::from("0.0.0.0"),
|
||||||
port: 8080,
|
port: 8080,
|
||||||
ssl: false,
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -66,7 +64,6 @@ pub struct Config {
|
||||||
pub struct Web {
|
pub struct Web {
|
||||||
pub url: String,
|
pub url: String,
|
||||||
pub port: u16,
|
pub port: u16,
|
||||||
pub ssl: bool,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Database {
|
impl Database {
|
||||||
|
|
13
src/main.rs
13
src/main.rs
|
@ -1,5 +1,6 @@
|
||||||
use actix_web::{App, HttpServer, web};
|
use actix_web::{App, HttpServer, web};
|
||||||
use argon2::Argon2;
|
use argon2::Argon2;
|
||||||
|
use clap::Parser;
|
||||||
use sqlx::{PgPool, Pool, Postgres};
|
use sqlx::{PgPool, Pool, Postgres};
|
||||||
use std::time::SystemTime;
|
use std::time::SystemTime;
|
||||||
mod config;
|
mod config;
|
||||||
|
@ -8,6 +9,14 @@ mod api;
|
||||||
|
|
||||||
type Error = Box<dyn std::error::Error>;
|
type Error = Box<dyn std::error::Error>;
|
||||||
|
|
||||||
|
/// Simple program to greet a person
|
||||||
|
#[derive(Parser, Debug)]
|
||||||
|
#[command(version, about, long_about = None)]
|
||||||
|
struct Args {
|
||||||
|
#[arg(short, long, default_value_t = String::from("/etc/gorb/config.toml"))]
|
||||||
|
config: String,
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
struct Data {
|
struct Data {
|
||||||
pub pool: Pool<Postgres>,
|
pub pool: Pool<Postgres>,
|
||||||
|
@ -18,7 +27,9 @@ struct Data {
|
||||||
|
|
||||||
#[tokio::main]
|
#[tokio::main]
|
||||||
async fn main() -> Result<(), Error> {
|
async fn main() -> Result<(), Error> {
|
||||||
let config = ConfigBuilder::load().await?.build();
|
let args = Args::parse();
|
||||||
|
|
||||||
|
let config = ConfigBuilder::load(args.config).await?.build();
|
||||||
|
|
||||||
let web = config.web.clone();
|
let web = config.web.clone();
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue