# Route Guards
Route Guards allow you to dictate whether a request matches a specific route based on more than just the path and method. They can check headers, content types, or even custom logic.
#Built-in Guards
Actix Web provides several built-in guards in the guard module, such as:
- guard::Get(), guard::Post()
- guard::Header(name, value)
- guard::Host(host)
- guard::Fn(closure)
#Custom Logic
You can create a custom guard by implementing the actix_web::guard::Guard trait or by using guard::fn_guard.
code_blocksrust
use actix_web::{guard, web, App, HttpResponse};
fn custom_guard(ctx: &guard::GuardContext) -> bool {
ctx.head().headers().contains_key("x-my-custom-header")
}
// Inside App configuration:
// .route(
// "/guarded",
// web::get()
// .guard(guard::fn_guard(custom_guard))
// .to(|| async { HttpResponse::Ok().body("Success!") })
// )