-
-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
83 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
use std::io; | ||
|
||
use actix_web::{get, http::StatusCode, App, HttpServer, Responder}; | ||
use actix_web_lab::respond::Cbor; | ||
use serde::Serialize; | ||
use tracing::info; | ||
|
||
#[derive(Debug, Serialize)] | ||
struct Test { | ||
one: u32, | ||
two: String, | ||
} | ||
|
||
#[get("/")] | ||
async fn index() -> impl Responder { | ||
Cbor(Test { | ||
one: 42, | ||
two: "two".to_owned(), | ||
}) | ||
} | ||
|
||
#[actix_web::main] | ||
async fn main() -> io::Result<()> { | ||
env_logger::init_from_env(env_logger::Env::new().default_filter_or("info")); | ||
|
||
let bind = ("127.0.0.1", 8080); | ||
info!("staring server at http://{}:{}", &bind.0, &bind.1); | ||
|
||
HttpServer::new(|| App::new().service(index)) | ||
.workers(1) | ||
.bind(bind)? | ||
.run() | ||
.await | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
//! CBOR responder. | ||
use actix_web::{HttpRequest, HttpResponse, Responder}; | ||
use bytes::Bytes; | ||
use derive_more::{Deref, DerefMut, Display}; | ||
use mime::Mime; | ||
use once_cell::sync::Lazy; | ||
use serde::Serialize; | ||
|
||
static CBOR_MIME: Lazy<Mime> = Lazy::new(|| "application/cbor".parse().unwrap()); | ||
|
||
/// CBOR responder. | ||
#[cfg_attr(docsrs, doc(cfg(feature = "cbor")))] | ||
#[derive(Debug, Deref, DerefMut, Display)] | ||
pub struct Cbor<T>(pub T); | ||
|
||
impl<T: Serialize> Responder for Cbor<T> { | ||
type Body = Bytes; | ||
|
||
fn respond_to(self, _req: &HttpRequest) -> HttpResponse<Self::Body> { | ||
let body = Bytes::from(serde_cbor_2::to_vec(&self.0).unwrap()); | ||
|
||
HttpResponse::Ok() | ||
.content_type(CBOR_MIME.clone()) | ||
.message_body(body) | ||
.unwrap() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters