I am using the NormalizePath middleware component with the trailing slash style set to "Trim". I have connected two endpoints using an actix service and one endpoint wrapped inside an extra scope. This setup can be recreated using the following codeblocks:
use actix_web::{middleware, web, App, HttpServer};
use minimal_test::{api_version, echo, welcome};
#[actix_web::main]
async fn main() -> std::io::Result<()> {
HttpServer::new(move || {
App::new()
.wrap(middleware::NormalizePath::new(
middleware::TrailingSlash::Trim,
))
.service(welcome)
.service(echo)
.service(web::scope("/api").service(api_version))
})
.bind(("127.0.0.1", 8080))?
.run()
.await
}
use actix_web::{get, post, HttpResponse, Responder};
// Base endpoints
#[get("/")]
pub async fn welcome() -> impl Responder {
HttpResponse::Ok().body("Welcome to aayojak!")
}
#[post("/echo")]
pub async fn echo(req_body: String) -> impl Responder {
HttpResponse::Ok().body(req_body)
}
#[get("/")]
pub async fn api_version() -> impl Responder {
HttpResponse::Ok().body("v0.0.2")
}
After running up the server I can reach the base "/" and "/echo" contexts as expected, however when I try to call the api_version request, I get a 404 not found at http://localhost:8080/api. After playing around a bit I found out that if I change the context route of the api_version function to, for example, "/api", the endpoint can successfully be called under http://localhost:8080/api/api and http://localhost:8080/api/api/ which is what I wanted from the very start.
So the function after changing the context route will become
#[get("/api")]
pub async fn api_version() -> impl Responder {
HttpResponse::Ok().body("v0.0.2")
}
Could someone please help me out a bit and explain this happens? Is it a bug or am I not using the components as they're supposed to be used?