pub struct Builder<G: Gate> { /* private fields */ }Expand description
Declares an application. Every mistake is reported by
build at once, before anything listens.
Implementations§
Source§impl<G: Gate> Builder<G>
impl<G: Gate> Builder<G>
Sourcepub fn route(self, route: Route<G::Identity, G::Rule>) -> Self
pub fn route(self, route: Route<G::Identity, G::Rule>) -> Self
Adds a route.
Examples found in repository?
14fn main() -> Result<(), Box<dyn std::error::Error>> {
15 let app = App::builder()
16 .host("127.0.0.1:8091")
17 .development()
18 .base("/app")
19 .route(
20 get("/", |cx| {
21 let users = cx.url("/users/7");
22 cx.html(
23 200,
24 &format!("<h1>Hello</h1><a href=\"{users}\">user 7</a>"),
25 );
26 Ok(())
27 })
28 .public(),
29 )
30 .route(
31 get("/users/:id", |cx| {
32 let id: u64 = cx.param_as("id")?;
33 cx.text(200, &format!("user {id}"));
34 Ok(())
35 })
36 .public(),
37 )
38 .route(
39 Route::at("/contact")
40 .get(|cx| {
41 cx.html(200, "<form method=post><button>Send</button></form>");
42 Ok(())
43 })
44 .post(|cx| cx.see_other("/"))
45 .public(),
46 )
47 .build()?;
48
49 let server = serve_net::Server::builder()
50 .listener(serve_net::ListenerSpec::tcp("web", "127.0.0.1:8091"))
51 .serve(app)?;
52 println!("listening on http://127.0.0.1:8091/app/");
53 server.wait();
54 Ok(())
55}Sourcepub fn hook<F>(self, hook: Hook, routes: Routes, f: F) -> Self
pub fn hook<F>(self, hook: Hook, routes: Routes, f: F) -> Self
Runs f at hook for the routes routes selects.
Sourcepub fn host(self, host: &str) -> Self
pub fn host(self, host: &str) -> Self
A host the site answers (example.com, or localhost:8080 with its
port). The first one is canonical. With no host declared the site
answers any host; with hosts declared, any other host gets 421.
Examples found in repository?
14fn main() -> Result<(), Box<dyn std::error::Error>> {
15 let app = App::builder()
16 .host("127.0.0.1:8091")
17 .development()
18 .base("/app")
19 .route(
20 get("/", |cx| {
21 let users = cx.url("/users/7");
22 cx.html(
23 200,
24 &format!("<h1>Hello</h1><a href=\"{users}\">user 7</a>"),
25 );
26 Ok(())
27 })
28 .public(),
29 )
30 .route(
31 get("/users/:id", |cx| {
32 let id: u64 = cx.param_as("id")?;
33 cx.text(200, &format!("user {id}"));
34 Ok(())
35 })
36 .public(),
37 )
38 .route(
39 Route::at("/contact")
40 .get(|cx| {
41 cx.html(200, "<form method=post><button>Send</button></form>");
42 Ok(())
43 })
44 .post(|cx| cx.see_other("/"))
45 .public(),
46 )
47 .build()?;
48
49 let server = serve_net::Server::builder()
50 .listener(serve_net::ListenerSpec::tcp("web", "127.0.0.1:8091"))
51 .serve(app)?;
52 println!("listening on http://127.0.0.1:8091/app/");
53 server.wait();
54 Ok(())
55}Sourcepub fn alias(self, host: &str) -> Self
pub fn alias(self, host: &str) -> Self
A host that is redirected (301, or 308 for unsafe methods) to the
canonical host (www.example.com to example.com).
Sourcepub fn development(self) -> Self
pub fn development(self) -> Self
A development instance: localhost, 127.0.0.1, [::1], and
*.localhost are answered as the site (with any port), and HTTPS is
not required for them. Never set this in production: it is the only
way a Host: localhost is trusted.
Examples found in repository?
14fn main() -> Result<(), Box<dyn std::error::Error>> {
15 let app = App::builder()
16 .host("127.0.0.1:8091")
17 .development()
18 .base("/app")
19 .route(
20 get("/", |cx| {
21 let users = cx.url("/users/7");
22 cx.html(
23 200,
24 &format!("<h1>Hello</h1><a href=\"{users}\">user 7</a>"),
25 );
26 Ok(())
27 })
28 .public(),
29 )
30 .route(
31 get("/users/:id", |cx| {
32 let id: u64 = cx.param_as("id")?;
33 cx.text(200, &format!("user {id}"));
34 Ok(())
35 })
36 .public(),
37 )
38 .route(
39 Route::at("/contact")
40 .get(|cx| {
41 cx.html(200, "<form method=post><button>Send</button></form>");
42 Ok(())
43 })
44 .post(|cx| cx.see_other("/"))
45 .public(),
46 )
47 .build()?;
48
49 let server = serve_net::Server::builder()
50 .listener(serve_net::ListenerSpec::tcp("web", "127.0.0.1:8091"))
51 .serve(app)?;
52 println!("listening on http://127.0.0.1:8091/app/");
53 server.wait();
54 Ok(())
55}Sourcepub fn base(self, base: &str) -> Self
pub fn base(self, base: &str) -> Self
The path the site is installed under (/blog). It is removed before
routing and added back by Cx::url, so routes and links are written
as if the site were at /. Requests outside it get 404.
Examples found in repository?
14fn main() -> Result<(), Box<dyn std::error::Error>> {
15 let app = App::builder()
16 .host("127.0.0.1:8091")
17 .development()
18 .base("/app")
19 .route(
20 get("/", |cx| {
21 let users = cx.url("/users/7");
22 cx.html(
23 200,
24 &format!("<h1>Hello</h1><a href=\"{users}\">user 7</a>"),
25 );
26 Ok(())
27 })
28 .public(),
29 )
30 .route(
31 get("/users/:id", |cx| {
32 let id: u64 = cx.param_as("id")?;
33 cx.text(200, &format!("user {id}"));
34 Ok(())
35 })
36 .public(),
37 )
38 .route(
39 Route::at("/contact")
40 .get(|cx| {
41 cx.html(200, "<form method=post><button>Send</button></form>");
42 Ok(())
43 })
44 .post(|cx| cx.see_other("/"))
45 .public(),
46 )
47 .build()?;
48
49 let server = serve_net::Server::builder()
50 .listener(serve_net::ListenerSpec::tcp("web", "127.0.0.1:8091"))
51 .serve(app)?;
52 println!("listening on http://127.0.0.1:8091/app/");
53 server.wait();
54 Ok(())
55}Sourcepub fn trailing_slash(self, policy: TrailingSlash) -> Self
pub fn trailing_slash(self, policy: TrailingSlash) -> Self
The trailing-slash policy (default TrailingSlash::Strip).
Sourcepub fn lowercase_paths(self) -> Self
pub fn lowercase_paths(self) -> Self
Paths are lower case: one with an upper-case letter is redirected.
Sourcepub fn https_only(self) -> Self
pub fn https_only(self) -> Self
Plain HTTP is redirected to HTTPS on the canonical host (behind a TLS proxy, serve-net must trust the proxy’s scheme). Needs a host.
Sourcepub fn sign_in(self, path: &str) -> Self
pub fn sign_in(self, path: &str) -> Self
The site-relative sign-in page. A Denial::sign_in on a GET or
HEAD is then answered with a 303 to it, the page asked for passed
as next; without it, or for other methods, the answer is 401.
Sourcepub fn challenge(self, challenge: &str) -> Self
pub fn challenge(self, challenge: &str) -> Self
The WWW-Authenticate challenge sent with a 401 (default
Session).
Sourcepub fn events(self, events: impl Events) -> Self
pub fn events(self, events: impl Events) -> Self
Where refusals, denials, and failures are reported.
Sourcepub fn checkpoint(self, checkpoint: impl Checkpoint) -> Self
pub fn checkpoint(self, checkpoint: impl Checkpoint) -> Self
The in-flight revocation check at web-net’s stages.
Sourcepub fn page_limit(self, bytes: usize) -> Self
pub fn page_limit(self, bytes: usize) -> Self
The budget for one rendered page (default 8 MiB).
Sourcepub fn wipe_buffers(self) -> Self
pub fn wipe_buffers(self) -> Self
Zero the reply buffers web-net reuses after every request, so one visitor’s page never lingers in memory another request reuses. Costs one pass over the bytes written.
Sourcepub fn build(self) -> Result<App<G>, BuildError>
pub fn build(self) -> Result<App<G>, BuildError>
Compiles the application.
§Errors
A BuildError listing every problem: a route without declared
access, a malformed pattern, two handlers for one method of one
path, an unknown template, a malformed host or base path.
Examples found in repository?
14fn main() -> Result<(), Box<dyn std::error::Error>> {
15 let app = App::builder()
16 .host("127.0.0.1:8091")
17 .development()
18 .base("/app")
19 .route(
20 get("/", |cx| {
21 let users = cx.url("/users/7");
22 cx.html(
23 200,
24 &format!("<h1>Hello</h1><a href=\"{users}\">user 7</a>"),
25 );
26 Ok(())
27 })
28 .public(),
29 )
30 .route(
31 get("/users/:id", |cx| {
32 let id: u64 = cx.param_as("id")?;
33 cx.text(200, &format!("user {id}"));
34 Ok(())
35 })
36 .public(),
37 )
38 .route(
39 Route::at("/contact")
40 .get(|cx| {
41 cx.html(200, "<form method=post><button>Send</button></form>");
42 Ok(())
43 })
44 .post(|cx| cx.see_other("/"))
45 .public(),
46 )
47 .build()?;
48
49 let server = serve_net::Server::builder()
50 .listener(serve_net::ListenerSpec::tcp("web", "127.0.0.1:8091"))
51 .serve(app)?;
52 println!("listening on http://127.0.0.1:8091/app/");
53 server.wait();
54 Ok(())
55}