Improve config system
Add pkl to generate configs
This commit is contained in:
parent
8785186dfa
commit
b2ff632e64
47 changed files with 916 additions and 277 deletions
emgauwa-core
|
@ -24,4 +24,3 @@ serde_derive = "1.0"
|
|||
sqlx = { version = "0.7", features = ["sqlite", "runtime-tokio", "macros", "chrono"] }
|
||||
|
||||
futures = "0.3.29"
|
||||
libc = "0.2"
|
||||
|
|
|
@ -1,3 +1,3 @@
|
|||
fn main() {
|
||||
println!("cargo:rustc-env=DATABASE_URL=sqlite://emgauwa-core.sqlite")
|
||||
println!("cargo:rustc-env=DATABASE_URL=sqlite://_local/emgauwa-core.sqlite")
|
||||
}
|
||||
|
|
|
@ -6,10 +6,9 @@ use actix_web::middleware::TrailingSlash;
|
|||
use actix_web::{middleware, web, App, HttpServer};
|
||||
use emgauwa_lib::db::DbController;
|
||||
use emgauwa_lib::errors::EmgauwaError;
|
||||
use emgauwa_lib::utils::init_logging;
|
||||
use emgauwa_lib::utils::{drop_privileges, init_logging};
|
||||
|
||||
use crate::app_state::AppState;
|
||||
use crate::utils::drop_privileges;
|
||||
|
||||
mod app_state;
|
||||
mod handlers;
|
||||
|
@ -19,12 +18,12 @@ mod utils;
|
|||
#[actix_web::main]
|
||||
async fn main() -> Result<(), std::io::Error> {
|
||||
let settings = settings::init()?;
|
||||
|
||||
let listener = TcpListener::bind(format!("{}:{}", settings.server.host, settings.server.port))?;
|
||||
drop_privileges(&settings.permissions)?;
|
||||
|
||||
init_logging(&settings.logging.level)?;
|
||||
|
||||
let listener = TcpListener::bind(format!("{}:{}", settings.host, settings.port))?;
|
||||
|
||||
drop_privileges(&settings)?;
|
||||
|
||||
let pool = emgauwa_lib::db::init(&settings.database).await?;
|
||||
|
||||
let mut conn = pool.acquire().await.map_err(EmgauwaError::from)?;
|
||||
|
@ -35,7 +34,11 @@ async fn main() -> Result<(), std::io::Error> {
|
|||
|
||||
let app_state = AppState::new(pool.clone()).start();
|
||||
|
||||
log::info!("Starting server on {}:{}", settings.host, settings.port);
|
||||
log::info!(
|
||||
"Starting server on {}:{}",
|
||||
settings.server.host,
|
||||
settings.server.port
|
||||
);
|
||||
|
||||
HttpServer::new(move || {
|
||||
let cors = Cors::default().allow_any_method().allow_any_header();
|
||||
|
|
|
@ -1,57 +1,32 @@
|
|||
use emgauwa_lib::errors::EmgauwaError;
|
||||
use emgauwa_lib::{constants, utils};
|
||||
use emgauwa_lib::settings;
|
||||
use serde_derive::Deserialize;
|
||||
|
||||
#[derive(Clone, Debug, Deserialize)]
|
||||
#[serde(default)]
|
||||
#[allow(unused)]
|
||||
pub struct Logging {
|
||||
pub level: String,
|
||||
pub file: String,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Deserialize)]
|
||||
#[serde(default)]
|
||||
#[allow(unused)]
|
||||
pub struct Settings {
|
||||
pub server: settings::Server,
|
||||
pub database: String,
|
||||
pub permissions: settings::Permissions,
|
||||
pub logging: settings::Logging,
|
||||
|
||||
pub host: String,
|
||||
pub port: u16,
|
||||
pub origins: Vec<String>,
|
||||
|
||||
pub user: String,
|
||||
pub group: String,
|
||||
|
||||
pub logging: Logging,
|
||||
}
|
||||
|
||||
impl Default for Settings {
|
||||
fn default() -> Self {
|
||||
Settings {
|
||||
database: String::from("sqlite://emgauwa-core.sqlite"),
|
||||
server: settings::Server::default(),
|
||||
database: String::from("sqlite://_local/emgauwa-core.sqlite"),
|
||||
permissions: settings::Permissions::default(),
|
||||
logging: settings::Logging::default(),
|
||||
|
||||
host: String::from("127.0.0.1"),
|
||||
port: constants::DEFAULT_PORT,
|
||||
origins: Vec::new(),
|
||||
|
||||
user: String::from(""),
|
||||
group: String::from(""),
|
||||
|
||||
logging: Logging::default(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for Logging {
|
||||
fn default() -> Self {
|
||||
Logging {
|
||||
level: String::from("info"),
|
||||
file: String::from("stdout"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn init() -> Result<Settings, EmgauwaError> {
|
||||
utils::load_settings("core", "CORE")
|
||||
settings::load("core", "CORE")
|
||||
}
|
||||
|
|
|
@ -1,8 +1,3 @@
|
|||
use std::ffi::CString;
|
||||
use std::io::{Error, ErrorKind};
|
||||
|
||||
use crate::settings::Settings;
|
||||
|
||||
pub fn flatten_result<T, E>(res: Result<Result<T, E>, E>) -> Result<T, E> {
|
||||
match res {
|
||||
Ok(Ok(t)) => Ok(t),
|
||||
|
@ -10,64 +5,3 @@ pub fn flatten_result<T, E>(res: Result<Result<T, E>, E>) -> Result<T, E> {
|
|||
Err(e) => Err(e),
|
||||
}
|
||||
}
|
||||
|
||||
// https://blog.lxsang.me/post/id/28.0
|
||||
pub fn drop_privileges(settings: &Settings) -> Result<(), Error> {
|
||||
log::info!(
|
||||
"Dropping privileges to {}:{}",
|
||||
settings.user,
|
||||
settings.group
|
||||
);
|
||||
|
||||
// the group id need to be set first, because, when the user privileges drop,
|
||||
// we are unable to drop the group privileges
|
||||
if !settings.group.is_empty() {
|
||||
drop_privileges_group(&settings.group)?;
|
||||
}
|
||||
if !settings.user.is_empty() {
|
||||
drop_privileges_user(&settings.user)?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn drop_privileges_group(group: &str) -> Result<(), Error> {
|
||||
// get the gid from username
|
||||
if let Ok(cstr) = CString::new(group.as_bytes()) {
|
||||
let p = unsafe { libc::getgrnam(cstr.as_ptr()) };
|
||||
if p.is_null() {
|
||||
log::error!("Unable to getgrnam of group: {}", group);
|
||||
return Err(Error::last_os_error());
|
||||
}
|
||||
if unsafe { libc::setgid((*p).gr_gid) } != 0 {
|
||||
log::error!("Unable to setgid of group: {}", group);
|
||||
return Err(Error::last_os_error());
|
||||
}
|
||||
} else {
|
||||
return Err(Error::new(
|
||||
ErrorKind::Other,
|
||||
"Cannot create CString from String (group)!",
|
||||
));
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn drop_privileges_user(user: &str) -> Result<(), Error> {
|
||||
// get the uid from username
|
||||
if let Ok(cstr) = CString::new(user.as_bytes()) {
|
||||
let p = unsafe { libc::getpwnam(cstr.as_ptr()) };
|
||||
if p.is_null() {
|
||||
log::error!("Unable to getpwnam of user: {}", user);
|
||||
return Err(Error::last_os_error());
|
||||
}
|
||||
if unsafe { libc::setuid((*p).pw_uid) } != 0 {
|
||||
log::error!("Unable to setuid of user: {}", user);
|
||||
return Err(Error::last_os_error());
|
||||
}
|
||||
} else {
|
||||
return Err(Error::new(
|
||||
ErrorKind::Other,
|
||||
"Cannot create CString from String (user)!",
|
||||
));
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue