Add default user

This commit is contained in:
Andras Schmelczer 2024-12-08 17:16:27 +00:00
parent 6a002561a4
commit af7f6dd8f3
No known key found for this signature in database
GPG key ID: FC8F2C3D3D1A718C
2 changed files with 29 additions and 5 deletions

View file

@ -21,3 +21,4 @@ sqlx = { version = "0.8.2", features = ["sqlite", "runtime-tokio", "uuid", "chro
chrono = { version = "0.4.38", features = ["serde"] }
aide = { version = "0.13.4", features = ["axum", "axum-ws", "scalar"] }
schemars = { version = "0.8.21", features = ["chrono", "uuid1"] }
rand = "0.8.5"

View file

@ -1,13 +1,36 @@
use rand::distributions::Alphanumeric;
use rand::{thread_rng, Rng};
use serde::{Deserialize, Serialize};
#[derive(Debug, Deserialize, Serialize, Clone, Default)]
#[derive(Debug, Deserialize, Serialize, Clone)]
pub struct UserConfig {
#[serde(default = "Vec::new")]
pub users: Vec<User>,
#[serde(default = "default_users")]
pub user_tokens: Vec<User>,
}
#[derive(Debug, Deserialize, Serialize, Clone)]
pub struct User {
pub name: String,
pub token: String,
}
impl Default for UserConfig {
fn default() -> Self {
UserConfig {
user_tokens: default_users(),
}
}
}
fn default_users() -> Vec<User> {
vec![User {
name: "admin".to_string(),
token: get_random_token(),
}]
}
pub fn get_random_token() -> String {
thread_rng()
.sample_iter(&Alphanumeric)
.take(64)
.map(char::from)
.collect()
}