2019-06-20 08:04:22 +02:00
|
|
|
// <responder-trait>
|
2019-06-15 22:37:08 +02:00
|
|
|
use actix_web::{web, App, Error, HttpRequest, HttpResponse, HttpServer, Responder};
|
|
|
|
use serde::Serialize;
|
|
|
|
|
|
|
|
#[derive(Serialize)]
|
|
|
|
struct MyObj {
|
|
|
|
name: &'static str,
|
|
|
|
}
|
|
|
|
|
|
|
|
// Responder
|
|
|
|
impl Responder for MyObj {
|
|
|
|
type Error = Error;
|
|
|
|
type Future = Result<HttpResponse, Error>;
|
|
|
|
|
|
|
|
fn respond_to(self, _req: &HttpRequest) -> Self::Future {
|
|
|
|
let body = serde_json::to_string(&self)?;
|
|
|
|
|
|
|
|
// Create response and set content type
|
|
|
|
Ok(HttpResponse::Ok()
|
|
|
|
.content_type("application/json")
|
|
|
|
.body(body))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-20 08:04:22 +02:00
|
|
|
fn index() -> impl Responder {
|
2019-06-15 22:37:08 +02:00
|
|
|
MyObj { name: "user" }
|
|
|
|
}
|
2019-06-20 08:04:22 +02:00
|
|
|
// </responder-trait>
|
2019-06-15 22:37:08 +02:00
|
|
|
|
|
|
|
fn main() {
|
|
|
|
HttpServer::new(|| App::new().route("/", web::get().to(index)))
|
|
|
|
.bind("127.0.0.1:8088")
|
|
|
|
.unwrap()
|
|
|
|
.run()
|
|
|
|
.unwrap();
|
|
|
|
}
|