| 1 | use std::env; |
| 2 | |
| 3 | #[derive(Clone)] |
| 4 | pub struct Config { |
| 5 | pub site_name: String, |
| 6 | pub bind: String, |
| 7 | pub repos_root: String, |
| 8 | pub db_url: String, |
| 9 | pub max_repos_per_user: i64, |
| 10 | pub max_repo_bytes: u64, |
| 11 | pub max_push_bytes: usize, |
| 12 | pub hook_path: String, |
| 13 | pub git_http_backend: String, |
| 14 | pub admin_key: Option<String>, |
| 15 | pub ssh_bind: String, |
| 16 | pub ssh_host_key_path: String, |
| 17 | } |
| 18 | |
| 19 | impl Config { |
| 20 | pub fn from_env() -> Self { |
| 21 | let site_name = env::var("OPENHUB_SITE_NAME").unwrap_or_else(|_| "openhub".to_string()); |
| 22 | let bind = env::var("OPENHUB_BIND").unwrap_or_else(|_| "0.0.0.0:8080".to_string()); |
| 23 | let repos_root = env::var("OPENHUB_REPOS_ROOT").unwrap_or_else(|_| "/srv/openhub/repos".to_string()); |
| 24 | let db_url = env::var("OPENHUB_DB_URL").unwrap_or_else(|_| "sqlite:/srv/openhub/openhub.db".to_string()); |
| 25 | |
| 26 | let max_repos_per_user = env::var("OPENHUB_MAX_REPOS_PER_USER").ok().and_then(|v| v.parse().ok()).unwrap_or(25); |
| 27 | let max_repo_bytes = env::var("OPENHUB_MAX_REPO_BYTES").ok().and_then(|v| v.parse().ok()).unwrap_or(50 * 1024 * 1024); |
| 28 | let max_push_bytes = env::var("OPENHUB_MAX_PUSH_BYTES").ok().and_then(|v| v.parse().ok()).unwrap_or(10 * 1024 * 1024); |
| 29 | |
| 30 | let hook_path = env::var("OPENHUB_HOOK_PATH").unwrap_or_else(|_| "/usr/local/bin/git-quota-hook".to_string()); |
| 31 | let git_http_backend = env::var("OPENHUB_GIT_HTTP_BACKEND").unwrap_or_else(|_| "git-http-backend".to_string()); |
| 32 | let admin_key = env::var("OPENHUB_ADMIN_KEY").ok(); |
| 33 | let ssh_bind = env::var("OPENHUB_SSH_BIND").unwrap_or_else(|_| "0.0.0.0:22".to_string()); |
| 34 | let ssh_host_key_path = env::var("OPENHUB_SSH_HOST_KEY").unwrap_or_else(|_| "/srv/openhub/ssh_host_ed25519_key".to_string()); |
| 35 | |
| 36 | Self { |
| 37 | site_name, |
| 38 | bind, |
| 39 | repos_root, |
| 40 | db_url, |
| 41 | max_repos_per_user, |
| 42 | max_repo_bytes, |
| 43 | max_push_bytes, |
| 44 | hook_path, |
| 45 | git_http_backend, |
| 46 | admin_key, |
| 47 | ssh_bind, |
| 48 | ssh_host_key_path, |
| 49 | } |
| 50 | } |
| 51 | } |