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;
|
2018-04-13 09:18:42 +08:00
|
|
|
|
2022-07-09 21:05:06 +01:00
|
|
|
#[derive(Clone, PartialEq, Eq, Message)]
|
2018-04-13 09:18:42 +08:00
|
|
|
pub struct MyObj {
|
2018-05-08 11:08:43 -07:00
|
|
|
#[prost(int32, tag = "1")]
|
2018-04-13 09:18:42 +08:00
|
|
|
pub number: i32,
|
2020-09-12 16:49:45 +01:00
|
|
|
|
2018-05-08 11:08:43 -07:00
|
|
|
#[prost(string, tag = "2")]
|
2018-04-13 09:18:42 +08:00
|
|
|
pub name: String,
|
|
|
|
}
|
|
|
|
|
2020-02-06 18:17:23 +01:00
|
|
|
async fn index(msg: ProtoBuf<MyObj>) -> Result<HttpResponse> {
|
2022-06-07 22:53:38 -04:00
|
|
|
log::info!("model: {msg:?}");
|
2019-05-21 22:09:27 +02:00
|
|
|
HttpResponse::Ok().protobuf(msg.0) // <- send response
|
2018-04-13 09:18:42 +08:00
|
|
|
}
|
|
|
|
|
2020-09-12 16:49:45 +01:00
|
|
|
#[actix_web::main]
|
2020-02-06 18:17:23 +01:00
|
|
|
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");
|
2018-04-13 09:18:42 +08:00
|
|
|
|
2019-03-30 09:12:42 -07:00
|
|
|
HttpServer::new(|| {
|
2018-04-13 09:18:42 +08:00
|
|
|
App::new()
|
2019-05-21 22:09:27 +02:00
|
|
|
.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))?
|
2020-02-06 18:17:23 +01:00
|
|
|
.run()
|
|
|
|
.await
|
2018-04-13 09:18:42 +08:00
|
|
|
}
|