route-util 0.1.0

route utils
Documentation
use http_pool::body::{VariantBody, variant_body};
use hyper::body::Bytes;
use hyper::{Response, StatusCode};

/// 文本内容
pub static TEXT_CONTENT_TYPE: &str = "text/plain; charset=utf-8";
/// json内容
pub static JSON_CONTENT_TYPE: &str = "application/json";

pub fn response<B: Into<String>>(
    code: StatusCode,
    body: Option<B>,
    content_type: &str,
) -> Response<VariantBody> {
    let builder = Response::builder()
        .status(code)
        .header("Content-Type", content_type);
    if let Some(b) = body {
        let b = b.into();
        builder
            .header("Content-Length", b.len().to_string())
            .body(variant_body(http_pool::body::Full::new(Bytes::from(b))))
            .unwrap()
    } else {
        builder.body(http_pool::body::empty()).unwrap()
    }
}