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

2
.gitignore vendored
View file

@ -1,2 +1,4 @@
/target /target
.env .env
config.toml

1
Cargo.lock generated
View file

@ -413,6 +413,7 @@ dependencies = [
"rocket", "rocket",
"serde", "serde",
"sha", "sha",
"toml",
] ]
[[package]] [[package]]

View file

@ -11,3 +11,4 @@ regex = "1.9.4"
rocket = "=0.5.0-rc.3" rocket = "=0.5.0-rc.3"
serde = { version = "1.0.188", features = ["derive"] } serde = { version = "1.0.188", features = ["derive"] }
sha = "1.0.3" sha = "1.0.3"
toml = "0.7.6"

19
config.example.toml Normal file
View file

@ -0,0 +1,19 @@
[general]
# if this path is encountered during path traversal,
# it will be removed. this is useful for instances
# where your absolute domain path is not long enough
# to replace boomlings.com, because you can then point
# it at a different, longer path to fill the gap
#
# example:
# boomlings.com/database/
# example.com/aaaaaaaaaa/
# ^^^^^^^^^^^
# leaving blank will disable this
append_path = ""
# where can your server be accessible?
port = 8000
[accounts]
# allow new accounts to be created
allow_registration = true

View file

@ -34,5 +34,5 @@ _these features are implemented_
## todo ## todo
- cache hashed passwords - make the `append_path` option do something (atm doesnt work)
- our passwords are a little insecure (`argon2(sha1(password + "mI29fmAnxgTs"))`) and there isnt anything we can do about this because gpj2 is forced like that!! thanks robtop!! (try and find a fix anyway lul) - move authorization logic to (./src/helpers/accounts.rs)[./src/helpers/accounts.rs] + cache authorization

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::prelude::*;
use diesel::result::Error; use diesel::result::Error;
use crate::CONFIG;
use crate::helpers; use crate::helpers;
use crate::db; use crate::db;
@ -19,6 +20,10 @@ pub struct FormRegisterAccount {
pub fn register_account(input: Form<FormRegisterAccount>) -> status::Custom<&'static str> { pub fn register_account(input: Form<FormRegisterAccount>) -> status::Custom<&'static str> {
let connection = &mut db::establish_connection_pg(); 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()) { if input.userName != helpers::clean::clean(input.userName.as_ref()) {
return status::Custom(Status::Ok, "-4") return status::Custom(Status::Ok, "-4")
} }

View file

@ -1,4 +1,5 @@
#![feature(decl_macro)] #![feature(decl_macro)]
#![feature(lazy_cell)]
#[macro_use] extern crate rocket; #[macro_use] extern crate rocket;
@ -11,14 +12,19 @@ use helpers::*;
mod endpoints; mod endpoints;
use endpoints::*; use endpoints::*;
mod config;
use config::*;
#[get("/")] #[get("/")]
fn index() -> String { 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] #[launch]
fn rocket() -> _ { fn rocket() -> _ {
rocket::build().mount("/", routes![ rocket::build()
.configure(rocket::Config::figment().merge(("port", CONFIG.general.port)))
.mount("/", routes![
index, index,
endpoints::accounts::login_account::login_account, endpoints::accounts::login_account::login_account,