From c6e6a9b5fe50a8a3a7cdc8547b0b54d299f84887 Mon Sep 17 00:00:00 2001 From: Valentin Brandl Date: Tue, 16 Apr 2019 21:37:39 +0200 Subject: [PATCH] Implement streaming responses --- src/main.rs | 26 ++++++++++++++------------ 1 file changed, 14 insertions(+), 12 deletions(-) diff --git a/src/main.rs b/src/main.rs index 59a38bc..aad6d85 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,13 +1,10 @@ #[macro_use] extern crate actix_web; -extern crate badge; -extern crate git2; -extern crate openssl_probe; -extern crate pretty_env_logger; -extern crate structopt; -use actix_web::{http::StatusCode, middleware, web, App, HttpResponse, HttpServer, ResponseError}; +use actix_web::{error, middleware, web, App, HttpResponse, HttpServer, ResponseError}; use badge::{Badge, BadgeOptions}; +use bytes::Bytes; +use futures::{unsync::mpsc, Stream}; use git2::{Repository, ResetType}; use std::{ path::{Path, PathBuf}, @@ -18,6 +15,8 @@ use structopt::StructOpt; type State = Arc; +const INDEX: &str = include_str!("../static/index.html"); + #[derive(StructOpt, Debug)] struct Opt { #[structopt( @@ -142,9 +141,11 @@ fn calculate_hoc( status: hoc.to_string(), }; let badge = Badge::new(badge_opt)?; - Ok(HttpResponse::Ok() - .content_type("image/svg+xml") - .body(badge.to_svg())) + + let (tx, rx_body) = mpsc::unbounded(); + let _ = tx.unbounded_send(Bytes::from(badge.to_svg().as_bytes())); + + Ok(HttpResponse::Ok().streaming(rx_body.map_err(|_| error::ErrorBadRequest("bad request")))) } fn github( @@ -170,9 +171,10 @@ fn bitbucket( #[get("/")] fn index() -> HttpResponse { - HttpResponse::build(StatusCode::OK) - .content_type("text/html; charset=utf-8") - .body(include_str!("../static/index.html")) + let (tx, rx_body) = mpsc::unbounded(); + let _ = tx.unbounded_send(Bytes::from(INDEX.as_bytes())); + + HttpResponse::Ok().streaming(rx_body.map_err(|_| error::ErrorBadRequest("bad request"))) } fn main() -> std::io::Result<()> {