basic config system

This commit is contained in:
Reid 2023-08-28 16:30:10 -07:00
parent 77ad597963
commit fa1d23cb3e
Signed by: reidlab
GPG key ID: 6C9EAA3364F962C8
8 changed files with 78 additions and 10 deletions

34
src/config.rs Normal file
View file

@ -0,0 +1,34 @@
use serde::Deserialize;
use std::fs;
use std::sync::LazyLock;
#[derive(Deserialize)]
pub struct Config {
pub general: ConfigGeneral,
pub accounts: ConfigAccounts
}
#[derive(Deserialize)]
pub struct ConfigGeneral {
pub append_path: String,
pub port: u16
}
#[derive(Deserialize)]
pub struct ConfigAccounts {
pub allow_registration: bool
}
impl Config {
pub fn load_from_file(file_path: &str) -> Self {
let toml_str = fs::read_to_string(file_path).expect("Error finding toml config:");
let config: Config = toml::from_str(toml_str.as_str()).expect("Error parsing toml config:");
return config;
}
}
pub static CONFIG: LazyLock<Config> = LazyLock::new(|| {
let config = Config::load_from_file("config.toml");
config
});

View file

@ -5,6 +5,7 @@ use rocket::response::status;
use diesel::prelude::*;
use diesel::result::Error;
use crate::CONFIG;
use crate::helpers;
use crate::db;
@ -18,6 +19,10 @@ pub struct FormRegisterAccount {
#[post("/memaddrefix/accounts/registerGJAccount.php", data = "<input>")]
pub fn register_account(input: Form<FormRegisterAccount>) -> status::Custom<&'static str> {
let connection = &mut db::establish_connection_pg();
if CONFIG.accounts.allow_registration == false {
return status::Custom(Status::Ok, "-1")
}
if input.userName != helpers::clean::clean(input.userName.as_ref()) {
return status::Custom(Status::Ok, "-4")

View file

@ -1,4 +1,5 @@
#![feature(decl_macro)]
#![feature(lazy_cell)]
#[macro_use] extern crate rocket;
@ -11,17 +12,22 @@ use helpers::*;
mod endpoints;
use endpoints::*;
mod config;
use config::*;
#[get("/")]
fn index() -> String {
return String::from("index | coming soon to a localhost:8000 near u");
return String::from("gdps-server | https://git.reidlab.online/reidlab/gdps-server");
}
#[launch]
fn rocket() -> _ {
rocket::build().mount("/", routes![
index,
endpoints::accounts::login_account::login_account,
endpoints::accounts::register_account::register_account
])
rocket::build()
.configure(rocket::Config::figment().merge(("port", CONFIG.general.port)))
.mount("/", routes![
index,
endpoints::accounts::login_account::login_account,
endpoints::accounts::register_account::register_account
])
}