diff options
author | curly <curly@infernal.garden> | 2024-07-17 19:25:00 -0600 |
---|---|---|
committer | curly <curly@infernal.garden> | 2024-07-17 19:25:00 -0600 |
commit | 19979216185894d99a108adcc8e3d356a5979dd4 (patch) | |
tree | 154306a2bf5ccc00fd669c5a83a9c06225f42b27 /src | |
parent | 5c95f38610c61f43700b3e78dcb56c552d152c2b (diff) | |
download | poko_server-19979216185894d99a108adcc8e3d356a5979dd4.tar.gz poko_server-19979216185894d99a108adcc8e3d356a5979dd4.tar.bz2 poko_server-19979216185894d99a108adcc8e3d356a5979dd4.zip |
config
Diffstat (limited to 'src')
-rw-r--r-- | src/db.rs | 60 | ||||
-rw-r--r-- | src/main.rs | 30 |
2 files changed, 78 insertions, 12 deletions
@@ -1,6 +1,8 @@ use serde::{Serialize, Deserialize}; use sha2::{Digest, Sha256}; use std::collections::HashMap; +use std::io::ErrorKind; +use std::net::{IpAddr, Ipv4Addr}; use std::path::PathBuf; use std::{fs::File, io::Write}; @@ -9,11 +11,40 @@ use crate::uid::{self, UID}; #[derive(Serialize, Deserialize)] pub struct Config { dbsave: PathBuf, - realm: String, + address: IpAddr, + port: u16, } impl Config { pub fn new() -> Config { - Config { dbsave: "db.json".into(), realm: "localhost".into() } + Config { dbsave: "db.json".into(), address: IpAddr::V4(Ipv4Addr::new(0,0,0,0)), port: 9050} + } + + pub fn load(path: PathBuf) -> Result<Config, ()> { + match File::open(path) { + Ok(n) => { + match serde_json::from_reader(n) { + Ok(n) => Ok(n), + Err(_) => Err(()) + } + }, + Err(n) => match n.kind() { + ErrorKind::PermissionDenied => panic!("Could not open config file"), + _ => Err(()) + } + } + } + + pub fn genconfig() { + let f = File::create("config.json").unwrap(); + let config = Config::new(); + serde_json::to_writer(f, &config).unwrap(); + } + + pub fn port(&self) -> u16 { + self.port + } + pub fn address(&self) -> IpAddr { + self.address } } @@ -162,20 +193,26 @@ impl User { } } +fn dbsave_default() -> PathBuf { + "".into() +} + #[derive(Serialize, Deserialize)] pub struct DB { pub uid_generator: uid::Generator, users: Vec<User>, - config: Config, + #[serde(skip_serializing)] + #[serde(default = "dbsave_default")] + dbsave: PathBuf, registration_keys: Vec<String>, } impl DB { async fn save(&self) -> Result<(), String> { - let mut f = match File::create(&self.config.dbsave) { + let mut f = match File::create(&self.dbsave) { Ok(n) => n, Err(n) => { match n.kind() { - std::io::ErrorKind::PermissionDenied => panic!("{:?}: Permission Denied", &self.config.dbsave), + std::io::ErrorKind::PermissionDenied => panic!("{:?}: Permission Denied", &self.dbsave), n => return Err(n.to_string()), } }, @@ -189,11 +226,14 @@ impl DB { Err(n) => return Err(n.to_string()), }; } - pub fn load(c: Config) -> Self { + pub fn load(c: &Config) -> Self { match File::open(&c.dbsave) { Ok(n) => { - match serde_json::from_reader(n) { - Ok(n) => n, + match serde_json::from_reader::<std::fs::File, DB>(n) { + Ok(mut n) => { + n.dbsave = c.dbsave.clone(); + n + }, Err(n) => panic!("{}", n), } }, @@ -203,8 +243,8 @@ impl DB { }, } } - pub fn new(config: Config) -> Self { - DB { uid_generator: uid::Generator::new(), users: vec![], config, registration_keys: vec!["ADMIN".into()] } + pub fn new(config: &Config) -> Self { + DB { uid_generator: uid::Generator::new(), dbsave: config.dbsave.clone(), users: vec![], registration_keys: vec!["ADMIN".into()] } } pub fn get_user(&self, id: UID) -> Result<&User, String> { diff --git a/src/main.rs b/src/main.rs index 3ad9efd..6135e97 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,6 +1,6 @@ #[macro_use] extern crate rocket; -use std::collections::HashSet; +use std::{collections::HashSet, process::exit}; use rocket::{http::Status, serde::json::Json, State}; use rocket_cors::CorsOptions; @@ -292,7 +292,33 @@ fn rocket() -> _ { }; let cors = cors.to_cors().unwrap(); - rocket::build().manage(Mutex::new(DB::load(Config::new()))) + let mut config_path = "config.json".to_string(); + + let args: Vec<String> = std::env::args().collect(); + for a in args { + let b: Vec<&str> = a.split('=').collect(); + match b[0] { + "config" => { + config_path = b[1].to_string(); + }, + "genconfig" => { + Config::genconfig(); + exit(0); + } + _ => () + } + } + + let config = match Config::load(config_path.into()) { + Ok(n) => n, + Err(_) => Config::new(), + }; + + let rocket_config = rocket::config::Config::figment() + .merge(("address", config.address())) + .merge(("port", config.port())); + + rocket::custom(rocket_config).manage(Mutex::new(DB::load(&config))) .mount("/", routes![index]) .mount("/user", routes![login, get_users_by_name, get_user_authenticated, get_user, new_user, get_all_users, logout, logout_all, get_sessions, delete]) .mount("/user/update", routes![update_name, update_password]) |