Skip to content

Commit 15526d2

Browse files
committed
add http port env var
1 parent c910df5 commit 15526d2

File tree

4 files changed

+42
-2
lines changed

4 files changed

+42
-2
lines changed

Cargo.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,7 @@ tokio = { version = "1.41.1", features = ["macros", "rt-multi-thread"] }
1212
tower-http = { version = "0.6.2", features = ["trace"] }
1313
tracing = "0.1.41"
1414
tracing-subscriber = "0.3.19"
15+
16+
[[bin]]
17+
name = "local-detectors"
18+
path = "src/main.rs"

Dockerfile

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Use the official Rust image as a base
2+
FROM rust:latest
3+
4+
# Set the working directory to /app
5+
WORKDIR /app
6+
7+
# Copy the Cargo.toml file into the working directory
8+
COPY Cargo.toml .
9+
10+
COPY src/ /app/src/
11+
12+
# Build the Rust application
13+
RUN cargo build --release
14+
15+
# Copy the built application into the working directory
16+
COPY target/release/* .
17+
18+
# Expose the port that the application will listen on
19+
EXPOSE 8080
20+
21+
# Run the command to start the application when the container is launched
22+
CMD ["/app/local-detectors"]

src/detectors.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use regex::Regex;
55
use axum::{http::StatusCode, response::IntoResponse, Json};
66
use serde::{Deserialize, Serialize};
77
use serde_json::json;
8+
use tracing::info;
89

910
#[derive(Serialize, Deserialize, Debug)]
1011
#[cfg_attr(test, derive(PartialEq))]
@@ -92,9 +93,14 @@ fn regex_match(
9293
}
9394
}
9495

96+
// pub async fn handle_text_contents(Json(payload): Json<DetectionRequest>) {
97+
// info!("incoming payload: {:#?}", payload);
98+
// }
9599
pub async fn handle_text_contents(
96100
Json(payload): Json<DetectionRequest>,
97101
) -> Result<impl IntoResponse, (StatusCode, String)> {
102+
println!("hi");
103+
info!("incoming payload: {:?}", payload);
98104
if payload.detector_params.regex.is_empty() {
99105
return Err((StatusCode::BAD_REQUEST, format!("empty regex")));
100106
}

src/main.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::net::SocketAddr;
1+
use std::{env, net::SocketAddr};
22

33
use axum::{
44
routing::{get, post},
@@ -18,6 +18,14 @@ async fn main() {
1818
.compact()
1919
.init();
2020

21+
let mut http_port = 8080;
22+
if let Ok(port) = env::var("HTTP_PORT") {
23+
match port.parse::<u16>() {
24+
Ok(port) => http_port = port,
25+
Err(err) => println!("{}", err),
26+
}
27+
}
28+
2129
let app = Router::new()
2230
.route("/health", get(|| async { "Hello, World!" }))
2331
.route("/api/v1/text/contents", post(handle_text_contents))
@@ -27,7 +35,7 @@ async fn main() {
2735
.on_response(trace::DefaultOnResponse::new().level(Level::INFO)),
2836
);
2937

30-
let addr = SocketAddr::from(([127, 0, 0, 1], 8080));
38+
let addr = SocketAddr::from(([127, 0, 0, 1], http_port));
3139
tracing::info!("listening on {}", addr);
3240

3341
let listener = tokio::net::TcpListener::bind(addr).await.unwrap();

0 commit comments

Comments
 (0)