modificando json e protobuf

This commit is contained in:
2023-11-04 17:05:55 -03:00
parent 1eb02db473
commit 95a85be35d
15 changed files with 163 additions and 65 deletions

View File

@@ -1,14 +1,31 @@
use qstring::QString;
use actix_web::{get, post, web, App, HttpResponse, HttpServer, Responder, HttpRequest, Result};
use actix_files::NamedFile;
// use actix_protobuf::{ProtoBuf, ProtoBufResponseBuilder as _};
use actix_protobuf::*;
use prost::Message;
use serde::{Deserialize, Serialize};
#[derive(Clone, PartialEq, Eq, Message)]
pub struct HelloWorld {
pub struct PersonProtobuf {
#[prost(string, tag = "1")]
pub message: String
pub name: String,
#[prost(int32, tag = "2")]
pub age: i32,
#[prost(string, repeated, tag = "3")]
pub friends: Vec<String>,
#[prost(string, tag = "4")]
pub email: String,
#[prost(string, tag = "5")]
pub phone: String,
}
#[derive(Deserialize, Serialize)]
pub struct PersonJson {
pub name: String,
pub age: i32,
pub friends: Vec<String>,
pub email: String,
pub phone: String,
}
#[get("/status/ok")]
@@ -37,20 +54,13 @@ async fn simulation_harmonic_progression(req: HttpRequest) -> impl Responder {
HttpResponse::Ok().body(format!("{}", sum))
}
#[get("/simulation/json")]
async fn simulation_json() -> impl Responder {
let body = serde_json::json!({
"message": "Hello World!"
});
HttpResponse::Ok()
.json(body)
#[post("/simulation/json")]
async fn simulation_json(msg: web::Json<PersonJson>) -> impl Responder {
HttpResponse::Ok().json(web::Json(msg))
}
#[post("/simulation/protobuf")]
async fn simulation_protobuf(msg: ProtoBuf<HelloWorld>) -> impl Responder {
println!("model: {:?}", msg.0);
async fn simulation_protobuf(msg: ProtoBuf<PersonProtobuf>) -> impl Responder {
HttpResponse::Ok().protobuf(msg.0)
}