2019-06-20 08:04:22 +02:00
|
|
|
// <responder-trait>
|
2019-06-28 19:31:30 +02:00
|
|
|
use actix_web::{Error, HttpRequest, HttpResponse, Responder};
|
2019-06-15 22:37:08 +02:00
|
|
|
use serde::Serialize;
|
2019-12-28 18:47:27 +01:00
|
|
|
use futures::future::{ready, Ready};
|
2019-06-15 22:37:08 +02:00
|
|
|
|
|
|
|
#[derive(Serialize)]
|
|
|
|
struct MyObj {
|
|
|
|
name: &'static str,
|
|
|
|
}
|
|
|
|
|
|
|
|
// Responder
|
|
|
|
impl Responder for MyObj {
|
|
|
|
type Error = Error;
|
2019-12-28 18:47:27 +01:00
|
|
|
type Future = Ready<Result<HttpResponse, Error>>;
|
2019-06-15 22:37:08 +02:00
|
|
|
|
|
|
|
fn respond_to(self, _req: &HttpRequest) -> Self::Future {
|
2019-12-28 18:47:27 +01:00
|
|
|
let body = serde_json::to_string(&self).unwrap();
|
2019-06-15 22:37:08 +02:00
|
|
|
|
|
|
|
// Create response and set content type
|
2019-12-28 18:47:27 +01:00
|
|
|
ready(Ok(HttpResponse::Ok()
|
2019-06-15 22:37:08 +02:00
|
|
|
.content_type("application/json")
|
2019-12-28 18:47:27 +01:00
|
|
|
.body(body)))
|
2019-06-15 22:37:08 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-28 18:47:27 +01:00
|
|
|
async 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
|
|
|
|
2019-12-28 18:47:27 +01:00
|
|
|
#[actix_rt::main]
|
|
|
|
async fn main() -> std::io::Result<()> {
|
2019-06-28 19:31:30 +02:00
|
|
|
use actix_web::{web, App, HttpServer};
|
|
|
|
|
2019-06-15 22:37:08 +02:00
|
|
|
HttpServer::new(|| App::new().route("/", web::get().to(index)))
|
2019-12-28 18:47:27 +01:00
|
|
|
.bind("127.0.0.1:8088")?
|
2019-06-15 22:37:08 +02:00
|
|
|
.run()
|
2019-12-28 18:47:27 +01:00
|
|
|
.await
|
2019-06-15 22:37:08 +02:00
|
|
|
}
|