pub struct App<G: Gate = Open> { /* private fields */ }Expand description
A web application: one site, its routes, and its gate. Built once at
start-up with App::builder or App::with_gate; immutable after.
It is a serve_net::HttpService, so it runs on serve-net as it is, and
it has respond and
respond_identified for a wrapper that
calls it from its own service.
use web_net::{App, get};
let app = App::builder()
.route(get("/", |cx| { cx.html(200, "<h1>Hello</h1>"); Ok(()) }).public())
.route(get("/users/:id", |cx| {
let id: u64 = cx.param_as("id")?;
cx.text(200, &format!("user {id}"));
Ok(())
}).public())
.build()?;
let reply = serve_net::testing::respond(&app, b"GET /users/7 HTTP/1.1\r\nHost: a\r\n\r\n");
assert_eq!((reply.status, reply.text()), (200, "user 7"));
let reply = serve_net::testing::respond(&app, b"GET /users/7/ HTTP/1.1\r\nHost: a\r\n\r\n");
assert_eq!(reply.status, 301);
assert_eq!(reply.header("location"), Some(&b"/users/7"[..]));Implementations§
Source§impl App<Open>
impl App<Open>
Sourcepub fn builder() -> Builder<Open>
pub fn builder() -> Builder<Open>
A builder with the Open gate: every visitor is a guest and every
route is declared public.
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 // A form that changes state gets Builder::csrf(..) and a gate
47 // whose identities carry a CSRF binding instead.
48 .csrf_exempt("demo: the POST changes nothing"),
49 )
50 .build()?;
51
52 let server = serve_net::Server::builder()
53 .listener(serve_net::ListenerSpec::tcp("web", "127.0.0.1:8091"))
54 .serve(app)?;
55 println!("listening on http://127.0.0.1:8091/app/");
56 server.wait();
57 Ok(())
58}Source§impl<G: Gate> App<G>
impl<G: Gate> App<G>
Sourcepub fn respond(
&self,
req: &HttpRequest<'_>,
env: &Envelope,
reply: &mut HttpReply,
) -> Outcome
pub fn respond( &self, req: &HttpRequest<'_>, env: &Envelope, reply: &mut HttpReply, ) -> Outcome
Runs the whole pipeline for one request, authentication included.
Sourcepub fn serve(
&self,
req: &HttpRequest<'_>,
env: &Envelope,
who: Option<&G::Identity>,
reply: &mut HttpReply,
) -> Served
pub fn serve( &self, req: &HttpRequest<'_>, env: &Envelope, who: Option<&G::Identity>, reply: &mut HttpReply, ) -> Served
Runs the pipeline and reports what ran and whether a cache in front
may keep the reply for guests. who is Some when the caller
authenticated already (as for
respond_identified).
Sourcepub fn respond_identified(
&self,
req: &HttpRequest<'_>,
env: &Envelope,
who: &G::Identity,
reply: &mut HttpReply,
) -> Outcome
pub fn respond_identified( &self, req: &HttpRequest<'_>, env: &Envelope, who: &G::Identity, reply: &mut HttpReply, ) -> Outcome
Runs the pipeline for a request whose caller authenticated it
already: web-net takes who as given and does not call the gate’s
authenticate. Authorization still runs for every route that is
not public.
Sourcepub fn classify(&self, head: &HttpHead<'_>) -> Classified
pub fn classify(&self, head: &HttpHead<'_>) -> Classified
Looks at a head before admission: which route would run and how it behaves. Nothing is answered and no gate is called.
Trait Implementations§
Source§impl<G: Gate> HttpService for App<G>
impl<G: Gate> HttpService for App<G>
Source§fn call(
&self,
request: &HttpRequest<'_>,
env: &Envelope,
reply: &mut HttpReply,
) -> Outcome
fn call( &self, request: &HttpRequest<'_>, env: &Envelope, reply: &mut HttpReply, ) -> Outcome
Source§fn head(&self, head: &HttpHead<'_>, env: &mut Envelope) -> HeadAction
fn head(&self, head: &HttpHead<'_>, env: &mut Envelope) -> HeadAction
Source§fn upgraded(&self, env: &Envelope, conn: UpgradedConn)
fn upgraded(&self, env: &Envelope, conn: UpgradedConn)
head left,
and after done for that request. The conversation itself belongs to
the upgrade target (a WebSocketService, feature ws).