I'm encountering an error while running my API server using Ntex:
Error Message:
App state is not configured, to configure use App::state()
I have already configured the application state using App::state()
, but the error persists.
My server configuration is as follows
#[ntex::main]
async fn main() -> std::io::Result<()> {
dotenv().ok();
let pool = Arc::new(Mutex::new(database::establish_connection()));
let addr = SocketAddr::from(([0, 0, 0, 0], 8082));
let user_repository = Arc::new(Mutex::new(UserRepository::new(pool.clone())));
let user_service = UserService::new(user_repository);
HttpServer::new(move || {
App::new()
.state(user_service.clone())
.service(handle_login)
.service(handle_register)
.wrap(Logger::default())
})
.bind(addr)?
.run()
.await
}
Here’s a link to my server configuration in main.rs
:
GitHub Link to Code
Clone the repository: mt-auth-service.
Run the server using cargo run
.
Attempt to hit the /login
endpoint (or any other endpoint).
The server should process the request and return the appropriate response.
The server throws the error mentioned above when processing the request.
I was able to add data to the database earlier.
The error started occurring after refactoring my code.
Any insights into why this might be happening or how to debug it further would be greatly appreciated!
Initially, my UserService
struct was defined like this:
struct UserService {
repo: UserRepository,
}
To address the issue, I refactored it to use a thread-safe wrapper:
struct UserService {
repo: Arc<Mutex<UserRepository>>,
}
I made a similar change to the UserRepository
struct as well, wrapping its dependencies in Arc<Mutex<...>>
.
Despite these changes, the error persists.
The error has been fixed as my imports were incorrect , I was not using use crate::controllers..
, I was directly importing use controllers::..
, The working code is at this commit