From dbb25297da61fe393ca1e8a6b6c6beace2513e0a Mon Sep 17 00:00:00 2001 From: Dawid Rycerz Date: Mon, 14 Jul 2025 21:27:03 +0300 Subject: feat: fix user_id in requests missing --- src/main.rs | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) (limited to 'src/main.rs') 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) -- cgit v1.2.3