Add settings and constants

This commit is contained in:
Tobias Reisinger 2023-11-23 03:36:14 +01:00
parent e419923939
commit ee68405874
Signed by: serguzim
GPG key ID: 13AD60C237A28DFE
6 changed files with 26 additions and 8 deletions

View file

@ -1,6 +1,15 @@
use config::Config; use config::Config;
use emgauwa_lib::constants;
use serde_derive::Deserialize; use serde_derive::Deserialize;
#[derive(Clone, Debug, Deserialize)]
#[serde(default)]
#[allow(unused)]
pub struct Core {
pub host: String,
pub port: u16,
}
#[derive(Clone, Debug, Deserialize)] #[derive(Clone, Debug, Deserialize)]
#[serde(default)] #[serde(default)]
#[allow(unused)] #[allow(unused)]
@ -14,8 +23,6 @@ pub struct Logging {
#[allow(unused)] #[allow(unused)]
pub struct Settings { pub struct Settings {
pub database: String, pub database: String,
pub port: u16,
pub host: String,
pub logging: Logging, pub logging: Logging,
} }
@ -23,13 +30,20 @@ impl Default for Settings {
fn default() -> Self { fn default() -> Self {
Settings { Settings {
database: String::from("sqlite://emgauwa-controller.sqlite"), database: String::from("sqlite://emgauwa-controller.sqlite"),
port: 5000,
host: String::from("127.0.0.1"),
logging: Logging::default(), logging: Logging::default(),
} }
} }
} }
impl Default for Core {
fn default() -> Self {
Core {
host: String::from("127.0.0.1"),
port: constants::DEFAULT_PORT,
}
}
}
impl Default for Logging { impl Default for Logging {
fn default() -> Self { fn default() -> Self {
Logging { Logging {

View file

@ -1,4 +1,4 @@
port = 5000 port = 4419
host = "127.0.0.1" host = "127.0.0.1"
database = "sqlite://emgauwa-core.sqlite" database = "sqlite://emgauwa-core.sqlite"

View file

@ -23,6 +23,7 @@ async fn main() -> std::io::Result<()> {
let pool = emgauwa_lib::db::init(&settings.database).await; let pool = emgauwa_lib::db::init(&settings.database).await;
log::info!("Starting server on {}:{}", settings.host, settings.port);
HttpServer::new(move || { HttpServer::new(move || {
App::new() App::new()
.wrap( .wrap(

View file

@ -1,4 +1,5 @@
use config::Config; use config::Config;
use emgauwa_lib::constants;
use serde_derive::Deserialize; use serde_derive::Deserialize;
#[derive(Clone, Debug, Deserialize)] #[derive(Clone, Debug, Deserialize)]
@ -23,7 +24,7 @@ impl Default for Settings {
fn default() -> Self { fn default() -> Self {
Settings { Settings {
database: String::from("sqlite://emgauwa-core.sqlite"), database: String::from("sqlite://emgauwa-core.sqlite"),
port: 5000, port: constants::DEFAULT_PORT,
host: String::from("127.0.0.1"), host: String::from("127.0.0.1"),
logging: Logging::default(), logging: Logging::default(),
} }
@ -44,11 +45,11 @@ pub fn init() -> Settings {
.add_source(config::File::with_name("emgauwa-core")) .add_source(config::File::with_name("emgauwa-core"))
.add_source( .add_source(
config::Environment::with_prefix("EMGAUWA_CORE") config::Environment::with_prefix("EMGAUWA_CORE")
.prefix_separator("_") .prefix_separator("__")
.separator("__"), .separator("__"),
) )
.build() .build()
.unwrap() .unwrap()
.try_deserialize::<Settings>() .try_deserialize::<Settings>()
.unwrap_or_else(|_| panic!("Error reading settings.")) .expect("Error reading settings.")
} }

View file

@ -0,0 +1 @@
pub const DEFAULT_PORT: u16 = 4419;

View file

@ -1,3 +1,4 @@
pub mod constants;
pub mod db; pub mod db;
pub mod handlers; pub mod handlers;
pub mod return_models; pub mod return_models;