2021-02-19 21:46:44 -08:00
|
|
|
use surf::Body;
|
2021-02-09 09:38:52 -08:00
|
|
|
use tide::{Request, Response};
|
2020-11-22 20:22:51 -08:00
|
|
|
|
2021-02-19 21:46:44 -08:00
|
|
|
pub async fn handler(req: Request<()>, format: &str, params: Vec<&str>) -> tide::Result {
|
|
|
|
let mut url = format.to_string();
|
2020-11-29 18:50:29 -08:00
|
|
|
|
2021-02-19 21:46:44 -08:00
|
|
|
for name in params {
|
|
|
|
let param = req.param(name).unwrap_or_default();
|
|
|
|
url = url.replacen("{}", param, 1);
|
2020-11-23 16:57:37 -08:00
|
|
|
}
|
2021-02-19 20:50:55 -08:00
|
|
|
|
|
|
|
request(url).await
|
|
|
|
}
|
|
|
|
|
2021-02-18 10:04:59 -08:00
|
|
|
async fn request(url: String) -> tide::Result {
|
2021-02-20 12:14:32 -08:00
|
|
|
match surf::get(url).await {
|
|
|
|
Ok(res) => {
|
|
|
|
let content_length = res.header("Content-Length").map(|v| v.to_string()).unwrap_or_default();
|
|
|
|
let content_type = res.content_type().map(|m| m.to_string()).unwrap_or_default();
|
2021-02-18 10:04:59 -08:00
|
|
|
|
2021-02-20 12:14:32 -08: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 10:04:59 -08:00
|
|
|
}
|