git-gemini-forge 0.1.0

A simple Gemini server that serves a read-only view of public repositories from a Git forge.
pub mod error;
use error::Error;
use url::Url;
use std::env;

#[derive(Clone)]
pub struct Config {
	/// The API we should use to communicate with the forge.
	pub forge_type: String,

	/// The URL of the forge.
	pub forge_url: Url,

	/// The directory to look for certificate files.
	pub certs_dir: String,
}

/// Retrieves configuration values from environment variables.
pub fn config() -> Result<Config, Error> {
	let forge_type_key: &str = "PLATFORM_TYPE";
	let forge_url_key: &str = "FORGE_URL";
	let certs_dir_key: &str = "CERTS_DIR";

	// Get the API shape identifier we should use
	let default_type: &str = "forgejo";
	let forge_type: String = env::var(forge_type_key)
		.unwrap_or(default_type.to_string())
		.to_lowercase();

	// Make sure it's one we support
	if forge_type != default_type {
		return Err(Error::BadPlatformType(forge_type));
	}

	// Get the URL of the git forge
	let forge_url_string: String = env::var(forge_url_key)
		.unwrap_or("".to_string());
	if forge_url_string.len() == 0 {
		return Err(Error::MissingUrl);
	}

	// Get the certificates directory
	let certs_dir: String = env::var(certs_dir_key)
		.unwrap_or(".certs".to_string());

	match Url::parse(forge_url_string.as_str()) {
		Ok(forge_url) => Ok(Config { forge_type, forge_url, certs_dir }),
		Err(parse_error) => Err(Error::BadUrl(parse_error))
	}
}