basic config system
This commit is contained in:
parent
77ad597963
commit
fa1d23cb3e
8 changed files with 78 additions and 10 deletions
34
src/config.rs
Normal file
34
src/config.rs
Normal 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
|
||||
});
|
|
@ -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")
|
||||
|
|
20
src/main.rs
20
src/main.rs
|
@ -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
|
||||
])
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue