Serve gists

This commit is contained in:
Valentin Brandl
2019-08-05 21:15:51 +02:00
parent 851697f900
commit 493b5e8111
2 changed files with 32 additions and 47 deletions

View File

@ -85,6 +85,34 @@ fn handle_request<T: Service>(
}
}
fn serve_gist(
client: web::Data<Client>,
data: web::Path<FilePath>,
) -> impl Future<Item = HttpResponse, Error = Error> {
let url = format!(
"https://gist.github.com/{}/{}/raw/{}/{}",
data.user, data.repo, data.commit, data.file
);
client
.get(url)
.header(header::USER_AGENT, statics::USER_AGENT.as_str())
.send()
.from_err()
.and_then(move |response| match response.status() {
StatusCode::OK => {
let mime = mime_guess::guess_mime_type(&data.file);
Ok(HttpResponse::Ok()
.content_type(mime.to_string().as_str())
.set(CacheControl(vec![
CacheDirective::Public,
CacheDirective::MaxAge(2_592_000_000),
]))
.streaming(response))
}
code => Ok(HttpResponse::build(code).finish()),
})
}
#[get("/favicon.ico")]
fn favicon32() -> HttpResponse {
HttpResponse::Ok()
@ -139,6 +167,10 @@ fn main() -> Result<()> {
"/gitlab/{user}/{repo}/{commit}/{file:.*}",
web::get().to_async(handle_request::<GitLab>),
)
.route(
"/gist/{user}/{repo}/{commit}/{file:.*}",
web::get().to_async(serve_gist),
)
.route("/gitlab/{file:.*}", web::delete().to_async(dbg::<GitLab>))
.service(actix_files::Files::new("/", "./public").index_file("index.html"))
})