mirror of
https://github.com/actix/actix-website
synced 2024-12-01 03:24:36 +01:00
38 lines
834 B
Rust
38 lines
834 B
Rust
|
// <main>
|
||
|
use actix_web::{web, App, Error, HttpRequest, HttpResponse, HttpServer, Responder};
|
||
|
use serde::Serialize;
|
||
|
use serde_json;
|
||
|
|
||
|
#[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))
|
||
|
}
|
||
|
}
|
||
|
|
||
|
fn index(_req: HttpRequest) -> impl Responder {
|
||
|
MyObj { name: "user" }
|
||
|
}
|
||
|
|
||
|
fn main() {
|
||
|
HttpServer::new(|| App::new().route("/", web::get().to(index)))
|
||
|
.bind("127.0.0.1:8088")
|
||
|
.unwrap()
|
||
|
.run()
|
||
|
.unwrap();
|
||
|
}
|
||
|
// </main>
|