diff --git a/Dockerfile b/Dockerfile index b3f9e34..8ab3fd7 100644 --- a/Dockerfile +++ b/Dockerfile @@ -21,9 +21,7 @@ COPY --from=builder --chown=nonroot:nonroot /traefik_crowdsec_bouncer/target/rel COPY --from=samuelba/healthcheck:latest --chown=nonroot:nonroot /app/healthcheck /app/healthcheck USER nonroot WORKDIR /app -EXPOSE 9090 -ENV PORT=9090 ENV API_PATH=api/v1/health HEALTHCHECK --interval=30s --timeout=5s --start-period=5s CMD ["/app/healthcheck"] diff --git a/README.md b/README.md index 1502dad..d914b4d 100644 --- a/README.md +++ b/README.md @@ -55,10 +55,11 @@ services: - STREAM_UPDATE_INTERVAL=5 # The cache expiration time in seconds. Only needed in "live" mode. - LIVE_CACHE_EXPIRATION=5 + # The port the service should listen on. Default is 8080. + - PORT=8080 ``` ## Roadmap * [ ] Add support for log level. * [ ] Add support for trusted proxies. -* [ ] Add support for different server listening port. diff --git a/src/config.rs b/src/config.rs index d68e3a0..8ab1c84 100644 --- a/src/config.rs +++ b/src/config.rs @@ -23,6 +23,8 @@ pub struct Config { pub crowdsec_cache_ttl: i64, /// The CrowdSec stream update interval in seconds. pub stream_interval: u64, + /// The listening port. + pub port: u16, } /// Read the configuration from the environment variables. @@ -34,8 +36,9 @@ pub fn read_config() -> Config { crowdsec_stream_url: String::new(), crowdsec_api_key: String::new(), crowdsec_mode: CrowdSecMode::Stream, - crowdsec_cache_ttl: 5000, + crowdsec_cache_ttl: 0, stream_interval: 0, + port: 8080, }; // Get the CrowdSec mode. @@ -98,5 +101,11 @@ pub fn read_config() -> Config { } } + // Get the listening port. + config.port = match env::var("PORT") { + Ok(val) => val.parse::().unwrap_or(config.port), + Err(_) => config.port, + }; + config } diff --git a/src/main.rs b/src/main.rs index 2246b9c..f8ad00a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -63,6 +63,7 @@ async fn main() -> io::Result<()> { // API. info!("Starting HTTP server (API)."); + let port = config.port; HttpServer::new(move || { App::new() // Enable the logger - always register actix-web Logger middleware last. @@ -77,7 +78,7 @@ async fn main() -> io::Result<()> { .service(bouncer::block_list) .service(bouncer::health) }) - .bind("0.0.0.0:9090")? + .bind(format!("0.0.0.0:{}", port))? .run() .await }