1
0
mirror of https://github.com/actix/examples synced 2025-02-09 04:15:37 +01:00

35 lines
938 B
Rust
Raw Normal View History

2021-10-06 23:12:42 +01:00
use actix_protobuf::{ProtoBuf, ProtoBufResponseBuilder as _};
use actix_web::{middleware, web, App, HttpResponse, HttpServer, Result};
2022-06-07 22:53:28 -04:00
use prost_derive::Message;
#[derive(Clone, PartialEq, Message)]
pub struct MyObj {
2018-05-08 11:08:43 -07:00
#[prost(int32, tag = "1")]
pub number: i32,
2020-09-12 16:49:45 +01:00
2018-05-08 11:08:43 -07:00
#[prost(string, tag = "2")]
pub name: String,
}
async fn index(msg: ProtoBuf<MyObj>) -> Result<HttpResponse> {
log::info!("model: {msg:?}");
HttpResponse::Ok().protobuf(msg.0) // <- send response
}
2020-09-12 16:49:45 +01:00
#[actix_web::main]
async fn main() -> std::io::Result<()> {
2022-02-17 20:22:36 +00:00
env_logger::init_from_env(env_logger::Env::new().default_filter_or("info"));
log::info!("starting HTTP server at http://localhost:8080");
2019-03-30 09:12:42 -07:00
HttpServer::new(|| {
App::new()
.service(web::resource("/").route(web::post().to(index)))
2022-02-17 20:22:36 +00:00
.wrap(middleware::Logger::default())
2019-07-11 15:02:25 +06:00
})
2022-02-17 20:22:36 +00:00
.workers(1)
.bind(("127.0.0.1", 8080))?
.run()
.await
}