2021-02-20 06:46:44 +01:00
|
|
|
use surf::Body;
|
2021-02-09 18:38:52 +01:00
|
|
|
use tide::{Request, Response};
|
2020-11-23 05:22:51 +01:00
|
|
|
|
2021-02-20 06:46:44 +01:00
|
|
|
pub async fn handler(req: Request<()>, format: &str, params: Vec<&str>) -> tide::Result {
|
|
|
|
let mut url = format.to_string();
|
2020-11-30 03:50:29 +01:00
|
|
|
|
2021-02-20 06:46:44 +01:00
|
|
|
for name in params {
|
|
|
|
let param = req.param(name).unwrap_or_default();
|
|
|
|
url = url.replacen("{}", param, 1);
|
2020-11-24 01:57:37 +01:00
|
|
|
}
|
2021-02-20 05:50:55 +01:00
|
|
|
|
|
|
|
request(url).await
|
|
|
|
}
|
|
|
|
|
2021-02-18 19:04:59 +01:00
|
|
|
async fn request(url: String) -> tide::Result {
|
2021-02-20 21:14:32 +01:00
|
|
|
match surf::get(url).await {
|
|
|
|
Ok(res) => {
|
2021-03-09 03:49:06 +01:00
|
|
|
let content_length = res.header("Content-Length").map(std::string::ToString::to_string).unwrap_or_default();
|
2021-02-20 21:14:32 +01:00
|
|
|
let content_type = res.content_type().map(|m| m.to_string()).unwrap_or_default();
|
2021-02-18 19:04:59 +01:00
|
|
|
|
2021-02-20 21:14:32 +01:00
|
|
|
Ok(
|
|
|
|
Response::builder(res.status())
|
|
|
|
.body(Body::from_reader(res, None))
|
|
|
|
.header("Cache-Control", "public, max-age=1209600, s-maxage=86400")
|
|
|
|
.header("Content-Length", content_length)
|
|
|
|
.header("Content-Type", content_type)
|
|
|
|
.build(),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
Err(e) => Ok(Response::builder(503).body(e.to_string()).build()),
|
|
|
|
}
|
2021-02-18 19:04:59 +01:00
|
|
|
}
|