이 글은 Acticx Web의 공식문서에 있는 내용을 따라하여 웹 앱 제작을 연습하는 글입니다.
모든
actix-web
서버는App
인스턴스를 중심으로 만들어진다.
애플리케이션의scope
는 모든 route에 대해 namespace로서 기능한다. 예를 들어, 특정 애플리케이션 scope의 route들은 같은 url path prefix를 가진다. 애플리케이션의 prefix는 언제나 "/"로 시작한다. 만약 "/"이 없다면 자동으로 삽입된다.
route의 앞에 "/"가 붙지 않으면, 애플리케이션에서 해당 route를 처리할 때 자동으로 "/"를 앞에 붙이는 것 같다.
#[actix_web::main]
async fn main() -> std::io::Result<()> {
HttpServer::new(|| {
App::new()
.service(hello)
.service(echo)
.route("/hey", web::get().to(manual_hello))
.route("test", web::get().to(manual_test))
})
.bind(("127.0.0.1", 8080))?
.run()
.await
}
"/hey"나 "test"나, 모두 localhost:8080/hey, localhost:8080/test 로 들어갔을 시 정상적으로 작동됨을 확인하였다.
scope
/app
의 경우/app
,/app/
,/app/test
경로의 request가 해당된다.
#[actix_web::main]
async fn main() -> std::io::Result<()> {
HttpServer::new(|| {
App::new()
.service(hello)
.service(echo)
.route("/hey", web::get().to(manual_hello))
.service(
web::scope("/app")
.route("/index.html", web::get().to(index))
.route("/test", web::get().to(test))
.route("/test/to.html", web::get().to(to))
)
})
.bind(("127.0.0.1", 8080))?
.run()
.await
}
"/app" scope를 만들고, 그 하위에 "/index.html", "/test", "test/to.html" route를 만들고, "Hello world!", "Test!", "route test!"를 return하는 함수를 각각 만들었다. 그리고 "http://localhost:8080/app/index.html", "http://localhost:8080/app/test", "http://localhost:8080/app/test/to.html"에 진입하니 각각의 &str을 return하는 것을 확인할 수 있었다.