add redis config

This commit is contained in:
guochao 2024-05-29 19:05:15 +08:00
parent 3079e602c8
commit a4c5009947
2 changed files with 24 additions and 3 deletions

View File

@ -17,6 +17,9 @@ enum Subcommand {
listen_address: String, listen_address: String,
#[clap(long, short = 'p', default_value = "3000", env)] #[clap(long, short = 'p', default_value = "3000", env)]
listen_port: u16, listen_port: u16,
#[clap(long, default_value = "redis://127.0.0.1", env)]
redis_addr: String,
}, },
#[cfg(feature = "extract-static")] #[cfg(feature = "extract-static")]
@ -28,6 +31,7 @@ impl Default for Subcommand {
Subcommand::Serve { Subcommand::Serve {
listen_address: "127.0.0.1".into(), listen_address: "127.0.0.1".into(),
listen_port: 3000, listen_port: 3000,
redis_addr: String::from("redis://127.0.0.1")
} }
} }
} }
@ -42,12 +46,15 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
Subcommand::Serve { Subcommand::Serve {
listen_address, listen_address,
listen_port, listen_port,
redis_addr,
} => { } => {
let addrs: Vec<_> = (listen_address.as_str(), listen_port) let addrs: Vec<_> = (listen_address.as_str(), listen_port)
.to_socket_addrs()? .to_socket_addrs()?
.collect(); .collect();
let service = chat_signaling_server::web::routes::make_service()?; let service = chat_signaling_server::web::routes::make_service(Some(chat_signaling_server::web::routes::Config{
redis_addr,
}))?;
let mut futures = let mut futures =
futures::stream::FuturesUnordered::from_iter(addrs.iter().map(|addr| async { futures::stream::FuturesUnordered::from_iter(addrs.iter().map(|addr| async {

View File

@ -50,7 +50,21 @@ async fn reload(State(state): State<Arc<RwLock<AppState>>>) -> Json<ReloadResult
Json(ReloadResult::default()) Json(ReloadResult::default())
} }
pub fn make_service() -> Result<IntoMakeService<Router<()>>, Box<dyn std::error::Error>> { pub struct Config {
pub redis_addr: String,
}
impl Default for Config {
fn default() -> Self {
Self {
redis_addr: String::from("redis://127.0.0.1")
}
}
}
pub fn make_service(config: Option<Config>) -> Result<IntoMakeService<Router<()>>, Box<dyn std::error::Error>> {
let config = config.unwrap_or_default();
let templates = load_templates()?; let templates = load_templates()?;
let router = Router::new(); let router = Router::new();
@ -58,7 +72,7 @@ pub fn make_service() -> Result<IntoMakeService<Router<()>>, Box<dyn std::error:
#[cfg(feature = "serve-static")] #[cfg(feature = "serve-static")]
let router = router.route_service("/static/*path", staticfiles::StaticFiles::strip("/static/")); let router = router.route_service("/static/*path", staticfiles::StaticFiles::strip("/static/"));
let redis = redis::Client::open("redis://127.0.0.1")?; let redis = redis::Client::open(config.redis_addr)?;
let grpc_service = tonic_web::enable(signaling::signaling_server::SignalingServer::new( let grpc_service = tonic_web::enable(signaling::signaling_server::SignalingServer::new(
GrpcSignalingService { GrpcSignalingService {