Skip to content

Commit

Permalink
Merge pull request #2 from samuelba/feature/configurable-listening-port
Browse files Browse the repository at this point in the history
add configurable listening port
  • Loading branch information
samuelba authored Apr 8, 2023
2 parents ed83b96 + 6889a1b commit 991c583
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 5 deletions.
2 changes: 0 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -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"]

Expand Down
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
11 changes: 10 additions & 1 deletion src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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.
Expand Down Expand Up @@ -98,5 +101,11 @@ pub fn read_config() -> Config {
}
}

// Get the listening port.
config.port = match env::var("PORT") {
Ok(val) => val.parse::<u16>().unwrap_or(config.port),
Err(_) => config.port,
};

config
}
3 changes: 2 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
}

0 comments on commit 991c583

Please sign in to comment.