'Passing a struct pointer/ownership between Responder implementing functions in actix server
This is my first project in Rust and I am still trying to learn the basics. I am implementing a simple server to get GPS coordinates. This is all preliminary, and HTTP requests will be implemented later. Essentially, I want to have two endpoints /setUserInfo and /getUserInfo. I only have one user in this application and I wish to store it in a persistent struct that can be accessed by both handlers.
use actix_web::{get, web, App, HttpServer, Responder, HttpResponse, post};
struct UserInfo {
x: i32,
y: i32,
angle: f32,
lat: f32,
lon: f32
}
#[get("/user/userInfo")]
async fn getUserInfo(user_info::UserInfo) -> impl Responder {
format!("{},{},{},{},{}", user_info.x, user_info.y, user_info.angle, user_info.lat, user_info.lon )
}
#[actix_web::main]
async fn main() -> std::io::Result<()> {
let current_user_info = UserInfo{
x: 0,
y: 0,
angle: 0.0,
lat: 0.0,
lon: 0.0
};
HttpServer::new(|| {
App::new()
.route("/user", web::get().to(|| async {"userServer API"}))
.service(getUserInfo)
})
.bind(("127.0.0.1", 8080))?
.run()
.await
}
I know this doesn't work as I cannot pass parameters using .service() or anything nor can I declare a global variable. What is the best way to go about this?
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
