use reqkey::{
middleware::{KeyLocation, Middleware, MiddlewareConfig},
rocket::{default_catchers, ReqKeyFairing, ReqKeyGuard},
Client,
};
use rocket::serde::json::{json, Value};
#[rocket::get("/health")]
fn health() -> Value {
json!({ "ok": true })
}
#[rocket::post("/payments")]
#[allow(clippy::needless_pass_by_value)]
fn create_payment(reqkey: ReqKeyGuard) -> Value {
json!({
"created": true,
"credits_remaining": reqkey
.decision()
.and_then(|decision| decision.credits_remaining),
})
}
#[rocket::launch]
fn rocket() -> _ {
let client = Client::from_env().expect("REQKEY_PROJECT_KEY must be configured");
let config = MiddlewareConfig::builder("api_payments")
.key_location(KeyLocation::header("X-StartupName-Key"))
.exclude_path("/health")
.build()
.expect("valid ReqKey configuration");
rocket::build()
.attach(ReqKeyFairing::new(Middleware::new(client, config)))
.register("/", default_catchers())
.mount("/", rocket::routes![health, create_payment])
}