mirror of
https://github.com/actix/actix-website
synced 2025-06-27 15:39:02 +02:00
Intro: done-ish. Getting Started: done-ish. Application: done-ish.
This commit is contained in:
@ -5,7 +5,10 @@ fn index(_req: HttpRequest) -> impl Responder {
|
||||
"Hello world!"
|
||||
}
|
||||
|
||||
#[rustfmt::skip]
|
||||
pub fn main() {
|
||||
App::new().service(web::scope("/app").route("/index.html", web::get().to(index)));
|
||||
App::new().service(
|
||||
web::scope("/app")
|
||||
.route("/index.html", web::get().to(index)));
|
||||
}
|
||||
// </setup>
|
||||
|
@ -8,13 +8,13 @@ struct State2;
|
||||
pub fn main() {
|
||||
HttpServer::new(|| {
|
||||
App::new()
|
||||
.data(State1)
|
||||
.data(State2)
|
||||
.service(
|
||||
web::scope("/app1")
|
||||
.data(State1)
|
||||
.route("/", web::to(|| HttpResponse::Ok())))
|
||||
.service(
|
||||
web::scope("/app2")
|
||||
.data(State2)
|
||||
.route("/", web::to(|| HttpResponse::Ok())))
|
||||
})
|
||||
.bind("127.0.0.1:8088")
|
||||
|
34
examples/application/src/config.rs
Normal file
34
examples/application/src/config.rs
Normal file
@ -0,0 +1,34 @@
|
||||
// <config>
|
||||
use actix_web::{web, App, HttpResponse, HttpServer};
|
||||
|
||||
// this function could be located in different module
|
||||
fn scoped_config(cfg: &mut web::ServiceConfig) {
|
||||
cfg.service(
|
||||
web::resource("/test")
|
||||
.route(web::get().to(|| HttpResponse::Ok().body("test")))
|
||||
.route(web::head().to(|| HttpResponse::MethodNotAllowed())),
|
||||
);
|
||||
}
|
||||
|
||||
// this function could be located in different module
|
||||
fn config(cfg: &mut web::ServiceConfig) {
|
||||
cfg.service(
|
||||
web::resource("/app")
|
||||
.route(web::get().to(|| HttpResponse::Ok().body("app")))
|
||||
.route(web::head().to(|| HttpResponse::MethodNotAllowed())),
|
||||
);
|
||||
}
|
||||
|
||||
pub fn main() {
|
||||
HttpServer::new(|| {
|
||||
App::new()
|
||||
.configure(config)
|
||||
.service(web::scope("/api").configure(scoped_config))
|
||||
.route("/", web::get().to(|| HttpResponse::Ok().body("/")))
|
||||
})
|
||||
.bind("127.0.0.1:8088")
|
||||
.unwrap()
|
||||
.run()
|
||||
.unwrap();
|
||||
}
|
||||
// </config>
|
@ -1,22 +1,22 @@
|
||||
use actix_web::{web, App, HttpResponse, HttpServer};
|
||||
use actix_web::{web, App, HttpResponse};
|
||||
|
||||
pub mod app;
|
||||
pub mod combine;
|
||||
pub mod config;
|
||||
pub mod scope;
|
||||
pub mod state;
|
||||
pub mod vh;
|
||||
|
||||
#[rustfmt::skip]
|
||||
// <run_server>
|
||||
// <multi>
|
||||
fn main() {
|
||||
HttpServer::new(|| {
|
||||
App::new()
|
||||
.service(
|
||||
web::scope("/app1")
|
||||
.route("/", web::to(|| HttpResponse::Ok())))
|
||||
.service(
|
||||
web::scope("/app2")
|
||||
.route("/", web::to(|| HttpResponse::Ok())))
|
||||
.route("/", web::to(|| HttpResponse::Ok()))
|
||||
});
|
||||
App::new()
|
||||
.service(
|
||||
web::scope("/app1")
|
||||
.route("/", web::to(|| HttpResponse::Ok())))
|
||||
.service(
|
||||
web::scope("/app2")
|
||||
.route("/", web::to(|| HttpResponse::Ok())))
|
||||
.route("/", web::to(|| HttpResponse::Ok()));
|
||||
}
|
||||
// </run_server>
|
||||
// </multi>
|
||||
|
15
examples/application/src/scope.rs
Normal file
15
examples/application/src/scope.rs
Normal file
@ -0,0 +1,15 @@
|
||||
use actix_web::{web, App, HttpRequest, Responder};
|
||||
|
||||
fn show_users(_req: HttpRequest) -> impl Responder {
|
||||
unimplemented!()
|
||||
}
|
||||
|
||||
#[rustfmt::skip]
|
||||
// <scope>
|
||||
pub fn main() {
|
||||
App::new()
|
||||
.service(
|
||||
web::scope("/users")
|
||||
.route("/show", web::get().to(show_users)));
|
||||
}
|
||||
// </scope>
|
@ -4,7 +4,7 @@ use std::cell::Cell;
|
||||
|
||||
// This struct represents state
|
||||
struct AppState {
|
||||
counter: Cell<usize>,
|
||||
counter: Cell<i32>,
|
||||
}
|
||||
|
||||
fn index(data: web::Data<AppState>) -> String {
|
||||
|
@ -1,22 +1,24 @@
|
||||
#![allow(unused)]
|
||||
use actix_web::{guard, web, App, HttpRequest, HttpResponse, HttpServer, Responder};
|
||||
use actix_web::{guard, web, App, HttpResponse, HttpServer};
|
||||
|
||||
// <vh>
|
||||
fn main() {
|
||||
pub fn main() {
|
||||
HttpServer::new(|| {
|
||||
App::new()
|
||||
.service(
|
||||
web::scope("/")
|
||||
.guard(guard::Header("Host", "www.rust-lang.org"))
|
||||
.route("", web::to(|| HttpResponse::Ok())),
|
||||
.route("", web::to(|| HttpResponse::Ok().body("www"))),
|
||||
)
|
||||
.service(
|
||||
web::scope("/")
|
||||
.guard(guard::Header("Host", "users.rust-lang.org"))
|
||||
.route("", web::to(|| HttpResponse::Ok())),
|
||||
.route("", web::to(|| HttpResponse::Ok().body("user"))),
|
||||
)
|
||||
.route("/", web::to(|| HttpResponse::Ok()))
|
||||
})
|
||||
.run();
|
||||
.bind("127.0.0.1:8088")
|
||||
.unwrap()
|
||||
.run()
|
||||
.unwrap();
|
||||
}
|
||||
// </vh>
|
||||
|
@ -1,17 +1,25 @@
|
||||
// <setup>
|
||||
use actix_web::{web, App, HttpRequest, HttpResponse, HttpServer, Responder};
|
||||
|
||||
fn index(_req: HttpRequest) -> impl Responder {
|
||||
fn index() -> impl Responder {
|
||||
HttpResponse::Ok().body("Hello world!")
|
||||
}
|
||||
|
||||
fn index2(_req: HttpRequest) -> impl Responder {
|
||||
HttpResponse::Ok().body("Hello world again!")
|
||||
}
|
||||
// </setup>
|
||||
|
||||
// <main>
|
||||
fn main() {
|
||||
HttpServer::new(|| App::new().route("/", web::get().to(index)))
|
||||
.bind("127.0.0.1:8088")
|
||||
.unwrap()
|
||||
.run()
|
||||
.unwrap();
|
||||
HttpServer::new(|| {
|
||||
App::new()
|
||||
.route("/", web::get().to(index))
|
||||
.route("/again", web::get().to(index2))
|
||||
})
|
||||
.bind("127.0.0.1:8088")
|
||||
.unwrap()
|
||||
.run()
|
||||
.unwrap();
|
||||
}
|
||||
// </main>
|
||||
|
Reference in New Issue
Block a user