blob: 1cb1515d7c3f1a55b55186ab8670aa9f4a80c679 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
|
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() {
// Set up database path
let db_path =
env::var("DATABASE_URL").unwrap_or_else(|_| "sqlite://./data/silmataivas.db".to_string());
// Ensure data directory exists
let _ = fs::create_dir_all("./data").await;
// Connect to SQLite
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)
.await
.expect("Failed to bind address");
println!("Listening on {}", listener.local_addr().unwrap());
axum::serve(listener, app).await.unwrap();
}
|