intrepid_core/
axum.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
use std::marker::PhantomData;

use axum::{
    async_trait,
    body::{Body, Bytes},
    extract::FromRequest,
    response::IntoResponse,
};
use futures::future::BoxFuture;
use http::{Request, Response, StatusCode};
use tower::Service;

use crate::{Error, Frame, FrameFuture, Handler, StatefulSystem};

/// An axum request being turned into an intrepid frame.
pub struct FrameRequest(Bytes);

impl From<FrameRequest> for Frame {
    fn from(frame: FrameRequest) -> Self {
        Frame::new(frame.0)
    }
}

#[async_trait]
impl<State> FromRequest<State> for FrameRequest
where
    Bytes: FromRequest<State>,
    State: Send + Sync,
{
    type Rejection = Response<Body>;

    async fn from_request(req: Request<Body>, state: &State) -> Result<Self, Self::Rejection> {
        let body = Bytes::from_request(req, state)
            .await
            .map_err(IntoResponse::into_response)?;

        Ok(Self(body))
    }
}

/// An intrepid frame being turned into an axum response.
pub struct FrameResponse(Frame);

impl From<Frame> for FrameResponse {
    fn from(frame: Frame) -> Self {
        Self(frame)
    }
}

impl IntoResponse for FrameResponse {
    fn into_response(self) -> Response<Body> {
        let body = self.0.into_bytes();
        (StatusCode::OK, body).into_response()
    }
}

/// An intrepid error being turned into an axum response.
#[derive(Debug)]
pub struct FrameResponseError(Error);

impl From<Error> for FrameResponseError {
    fn from(error: Error) -> Self {
        Self(error)
    }
}

impl IntoResponse for FrameResponseError {
    fn into_response(self) -> Response<Body> {
        (StatusCode::INTERNAL_SERVER_ERROR, self.0.to_string()).into_response()
    }
}

/// A struct that lets us bridge intrepid handlers to axum handlers.
#[derive(Clone)]
pub struct RequestHandler<GivenHandler, Args, State>
where
    GivenHandler: crate::Handler<Args, State> + Clone + Send + Sync + 'static,
    State: Clone + Send + Sync + 'static,
    Args: Clone + Send + Sync + 'static,
{
    handler: GivenHandler,
    _state: PhantomData<State>,
    _args: PhantomData<Args>,
}

impl<GivenHandler, Args, State> RequestHandler<GivenHandler, Args, State>
where
    GivenHandler: crate::Handler<Args, State> + Clone + Send + Sync + 'static,
    State: Clone + Send + Sync + 'static,
    Args: Clone + Send + Sync + 'static,
{
    /// Create a new request handler.
    pub fn new(handler: GivenHandler) -> Self {
        Self {
            handler,
            _state: PhantomData,
            _args: PhantomData,
        }
    }
}

impl<GivenHandler, Args, State> Handler<Args, State> for RequestHandler<GivenHandler, Args, State>
where
    GivenHandler: Handler<Args, State> + Clone + Send + Sync + 'static,
    State: Clone + Send + Sync + 'static,
    Args: Clone + Send + Sync + 'static,
{
    type Future = FrameFuture;

    fn invoke(&self, frame: impl Into<Frame>, state: State) -> Self::Future {
        let handler = self.handler.clone();
        let frame = frame.into();

        FrameFuture::from_async_block(async move { handler.invoke(frame, state).await })
    }
}

impl<GivenHandler, Args, State> axum::handler::Handler<Args, State>
    for RequestHandler<GivenHandler, Args, State>
where
    GivenHandler: Handler<Args, State> + Clone + Send + Sync + 'static,
    State: Clone + Send + Sync + 'static,
    Args: Clone + Send + Sync + 'static,
{
    type Future = std::pin::Pin<
        Box<dyn std::future::Future<Output = http::Response<axum::body::Body>> + Send>,
    >;

    fn call(self, request: http::Request<axum::body::Body>, state: State) -> Self::Future {
        use axum::extract::FromRequest;
        use axum::response::IntoResponse;

        let handler = self.handler.clone();

        Box::pin(async move {
            match crate::FrameRequest::from_request(request, &state).await {
                Ok(frame_request) => handler
                    .invoke(frame_request, state)
                    .await
                    .map(crate::FrameResponse::from)
                    .map_err(crate::FrameResponseError::from)
                    .into_response(),
                Err(rejection) => rejection.into_response(),
            }
        })
    }
}

