feat(admin): host and port set via env vars

This allows users to use ipv4 or ipv6
This commit is contained in:
Zahin Mohammad 2024-09-04 11:04:35 -04:00
parent 886376e8bd
commit 8507a32729
2 changed files with 11 additions and 5 deletions

View File

@ -2,21 +2,26 @@ use tracing::warn;
#[derive(Debug, Clone)]
pub struct Config {
pub host: String,
pub port: u16,
pub redis_url: String,
pub gotrue_url: String,
pub appflowy_cloud_url: String,
}
impl Config {
pub fn from_env() -> Self {
Config {
pub fn from_env() -> Result<Config, anyhow::Error> {
let cfg = Config {
host: get_or_default("ADMIN_FRONTEND_HOST", "0.0.0.0"),
port: get_or_default("ADMIN_FRONTEND_PORT", "3000").parse()?,
redis_url: get_or_default("ADMIN_FRONTEND_REDIS_URL", "redis://localhost:6379"),
gotrue_url: get_or_default("ADMIN_FRONTEND_GOTRUE_URL", "http://localhost:9999"),
appflowy_cloud_url: get_or_default(
"ADMIN_FRONTEND_APPFLOWY_CLOUD_URL",
"http://localhost:8000",
),
}
};
Ok(cfg)
}
}

View File

@ -27,7 +27,7 @@ async fn main() {
.with_env_filter(tracing_subscriber::EnvFilter::from_default_env())
.init();
let config = Config::from_env();
let config = Config::from_env().unwrap();
info!("config loaded: {:?}", &config);
let gotrue_client = gotrue::api::Client::new(reqwest::Client::new(), &config.gotrue_url);
@ -65,7 +65,8 @@ async fn main() {
.nest_service("/web-api", web_api_router)
.nest_service("/assets", ServeDir::new("assets"));
let listener = TcpListener::bind("0.0.0.0:3000")
let address = format!("{}:{}", config.host, config.port);
let listener = TcpListener::bind(address)
.await
.expect("failed to bind to port");
info!("listening on: {:?}", listener);