Fix error logging on file not found
This commit is contained in:
@ -20,6 +20,7 @@ FROM scratch
|
|||||||
WORKDIR /
|
WORKDIR /
|
||||||
|
|
||||||
CMD mkdir /scripts
|
CMD mkdir /scripts
|
||||||
|
CMD touch /scripts/curl.sh
|
||||||
CMD echo "#!/bin/sh" >> /scripts/curl.sh
|
CMD echo "#!/bin/sh" >> /scripts/curl.sh
|
||||||
CMD echo "echo Hello there!" >> /scripts/curl.sh
|
CMD echo "echo Hello there!" >> /scripts/curl.sh
|
||||||
|
|
||||||
|
|||||||
19
src/main.rs
19
src/main.rs
@ -1,8 +1,7 @@
|
|||||||
use compound_error::CompoundError;
|
use compound_error::CompoundError;
|
||||||
use hyper::service::{make_service_fn, service_fn};
|
use hyper::service::{make_service_fn, service_fn};
|
||||||
use hyper::{Body, Client, Request, Response, Server, Uri};
|
use hyper::{Body, Client, Request, Response, Server, Uri};
|
||||||
use kv_log_macro::info;
|
use kv_log_macro::{error, info};
|
||||||
use log::error;
|
|
||||||
use std::net::SocketAddr;
|
use std::net::SocketAddr;
|
||||||
use std::path::PathBuf;
|
use std::path::PathBuf;
|
||||||
use structopt::StructOpt;
|
use structopt::StructOpt;
|
||||||
@ -43,8 +42,16 @@ async fn proxy_pass(mut req: Request<Body>, opt: &Opt) -> Result<Response<Body>,
|
|||||||
let mut error = None;
|
let mut error = None;
|
||||||
|
|
||||||
let response = if user_agent_matches {
|
let response = if user_agent_matches {
|
||||||
let file = fs::read_to_string(&opt.file).await?;
|
match fs::read_to_string(&opt.file).await {
|
||||||
Response::new(file.into())
|
Ok(file) => Response::new(file.into()),
|
||||||
|
Err(e) => {
|
||||||
|
error = Some(format!("{:?}", e));
|
||||||
|
Response::builder()
|
||||||
|
.status(404)
|
||||||
|
.body("File not found".into())
|
||||||
|
.expect("infallible response")
|
||||||
|
}
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
let proxy_uri: Uri = opt.proxy_pass.parse().expect("proxy uri");
|
let proxy_uri: Uri = opt.proxy_pass.parse().expect("proxy uri");
|
||||||
|
|
||||||
@ -62,11 +69,11 @@ async fn proxy_pass(mut req: Request<Body>, opt: &Opt) -> Result<Response<Body>,
|
|||||||
match client.request(req).await {
|
match client.request(req).await {
|
||||||
Ok(response) => response,
|
Ok(response) => response,
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
error = Some(e);
|
error = Some(format!("{:?}", e));
|
||||||
Response::builder()
|
Response::builder()
|
||||||
.status(503)
|
.status(503)
|
||||||
.body("503 Service Unavailable".into())
|
.body("503 Service Unavailable".into())
|
||||||
.unwrap()
|
.expect("infallible response")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user