| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | |
| 10 | use steid::{ |
| 11 | application::{AppConfig, is_claimed, port::Mailer}, |
| 12 | cli::{Command, print_reset_link, run_backup, run_restore}, |
| 13 | domain::SetupToken, |
| 14 | infrastructure::{self, mail::LettreMailer, repository::SqliteUserRepo}, |
| 15 | }; |
| 16 | use topcoat::asset::AssetBundle; |
| 17 | |
| 18 | #[tokio::main] |
| 19 | async fn main() -> Result<(), Box<dyn std::error::Error>> { |
| 20 | dotenvy::dotenv().ok(); |
| 21 | |
| 22 | |
| 23 | |
| 24 | let command = match Command::parse(std::env::args()) { |
| 25 | Ok(command) => command, |
| 26 | Err(message) => { |
| 27 | eprintln!("{message}"); |
| 28 | std::process::exit(2); |
| 29 | } |
| 30 | }; |
| 31 | |
| 32 | let mut config = AppConfig::from_env()?; |
| 33 | |
| 34 | match command { |
| 35 | Command::Serve => {} |
| 36 | |
| 37 | |
| 38 | Command::Backup { destination } => finish(run_backup(&config, &destination).await), |
| 39 | Command::Restore { archive, mode } => finish(run_restore(&config, &archive, mode).await), |
| 40 | Command::ResetPassword { handle } => { |
| 41 | let pool = infrastructure::database::connect(&config.database_url).await?; |
| 42 | print_reset_link(&handle, &config, &pool).await?; |
| 43 | return Ok(()); |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | |
| 48 | |
| 49 | |
| 50 | |
| 51 | match LettreMailer::from_config(&config) { |
| 52 | Ok(mailer) if mailer.configured() => println!("steid: outgoing mail is configured"), |
| 53 | Ok(_) => {} |
| 54 | Err(error) => { |
| 55 | eprintln!(); |
| 56 | eprintln!(" !! the mail configuration is not usable: {error}"); |
| 57 | eprintln!(" !! Unset STEID_SMTP_URL and STEID_MAIL_FROM to run without email."); |
| 58 | eprintln!(); |
| 59 | std::process::exit(4); |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | let pool = infrastructure::database::connect(&config.database_url).await?; |
| 64 | |
| 65 | infrastructure::app::sweep_on_boot(&config, &pool).await; |
| 66 | |
| 67 | |
| 68 | |
| 69 | |
| 70 | |
| 71 | match infrastructure::app::seed_owner_on_boot(&config, &pool).await { |
| 72 | Ok(Some(owner)) => { |
| 73 | println!( |
| 74 | "steid: owner {} <{}> created at boot; sign in with: steid reset-password {}", |
| 75 | owner.handle, owner.email, owner.handle |
| 76 | ); |
| 77 | if let Some(organisation) = &owner.organisation { |
| 78 | println!( |
| 79 | "steid: organisation {} created and set as the home page", |
| 80 | organisation.handle |
| 81 | ); |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | |
| 86 | Ok(None) => {} |
| 87 | Err(error) => { |
| 88 | eprintln!(); |
| 89 | eprintln!(" !! the configured owner could not be created: {error}"); |
| 90 | eprintln!(" !! STEID_OWNER_EMAIL and STEID_OWNER_HANDLE are read together and"); |
| 91 | eprintln!(" !! must both be usable, as must STEID_OWNER_ORG where it is set."); |
| 92 | eprintln!(" !! Unset them to start unclaimed and claim at /auth/setup instead."); |
| 93 | eprintln!(); |
| 94 | std::process::exit(4); |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | |
| 99 | |
| 100 | |
| 101 | |
| 102 | if config.allow_private_webhooks { |
| 103 | eprintln!(); |
| 104 | eprintln!(" !! STEID_ALLOW_PRIVATE_WEBHOOKS is on: a webhook may point inside"); |
| 105 | eprintln!(" !! this machine's own network. Local development only."); |
| 106 | eprintln!(); |
| 107 | } |
| 108 | |
| 109 | if config.insecure_cookies { |
| 110 | eprintln!(); |
| 111 | eprintln!(" !! STEID_INSECURE_COOKIES is on: the session cookie has no Secure"); |
| 112 | eprintln!(" !! flag and travels unencrypted. Local development only."); |
| 113 | eprintln!(); |
| 114 | } |
| 115 | |
| 116 | |
| 117 | |
| 118 | |
| 119 | let configured_token = config.setup_token.take(); |
| 120 | |
| 121 | |
| 122 | |
| 123 | let setup = if is_claimed(&SqliteUserRepo::new(pool.clone())).await? { |
| 124 | None |
| 125 | } else { |
| 126 | Some(claim_token(configured_token.as_ref())?) |
| 127 | }; |
| 128 | |
| 129 | let router = infrastructure::app::build(config, pool, AssetBundle::load()?, setup) |
| 130 | .await |
| 131 | .build(); |
| 132 | |
| 133 | |
| 134 | |
| 135 | |
| 136 | |
| 137 | topcoat::start(router).await?; |
| 138 | |
| 139 | println!("steid: shut down"); |
| 140 | |
| 141 | Ok(()) |
| 142 | } |
| 143 | |
| 144 | |
| 145 | |
| 146 | |
| 147 | |
| 148 | |
| 149 | fn claim_token( |
| 150 | configured: Option<&steid::application::Secret>, |
| 151 | ) -> Result<SetupToken, Box<dyn std::error::Error>> { |
| 152 | match configured { |
| 153 | Some(supplied) => { |
| 154 | let token = SetupToken::from_operator(supplied.expose()).map_err(|error| { |
| 155 | eprintln!(); |
| 156 | eprintln!(" !! STEID_SETUP_TOKEN is not usable: {error}"); |
| 157 | eprintln!(" !! Unset it to have one generated, or supply a stronger value."); |
| 158 | eprintln!(); |
| 159 | error |
| 160 | })?; |
| 161 | announce_configured_setup(); |
| 162 | Ok(token) |
| 163 | } |
| 164 | None => { |
| 165 | let token = SetupToken::generate(); |
| 166 | announce_setup(&token); |
| 167 | Ok(token) |
| 168 | } |
| 169 | } |
| 170 | } |
| 171 | |
| 172 | |
| 173 | |
| 174 | |
| 175 | |
| 176 | fn announce_configured_setup() { |
| 177 | println!(); |
| 178 | println!(" This steid has no owner yet. Claim it at /auth/setup with the token"); |
| 179 | println!(" from STEID_SETUP_TOKEN."); |
| 180 | println!(); |
| 181 | } |
| 182 | |
| 183 | |
| 184 | fn announce_setup(token: &SetupToken) { |
| 185 | println!(); |
| 186 | println!(" This steid has no owner yet. Claim it at /auth/setup with:"); |
| 187 | println!(); |
| 188 | println!(" {}", token.reveal()); |
| 189 | println!(); |
| 190 | println!(" The token is held in memory only — restarting issues a new one."); |
| 191 | println!(); |
| 192 | } |
| 193 | |
| 194 | |
| 195 | |
| 196 | |
| 197 | |
| 198 | |
| 199 | fn finish(outcome: Result<(), Box<dyn std::error::Error>>) -> ! { |
| 200 | match outcome { |
| 201 | Ok(()) => std::process::exit(0), |
| 202 | Err(error) => { |
| 203 | eprintln!("steid: {error}"); |
| 204 | std::process::exit(1); |
| 205 | } |
| 206 | } |
| 207 | } |