This commit is contained in:
Armaël Guéneau 2025-01-02 12:45:00 +01:00
parent aef35f2a0b
commit 52e9413349
3 changed files with 20 additions and 15 deletions

1
.gitignore vendored
View file

@ -5,3 +5,4 @@ api_token
profile.json profile.json
_*.json _*.json
env env
*~

View file

@ -14,8 +14,8 @@
## Configuration ## Configuration
Forgery reads the following environment variables: Forgery reads the following environment variables:
- `FORGEJO_URL`: url of the forgejo instance (e.g. https://git.deuxfleurs.fr) - `FORGE_URL`: url of the forgejo instance (e.g. https://git.deuxfleurs.fr)
- `FORGEJO_API_TOKEN`: Forgejo API token *granting admin access*. Required. You - `FORGE_API_TOKEN`: Forgejo API token *granting admin access*. Required. You
can generate an API token using the Forgejo web interface in `Settings -> can generate an API token using the Forgejo web interface in `Settings ->
Applications -> Generate New Token`. Applications -> Generate New Token`.
- `ORG_NAME`: organization name (used in the notification email sent when - `ORG_NAME`: organization name (used in the notification email sent when

View file

@ -53,6 +53,7 @@ const GUESS_LEGIT_THRESHOLD: f32 = 0.3;
pub struct Config { pub struct Config {
pub forge_url: Url, pub forge_url: Url,
pub forge_api_token: String,
pub org_name: String, pub org_name: String,
pub admin_contact_email: String, pub admin_contact_email: String,
pub actually_ban: ActuallyBan, pub actually_ban: ActuallyBan,
@ -63,7 +64,9 @@ pub struct Config {
impl Config { impl Config {
async fn from_env() -> anyhow::Result<Self> { async fn from_env() -> anyhow::Result<Self> {
let forge_url_s = 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 = let org_name =
std::env::var("ORG_NAME").context("reading the ORG_NAME environment variable")?; std::env::var("ORG_NAME").context("reading the ORG_NAME environment variable")?;
let admin_contact_email = std::env::var("ADMIN_CONTACT_EMAIL") let admin_contact_email = std::env::var("ADMIN_CONTACT_EMAIL")
@ -91,7 +94,8 @@ impl Config {
}; };
Ok(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, org_name,
admin_contact_email, admin_contact_email,
actually_ban, actually_ban,
@ -110,7 +114,7 @@ pub enum ActuallyBan {
// Runtime state of the application // Runtime state of the application
struct AppState { struct AppState {
// config parameters derived from environment variable // config parameters derived from environment variables
config: Arc<Config>, config: Arc<Config>,
// authenticated access to the forgejo instance // authenticated access to the forgejo instance
forge: Arc<Forgejo>, forge: Arc<Forgejo>,
@ -120,14 +124,6 @@ struct AppState {
db: Db, db: Db,
} }
fn forge(url: &Url) -> anyhow::Result<Forgejo> {
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<Db> { async fn load_db(storage: &Storage, forge: &Forgejo) -> anyhow::Result<Db> {
let classifier = storage::load_classifier(storage).await?; let classifier = storage::load_classifier(storage).await?;
let userdb = match storage::load_userdb(storage).await? { let userdb = match storage::load_userdb(storage).await? {
@ -459,8 +455,11 @@ async fn main() -> anyhow::Result<()> {
let _ = *TEMPLATES; let _ = *TEMPLATES;
let config = Arc::new(Config::from_env().await?); 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 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"); eprintln!("Load users and repos");
let db = load_db(&storage, &forge).await?; let db = load_db(&storage, &forge).await?;
@ -499,7 +498,12 @@ async fn main() -> anyhow::Result<()> {
println!( println!(
"Listening on http://{}:{}", "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() let app = Router::new()