Skip to content

Commit

Permalink
Merge branch 'master' of github.com:magiclen/nginx-cache-purge
Browse files Browse the repository at this point in the history
  • Loading branch information
magiclen committed Nov 27, 2023
2 parents a154968 + ff3112e commit d6ab071
Showing 1 changed file with 29 additions and 30 deletions.
59 changes: 29 additions & 30 deletions src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,35 @@ async fn index_handler(
}
}

async fn run(socket_file_path: &Path) -> anyhow::Result<AppResult> {
fn create_app() -> Router {
Router::new()
.route("/", any(index_handler))
.layer(SetResponseHeaderLayer::overriding(
header::CACHE_CONTROL,
HeaderValue::from_static("no-store"),
))
.layer(
TraceLayer::new_for_http()
.make_span_with(DefaultMakeSpan::new().level(Level::INFO))
.on_request(DefaultOnRequest::new().level(Level::INFO))
.on_response(DefaultOnResponse::new().level(Level::INFO)),
)
}

pub async fn server_main(socket_file_path: &Path) -> anyhow::Result<AppResult> {
let mut ansi_color = io::stdout().is_terminal();

if ansi_color && enable_ansi_support::enable_ansi_support().is_err() {
ansi_color = false;
}

tracing_subscriber::registry()
.with(tracing_subscriber::fmt::layer().with_ansi(ansi_color))
.with(EnvFilter::builder().with_default_directive(Level::INFO.into()).from_env_lossy())
.init();

let app = create_app();

let uds = {
match fs::metadata(socket_file_path).await {
Ok(metadata) => {
Expand Down Expand Up @@ -107,19 +135,6 @@ async fn run(socket_file_path: &Path) -> anyhow::Result<AppResult> {
uds
};

let app: Router = Router::new()
.route("/", any(index_handler))
.layer(SetResponseHeaderLayer::overriding(
header::CACHE_CONTROL,
HeaderValue::from_static("no-store"),
))
.layer(
TraceLayer::new_for_http()
.make_span_with(DefaultMakeSpan::new().level(Level::INFO))
.on_request(DefaultOnRequest::new().level(Level::INFO))
.on_response(DefaultOnResponse::new().level(Level::INFO)),
);

tracing::info!("listening on {socket_file_path:?}");
serve(uds, app).await?;

Expand All @@ -130,19 +145,3 @@ async fn run(socket_file_path: &Path) -> anyhow::Result<AppResult> {

Ok(AppResult::Ok)
}

#[inline]
pub async fn server_main(socket_file_path: &Path) -> anyhow::Result<AppResult> {
let mut ansi_color = io::stdout().is_terminal();

if ansi_color && enable_ansi_support::enable_ansi_support().is_err() {
ansi_color = false;
}

tracing_subscriber::registry()
.with(tracing_subscriber::fmt::layer().with_ansi(ansi_color))
.with(EnvFilter::builder().with_default_directive(Level::INFO.into()).from_env_lossy())
.init();

run(socket_file_path).await
}

0 comments on commit d6ab071

Please sign in to comment.