[READ-ONLY] Mirror of https://github.com/FoxxMD/docker-proxy-filter. Filter the contents of Docker API responses
docker docker-socket-proxy filter
0

Configure Feed

Select the types of activity you want to include in your feed.

feat: Improve env loading and error reporting

FoxxMD (Oct 8, 2025, 4:23 PM UTC) 471f4247 540d6693

+34 -6
+1 -1
docker/Dockerfile
··· 1 1 FROM rust:1.90-bookworm as Builder 2 2 3 - ENV RUST_LOG=info,docker-proxy-filter=debug 3 + ENV RUST_LOG=info,docker_proxy_filter=debug 4 4 5 5 WORKDIR /usr/src/app 6 6 COPY . .
+12 -3
src/config.rs
··· 1 1 use dotenvy; 2 2 use serde::Deserialize; 3 - use std::path::PathBuf; 4 3 use tracing::*; 5 4 6 5 fn default_scrub() -> bool { ··· 20 19 pub port: u16, 21 20 } 22 21 23 - pub fn loadenv() -> Result<PathBuf, dotenvy::Error> { 24 - dotenvy::dotenv() 22 + pub fn loadenv() -> Result<(), dotenvy::Error> { 23 + match dotenvy::dotenv() { 24 + Ok(_) => Ok(()), 25 + Err(err) => { 26 + // we don't care if there isn't a .env file 27 + if err.not_found() { 28 + Ok(()) 29 + } else { 30 + Err(err) 31 + } 32 + } 33 + } 25 34 } 26 35 27 36 pub fn get_config() -> Result<Config, envy::Error> {
+8 -1
src/main.rs
··· 14 14 15 15 #[ntex::main] 16 16 async fn main() -> std::io::Result<()> { 17 - let _ = config::loadenv(); 17 + 18 + match config::loadenv() { 19 + Ok(_) => (), 20 + Err(err) => { 21 + println!("there was a problem reading .env: {err}"); 22 + } 23 + } 18 24 19 25 tracing_subscriber::fmt::fmt() 26 + // uses RUST_LOG env for filtering log levels and namespaces 20 27 .with_env_filter(tracing_subscriber::EnvFilter::from_default_env()) 21 28 .init(); 22 29
+13 -1
src/proxy.rs
··· 35 35 36 36 if res.status() == 200 { 37 37 38 + // if route is to list containers we want to return a filtered list 38 39 if new_url.path().contains("containers/json") { 39 40 40 41 let containers = &res.json::<Vec<ContainerSummary>>().await.unwrap(); 41 42 43 + // filter all containers to only those that have values from CONTAINER_NAMES includes in their names 42 44 let filtered_containers = containers.into_iter() 43 45 .filter(|&con| docker::is_container_named(con, &container_names.get_ref())) 44 46 .collect::<Vec<&ContainerSummary>>(); ··· 48 50 Ok(fresp) 49 51 } else { 50 52 53 + // only deal with routes that are for containers like /containers/1234/{some_resource} 51 54 match docker::match_container_get(new_url.path()) { 55 + // the regex pulls the container id from the route with a named capture group, avaiable as m.id 52 56 Some (m) => { 57 + // we keep a stateful hashmap of all requested container ids and their names 58 + // see AppStateWithContainerMap 53 59 let mut cm = data.container_map.lock().unwrap(); 60 + 61 + // if the map does not already include the container id-name then we try to get it with our own request to docker api 54 62 if !cm.contains_key(&m.id) { 55 63 debug!("Requested container Id not in map, trying to inspect: {}", &m.id); 56 64 let name_res = docker::get_container_name(&forward_url.to_string(), &m.id).await; ··· 66 74 } 67 75 } 68 76 77 + // then we try to get the name from the stateful hashmap 69 78 let name_val = cm.get(&m.id).unwrap(); 70 79 71 80 match name_val { 72 81 Some(n) => { 82 + // only return if a response if requested container has a name that includes values from CONTAINER_NAMES 73 83 if container_names.get_ref().iter().any(|x| n.contains(x)) { 74 - //if n.contains(container_name.get_ref()) { //n.iter().any(|x| x.contains(container_name.get_ref())) { 75 84 85 + // if the route resource is specifically the Container Inspect API 86 + // then we may need to scrub Envs if SCRUB_ENVS=true 76 87 if m.resource == "json" { 77 88 78 89 client_resp.content_type("application/json"); ··· 102 113 } 103 114 } 104 115 } 116 + // if we don't match container route then proxy response through, unmodified 105 117 None => { 106 118 let stream = res.into_stream(); 107 119 Ok(client_resp.streaming(stream))