AP-5/src/lib.rs

35 lines
1.4 KiB
Rust

use worker::*;
#[event(fetch)]
pub async fn main(req: Request, env: Env, _ctx: worker::Context) -> Result<Response> {
// Create an instance of the Router, which can use parameters (/user/:name) or wildcard values
// (/file/*pathname). Alternatively, use `Router::with_data(D)` and pass in arbitrary data for
// routes to access and share using the `ctx.data()` method.
let router = Router::new();
router
.get_async("/asterisk/:hostname", |_req, ctx| async move {
if let Some(hostname) = ctx.param("hostname") {
let kv = ctx.kv("GONK_NODES")?;
return match kv.get("asterisk_base").cache_ttl(300).text().await? {
Some(a) => Response::ok(&a),
None => Response::error(format!("Could not find asterisk.conf for {}", hostname), 404),
};
}
Response::error("Bad Request", 400)
})
.get_async("/pjsip/:hostname", |_req, ctx| async move {
if let Some(hostname) = ctx.param("hostname") {
let kv = ctx.kv("GONK_NODES")?;
return match kv.get("pjspi_base").cache_ttl(300).text().await? {
Some(a) => Response::ok(&a),
None => Response::error(format!("Could not find pjsip.conf for {}", hostname), 404),
};
}
Response::error("Bad Request", 400)
})
.run(req, env).await
}