// impl<GivenBody> Service<Request<GivenBody>> for StatelessSystem
// where
//     GivenBody: HttpBody<Data = Bytes> + Send + 'static,
//     GivenBody::Error: Into<BoxError>,
// {
//     type Response = Response<Body>;
//     type Error = Infallible;
//     type Future = BoxFuture<'static, Result<Self::Response, Self::Error>>

//     fn poll_ready(
//         &mut self,
//         _: &mut std::task::Context<'_>,
//     ) -> std::task::Poll<Result<(), Self::Error>> {
//         std::task::Poll::Ready(Ok(()))
//     }

//     fn call(&mut self, request: Request<GivenBody>) -> Self::Future {
//         let req = request.map(Body::new);
//         Box::pin(async { self.handle_http(req, ()) })
//     }
// }

impl<State> Service<FrameRequest> for StatefulSystem<State>
where
    State: Clone + Send + Sync + 'static,
{
    type Response = FrameResponse;
    type Error = FrameResponseError;
    type Future = BoxFuture<'static, Result<Self::Response, Self::Error>>;

    fn poll_ready(
        &mut self,
        _: &mut std::task::Context<'_>,
    ) -> std::task::Poll<Result<(), Self::Error>> {
        std::task::Poll::Ready(Ok(()))
    }

    fn call(&mut self, request: FrameRequest) -> Self::Future {
        let instance = self.clone();

        Box::pin(async move {
            instance
                .handle_frame(request.into())
                .await
                .map(Into::into)
                .map_err(Into::into)
        })
    }
}

// fn wat() -> BoxCloneService<Request<Body>, Response<Body>, Infallible> {
//     use std::{iter::once, sync::Arc};
//     use tower::ServiceBuilder;
//     use tower_http::{
//         add_extension::AddExtensionLayer, compression::CompressionLayer,
//         propagate_header::PropagateHeaderLayer, sensitive_headers::SetSensitiveRequestHeadersLayer,
//         trace::TraceLayer, validate_request::ValidateRequestHeaderLayer,
//     };
//     let service = ServiceBuilder::new()
//         .boxed_clone()
//         .layer(SetSensitiveRequestHeadersLayer::new(once(AUTHORIZATION)))
//         .layer(TraceLayer::new_for_http())
//         .layer(AddExtensionLayer::new(Arc::new(())))
//         .layer(CompressionLayer::new())
//         .layer(PropagateHeaderLayer::new(HeaderName::from_static(
//             "x-request-id",
//         )))
//         .layer(ValidateRequestHeaderLayer::bearer("passwordlol"))
//         .layer(ValidateRequestHeaderLayer::accept("application/json"))
//         .service_fn(|_| async { Ok("hay gusy  lol".to_string().into_response()) });

//     service
// }

// mod wut {
//     use std::{convert::Infallible, iter::once, sync::Arc};

//     use axum::{
//         body::Body,
//         extract::Request,
//         response::{IntoResponse, Response},
//     };
//     use http::{
//         header::{AUTHORIZATION, CONTENT_TYPE},
//         HeaderName,
//     };
//     use tower::{util::BoxService, ServiceBuilder};
//     use tower_http::{
//         add_extension::AddExtensionLayer, compression::CompressionLayer,
//         propagate_header::PropagateHeaderLayer, sensitive_headers::SetSensitiveRequestHeadersLayer,
//         set_header::SetResponseHeaderLayer, trace::TraceLayer,
//         validate_request::ValidateRequestHeaderLayer,
//     };

//     fn wat() -> BoxService<Request, Response, Infallible> {
//         let service = ServiceBuilder::new()
//             .boxed()
//             // Mark the `Authorization` request header as sensitive so it doesn't show in logs
//             .layer(SetSensitiveRequestHeadersLayer::new(once(AUTHORIZATION)))
//             // High level logging of requests and responses
//             .layer(TraceLayer::new_for_http())
//             // Share an `Arc<State>` with all requests
//             .layer(AddExtensionLayer::new(Arc::new(())))
//             // Compress responses
//             .layer(CompressionLayer::new())
//             // Propagate `X-Request-Id`s from requests to responses
//             .layer(PropagateHeaderLayer::new(HeaderName::from_static(
//                 "x-request-id",
//             )))
//             // If the response has a known size set the `Content-Length` header
//             // Authorize requests using a token
//             .layer(ValidateRequestHeaderLayer::bearer("passwordlol"))
//             // Accept only application/json, application/* and */* in a request's ACCEPT header
//             .layer(ValidateRequestHeaderLayer::accept("application/json"))
//             // Wrap a `Service` in our middleware stack
//             .service_fn(|_| async { Ok("hay gusy  lol".to_string().into_response()) });

//         service
//     }
// }