Reputation: 71
In my code I have this main function:
#[actix_web::main]
async fn main() -> Result<(), StdErr> {
// loads env variables from .env
dotenv::dotenv().ok();
actix_web::HttpServer::new(move || {
let cors = actix_cors::Cors::default()
//.allowed_origin(
// &(std::env::var("SERVER_URL").unwrap().to_string()+ ":" + &std::env::var("FROTEND").unwrap().to_string())
//)
.allow_any_origin()
.allowed_methods(vec!["GET","POST","PUT"])
.allowed_headers(vec![
actix_web::http::header::AUTHORIZATION,
actix_web::http::header::ACCEPT
])
.allowed_header(
actix_web::http::header::CONTENT_TYPE
)
.max_age(3600);
//logger::init();
let party_repo = repo::PartyRepo::PartyRepo::connect();
actix_web::App::new()
.app_data(party_repo)
.wrap(
cors
)
.service(controllers::PartyController::party_api())
})
.bind((std::env::var("SERVER_URL").unwrap().to_string(),
std::env::var("PARTY_CONTROLLER_PORT").unwrap()
.parse::<u16>().unwrap()))?
.run()
.await?;
Ok(())
}
This compiles correctly; however, when I run the server and try to make a get request I receive a 500 error from postman that: App data is not configured, to configure use App::data()
.
Now I am new to rust, so I maybe missing something obvious. That being said my understanding was that App.data()
was used primarily with Arc
. I tried this yet I receive the same error.
Let me know if I should include more :) Thanks!
Upvotes: 3
Views: 4747
Reputation: 56
Look at the examples in the docs for Data. It seems you have to put your initialized struct in a Mutex
and then add that to your App
. Also beware that you need to include it in your path fn
as a Mutex as well.
let data = Data::new(Mutex::new(YOUR_DATA))
app.app_data(Data::clone(&data));
async fn index(data: Data<Mutex<YOUR_DATA_TYPE>>) -> impl Responder {
}
Upvotes: 4