diff --git a/.gitignore b/.gitignore index 21d607f..179cbcf 100644 --- a/.gitignore +++ b/.gitignore @@ -5,3 +5,4 @@ api_token profile.json _*.json env +*~ diff --git a/README.md b/README.md index f7924c1..4f4349f 100644 --- a/README.md +++ b/README.md @@ -14,8 +14,8 @@ ## Configuration Forgery reads the following environment variables: -- `FORGEJO_URL`: url of the forgejo instance (e.g. https://git.deuxfleurs.fr) -- `FORGEJO_API_TOKEN`: Forgejo API token *granting admin access*. Required. You +- `FORGE_URL`: url of the forgejo instance (e.g. https://git.deuxfleurs.fr) +- `FORGE_API_TOKEN`: Forgejo API token *granting admin access*. Required. You can generate an API token using the Forgejo web interface in `Settings -> Applications -> Generate New Token`. - `ORG_NAME`: organization name (used in the notification email sent when diff --git a/src/main.rs b/src/main.rs index cf5a660..7b6c12a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -53,6 +53,7 @@ const GUESS_LEGIT_THRESHOLD: f32 = 0.3; pub struct Config { pub forge_url: Url, + pub forge_api_token: String, pub org_name: String, pub admin_contact_email: String, pub actually_ban: ActuallyBan, @@ -63,7 +64,9 @@ pub struct Config { impl Config { async fn from_env() -> anyhow::Result { let forge_url_s = - std::env::var("FORGEJO_URL").context("reading the FORGEJO_URL environment variable")?; + std::env::var("FORGE_URL").context("reading the FORGE_URL environment variable")?; + let forge_api_token = + std::env::var("FORGE_API_TOKEN").context("reading the FORGE_API_TOKEN environment variable")?; let org_name = std::env::var("ORG_NAME").context("reading the ORG_NAME environment variable")?; let admin_contact_email = std::env::var("ADMIN_CONTACT_EMAIL") @@ -91,7 +94,8 @@ impl Config { }; Ok(Config { - forge_url: Url::parse(&forge_url_s).context("parsing FORGEJO_URL")?, + forge_url: Url::parse(&forge_url_s).context("parsing FORGE_URL")?, + forge_api_token, org_name, admin_contact_email, actually_ban, @@ -110,7 +114,7 @@ pub enum ActuallyBan { // Runtime state of the application struct AppState { - // config parameters derived from environment variable + // config parameters derived from environment variables config: Arc, // authenticated access to the forgejo instance forge: Arc, @@ -120,14 +124,6 @@ struct AppState { db: Db, } -fn forge(url: &Url) -> anyhow::Result { - let api_token = std::env::var("FORGEJO_API_TOKEN") - .context("reading the FORGEJO_API_TOKEN environment variable")?; - - let forge = Forgejo::new(Auth::Token(&api_token), url.clone())?; - Ok(forge) -} - async fn load_db(storage: &Storage, forge: &Forgejo) -> anyhow::Result { let classifier = storage::load_classifier(storage).await?; let userdb = match storage::load_userdb(storage).await? { @@ -459,8 +455,11 @@ async fn main() -> anyhow::Result<()> { let _ = *TEMPLATES; let config = Arc::new(Config::from_env().await?); - let forge = Arc::new(forge(&config.forge_url)?); let storage = Arc::new(Storage::from_env().await?); + let forge = Arc::new(Forgejo::new( + Auth::Token(&config.forge_api_token), + config.forge_url.clone(), + )?); eprintln!("Load users and repos"); let db = load_db(&storage, &forge).await?; @@ -499,7 +498,12 @@ async fn main() -> anyhow::Result<()> { println!( "Listening on http://{}:{}", - config.listen_addr, config.listen_port + (if config.listen_addr == "0.0.0.0" { + "127.0.0.1" + } else { + &config.listen_addr + }), + config.listen_port ); let app = Router::new()