diff options
| author | Dawid Rycerz <dawid@rycerz.xyz> | 2025-07-14 21:27:03 +0300 |
|---|---|---|
| committer | Dawid Rycerz <dawid@rycerz.xyz> | 2025-07-14 21:27:03 +0300 |
| commit | dbb25297da61fe393ca1e8a6b6c6beace2513e0a (patch) | |
| tree | d659a4a3465d519cc45b4501f0ac98060d227061 /src/main.rs | |
| parent | eb0c5d947a2e2755fac4a9b34d9dee6c2987afbb (diff) | |
feat: fix user_id in requests missing
Diffstat (limited to 'src/main.rs')
| -rw-r--r-- | src/main.rs | 27 |
1 files changed, 27 insertions, 0 deletions
diff --git a/src/main.rs b/src/main.rs index a387657..1cb1515 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,9 +1,11 @@ use silmataivas::app_with_state; +use silmataivas::users::{UserRepository, UserRole}; use sqlx::SqlitePool; use std::env; use std::net::SocketAddr; use std::sync::Arc; use tokio::fs; +use uuid::Uuid; #[tokio::main] async fn main() { @@ -16,6 +18,31 @@ async fn main() { let pool = SqlitePool::connect(&db_path) .await .expect("Failed to connect to DB"); + + // Create initial admin user if none exists + { + let repo = UserRepository { db: &pool }; + match repo.any_admin_exists().await { + Ok(false) => { + let admin_token = + env::var("ADMIN_TOKEN").unwrap_or_else(|_| Uuid::new_v4().to_string()); + match repo + .create_user(Some(admin_token.clone()), Some(UserRole::Admin)) + .await + { + Ok(_) => println!("Initial admin user created. Token: {admin_token}"), + Err(e) => eprintln!("Failed to create initial admin user: {e}"), + } + } + Ok(true) => { + // At least one admin exists, do nothing + } + Err(e) => { + eprintln!("Failed to check for existing admin users: {e}"); + } + } + } + let app = app_with_state(Arc::new(pool)); let addr = SocketAddr::from(([0, 0, 0, 0], 4000)); let listener = tokio::net::TcpListener::bind(addr) |
