forked from LukeMathWalker/pavex
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lib.rs
103 lines (103 loc) · 3.6 KB
/
lib.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
//! Do NOT edit this code.
//! It was automatically generated by Pavex.
//! All manual edits will be lost next time the code is generated.
#[allow(unused_imports)]
use std as alloc;
struct ServerState {
router: pavex::routing::Router<u32>,
application_state: ApplicationState,
}
pub struct ApplicationState {
s0: app_blueprint::HttpClient,
}
pub async fn build_application_state(
v0: app_blueprint::Config,
) -> crate::ApplicationState {
let v1 = app_blueprint::http_client(v0);
crate::ApplicationState { s0: v1 }
}
pub async fn run(
server_builder: pavex::hyper::server::Builder<
pavex::hyper::server::conn::AddrIncoming,
>,
application_state: ApplicationState,
) -> Result<(), pavex::Error> {
let server_state = std::sync::Arc::new(ServerState {
router: build_router().map_err(pavex::Error::new)?,
application_state,
});
let make_service = pavex::hyper::service::make_service_fn(move |_| {
let server_state = server_state.clone();
async move {
Ok::<
_,
pavex::hyper::Error,
>(
pavex::hyper::service::service_fn(move |request| {
let server_state = server_state.clone();
async move {
let response = route_request(request, server_state).await;
let response = pavex::hyper::Response::from(response);
Ok::<_, pavex::hyper::Error>(response)
}
}),
)
}
});
server_builder.serve(make_service).await.map_err(pavex::Error::new)
}
fn build_router() -> Result<pavex::routing::Router<u32>, pavex::routing::InsertError> {
let mut router = pavex::routing::Router::new();
router.insert("/home", 0u32)?;
Ok(router)
}
async fn route_request(
request: http::Request<pavex::hyper::body::Body>,
server_state: std::sync::Arc<ServerState>,
) -> pavex::response::Response {
#[allow(unused)]
let (request_head, request_body) = request.into_parts();
let request_head: pavex::request::RequestHead = request_head.into();
let matched_route = match server_state.router.at(&request_head.uri.path()) {
Ok(m) => m,
Err(_) => {
return pavex::response::Response::not_found().box_body();
}
};
let route_id = matched_route.value;
#[allow(unused)]
let url_params: pavex::extract::route::RawRouteParams<'_, '_> = matched_route
.params
.into();
match route_id {
0u32 => {
match &request_head.method {
&pavex::http::Method::GET => {
route_handler_0(
server_state.application_state.s0.clone(),
&request_head,
)
.await
}
_ => {
let header_value = pavex::http::HeaderValue::from_static("GET");
pavex::response::Response::method_not_allowed()
.insert_header(pavex::http::header::ALLOW, header_value)
.box_body()
}
}
}
_ => pavex::response::Response::not_found().box_body(),
}
}
pub async fn route_handler_0(
v0: app_blueprint::HttpClient,
v1: &pavex::request::RequestHead,
) -> pavex::response::Response {
let v2 = app_blueprint::extract_path(v1);
let v4 = {
let v3 = app_blueprint::logger();
app_blueprint::stream_file(v2, v3, v0)
};
<pavex::response::Response as pavex::response::IntoResponse>::into_response(v4)
}