| 1 | use steid::{ |
| 2 | application::{AppConfig, is_claimed, port::GitStorage, sweep_interrupted_imports}, |
| 3 | domain::SetupToken, |
| 4 | infrastructure::{ |
| 5 | self, |
| 6 | git::DiskGitStorage, |
| 7 | repository::{SqliteRepoRepo, SqliteUserRepo}, |
| 8 | web::{ |
| 9 | context::SetupState, rate_limit::RateLimiter, session_cookie::InsecureCookieTokenStore, |
| 10 | }, |
| 11 | }, |
| 12 | }; |
| 13 | use topcoat::{ |
| 14 | asset::{AssetBundle, RouterBuilderAssetExt}, |
| 15 | cookie::RouterBuilderCookieExt, |
| 16 | router::{Router, RouterBuilderDiscoverExt}, |
| 17 | session::{RouterBuilderSessionExt, SessionConfig}, |
| 18 | }; |
| 19 | |
| 20 | #[tokio::main] |
| 21 | async fn main() -> Result<(), Box<dyn std::error::Error>> { |
| 22 | dotenvy::dotenv().ok(); |
| 23 | |
| 24 | let mut config = AppConfig::from_env()?; |
| 25 | let pool = infrastructure::database::connect(&config.database_url).await?; |
| 26 | |
| 27 | |
| 28 | |
| 29 | |
| 30 | |
| 31 | match sweep_interrupted_imports(&SqliteRepoRepo::new(pool.clone())).await { |
| 32 | Ok(0) => {} |
| 33 | Ok(swept) => println!("steid: {swept} interrupted import(s) marked as failed"), |
| 34 | |
| 35 | |
| 36 | Err(error) => eprintln!("steid: interrupted imports could not be swept: {error}"), |
| 37 | } |
| 38 | |
| 39 | |
| 40 | |
| 41 | |
| 42 | |
| 43 | |
| 44 | |
| 45 | |
| 46 | |
| 47 | |
| 48 | |
| 49 | match DiskGitStorage::new(config.data_dir.clone()) |
| 50 | .enable_reflogs() |
| 51 | .await |
| 52 | { |
| 53 | Ok(0) => {} |
| 54 | Ok(fixed) => println!("steid: reflog enabled on {fixed} bare repository/repositories"), |
| 55 | |
| 56 | |
| 57 | Err(error) => eprintln!("steid: reflogs could not be swept: {error}"), |
| 58 | } |
| 59 | |
| 60 | |
| 61 | |
| 62 | |
| 63 | if config.allow_private_webhooks { |
| 64 | eprintln!(); |
| 65 | eprintln!(" !! STEID_ALLOW_PRIVATE_WEBHOOKS is on: a webhook may point inside"); |
| 66 | eprintln!(" !! this machine's own network. Local development only."); |
| 67 | eprintln!(); |
| 68 | } |
| 69 | |
| 70 | |
| 71 | |
| 72 | |
| 73 | let configured_token = config.setup_token.take(); |
| 74 | |
| 75 | let mut builder = Router::builder() |
| 76 | .assets(AssetBundle::load()?) |
| 77 | .cookies() |
| 78 | .sessions(session_config(config.insecure_cookies)) |
| 79 | .discover() |
| 80 | .app_context(config) |
| 81 | .app_context(pool.clone()) |
| 82 | |
| 83 | |
| 84 | .app_context(RateLimiter::default()); |
| 85 | |
| 86 | |
| 87 | |
| 88 | if !is_claimed(&SqliteUserRepo::new(pool)).await? { |
| 89 | let token = match &configured_token { |
| 90 | |
| 91 | |
| 92 | Some(supplied) => { |
| 93 | let token = SetupToken::from_operator(supplied.expose()).map_err(|error| { |
| 94 | eprintln!(); |
| 95 | eprintln!(" !! STEID_SETUP_TOKEN is not usable: {error}"); |
| 96 | eprintln!(" !! Unset it to have one generated, or supply a stronger value."); |
| 97 | eprintln!(); |
| 98 | error |
| 99 | })?; |
| 100 | announce_configured_setup(); |
| 101 | token |
| 102 | } |
| 103 | None => { |
| 104 | let token = SetupToken::generate(); |
| 105 | announce_setup(&token); |
| 106 | token |
| 107 | } |
| 108 | }; |
| 109 | |
| 110 | builder = builder.app_context(SetupState(token)); |
| 111 | } |
| 112 | |
| 113 | |
| 114 | |
| 115 | |
| 116 | |
| 117 | topcoat::start(builder.build()).await?; |
| 118 | |
| 119 | println!("steid: shut down"); |
| 120 | |
| 121 | Ok(()) |
| 122 | } |
| 123 | |
| 124 | |
| 125 | |
| 126 | |
| 127 | |
| 128 | |
| 129 | fn session_config(insecure_cookies: bool) -> SessionConfig { |
| 130 | if insecure_cookies { |
| 131 | eprintln!(); |
| 132 | eprintln!(" !! STEID_INSECURE_COOKIES is on: the session cookie has no Secure"); |
| 133 | eprintln!(" !! flag and travels unencrypted. Local development only."); |
| 134 | eprintln!(); |
| 135 | |
| 136 | SessionConfig::builder() |
| 137 | .token_store(InsecureCookieTokenStore::new()) |
| 138 | .build() |
| 139 | } else { |
| 140 | SessionConfig::default() |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | |
| 145 | |
| 146 | |
| 147 | |
| 148 | fn announce_configured_setup() { |
| 149 | println!(); |
| 150 | println!(" This steid has no owner yet. Claim it at /auth/setup with the token"); |
| 151 | println!(" from STEID_SETUP_TOKEN."); |
| 152 | println!(); |
| 153 | } |
| 154 | |
| 155 | |
| 156 | fn announce_setup(token: &SetupToken) { |
| 157 | println!(); |
| 158 | println!(" This steid has no owner yet. Claim it at /auth/setup with:"); |
| 159 | println!(); |
| 160 | println!(" {}", token.reveal()); |
| 161 | println!(); |
| 162 | println!(" The token is held in memory only — restarting issues a new one."); |
| 163 | println!(); |
| 164 | } |