summaryrefslogtreecommitdiff
path: root/src/main.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/main.rs')
-rw-r--r--src/main.rs27
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)