| 1 | use steid::{ |
| 2 | application::{AppConfig, is_claimed}, |
| 3 | domain::SetupToken, |
| 4 | infrastructure::{self, repository::SqliteUserRepo, web::context::SetupState}, |
| 5 | }; |
| 6 | use topcoat::{ |
| 7 | cookie::RouterBuilderCookieExt, |
| 8 | router::{Router, RouterBuilderDiscoverExt}, |
| 9 | session::{RouterBuilderSessionExt, SessionConfig}, |
| 10 | }; |
| 11 | |
| 12 | #[tokio::main] |
| 13 | async fn main() -> Result<(), Box<dyn std::error::Error>> { |
| 14 | dotenvy::dotenv().ok(); |
| 15 | |
| 16 | let config = AppConfig::from_env()?; |
| 17 | let pool = infrastructure::database::connect(&config.database_url).await?; |
| 18 | |
| 19 | let mut builder = Router::builder() |
| 20 | .cookies() |
| 21 | .sessions(SessionConfig::default()) |
| 22 | .discover() |
| 23 | .app_context(config) |
| 24 | .app_context(pool.clone()); |
| 25 | |
| 26 | |
| 27 | |
| 28 | if !is_claimed(&SqliteUserRepo::new(pool)).await? { |
| 29 | let token = SetupToken::generate(); |
| 30 | announce_setup(&token); |
| 31 | builder = builder.app_context(SetupState(token)); |
| 32 | } |
| 33 | |
| 34 | topcoat::start(builder.build()).await?; |
| 35 | |
| 36 | Ok(()) |
| 37 | } |
| 38 | |
| 39 | |
| 40 | fn announce_setup(token: &SetupToken) { |
| 41 | println!(); |
| 42 | println!(" This steid has no owner yet. Claim it at /setup with:"); |
| 43 | println!(); |
| 44 | println!(" {}", token.reveal()); |
| 45 | println!(); |
| 46 | println!(" The token is held in memory only — restarting issues a new one."); |
| 47 | println!(); |
| 48 | } |