tina-core 0.0.2

Tina platform
Documentation
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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
//! tonic request

use std::{
    ops::{Deref, DerefMut},
    str::FromStr,
};

use std::fmt::Debug;

use bytes::Buf;
use http::request::Parts;
use http_body::Body;
use prost::Message;
use tonic::{
    metadata::{MetadataKey, MetadataMap, MetadataValue},
    Extensions, Status,
};

use crate::{
    app_error_from, app_system_error,
    tina::{
        data::{app_error::AppError, grpc::request_data::GrpcReqData, AppResult},
        grpc::{FromGrpcRequest, IntoGrpcRequest, IntoGrpcResponse},
        server::{application::Application, session::Session},
    },
};

/// Request Body
pub type BoxBody = http_body::combinators::UnsyncBoxBody<bytes::Bytes, tonic::Status>;
/// Request
pub struct Request {
    pub(crate) inner: http::Request<hyper::Body>,
}

impl Request {
    /// 构建
    pub fn new(inner: http::Request<hyper::Body>) -> Self {
        Self {
            inner,
        }
    }
    /// 提取
    pub fn into_inner(self) -> http::Request<hyper::Body> {
        self.inner
    }
    /// 转换成tonic request
    pub async fn into_tonic_request(self) -> AppResult<tonic::Request<bytes::Bytes>> {
        let (parts, mut body) = self.inner.into_parts();
        match body.data().await {
            Some(r) => {
                let data = r.map_err(app_error_from!())?;
                let req = http::Request::from_parts(parts, data);
                Ok(tonic::Request::from_http(req))
            }
            None => {
                let req = http::Request::from_parts(parts, bytes::Bytes::new());
                Ok(tonic::Request::from_http(req))
            }
        }
    }
    /// box unsync
    pub fn boxed_unsync(self) -> http::Request<BoxBody> {
        let (parts, body) = self.inner.into_parts();
        let body = body.map_err(|err| Status::from_error(Box::new(err))).boxed_unsync();
        http::Request::from_parts(parts, body)
    }
    /// box
    pub fn boxed(self) -> http::Request<http_body::combinators::BoxBody<bytes::Bytes, tonic::Status>> {
        let (parts, body) = self.inner.into_parts();
        let body = body.map_err(|err| Status::from_error(Box::new(err))).boxed();
        http::Request::from_parts(parts, body)
    }
}

impl From<http::Request<hyper::Body>> for Request {
    fn from(value: http::Request<hyper::Body>) -> Self {
        Self {
            inner: value,
        }
    }
}

impl Debug for Request {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("Request").field("inner", &self.inner).finish()
    }
}

impl Deref for Request {
    type Target = http::Request<hyper::Body>;

    fn deref(&self) -> &Self::Target {
        &self.inner
    }
}

impl DerefMut for Request {
    fn deref_mut(&mut self) -> &mut Self::Target {
        &mut self.inner
    }
}

/// 从Parts转换
#[async_trait]
pub trait FromGrpcRequestParts: Sized {
    /// If the extractor fails it'll use this "rejection" type. A rejection is
    /// a kind of error that can be converted into a response.
    type Rejection: IntoGrpcResponse;

    /// Perform the extraction.
    async fn from_request_parts(parts: &mut Parts) -> Result<Self, Self::Rejection>;
}

/// 转换Parts
#[async_trait]
pub trait ToGrpcRequestParts {
    /// 转换
    async fn to_request_parts(&self, metadata: &mut MetadataMap, extensions: &mut Extensions);
}

#[async_trait]
impl FromGrpcRequestParts for Application {
    type Rejection = AppError;

    async fn from_request_parts(parts: &mut Parts) -> Result<Self, Self::Rejection> {
        match parts.extensions.get::<Application>() {
            Some(v) => Ok(v.clone()),
            None => Err(app_system_error!("No Application found from request extensions")),
        }
    }
}

#[async_trait]
impl ToGrpcRequestParts for Application {
    async fn to_request_parts(&self, _metadata: &mut MetadataMap, _extensions: &mut Extensions) {}
}

#[async_trait]
impl FromGrpcRequestParts for Session {
    type Rejection = AppError;

    async fn from_request_parts(parts: &mut Parts) -> Result<Self, Self::Rejection> {
        match parts.extensions.get::<Session>() {
            Some(v) => Ok(v.clone()),
            None => Err(app_system_error!("No Session found from request extensions")),
        }
    }
}

#[async_trait]
impl ToGrpcRequestParts for Session {
    async fn to_request_parts(&self, metadata: &mut MetadataMap, _extensions: &mut Extensions) {
        if let Some(token_value) = self.get_token() {
            let application = self.get_application();
            let security_config = match application.get_security_config() {
                Ok(v) => v,
                Err(err) => {
                    tracing::error!("{err:?}");
                    return;
                }
            };
            let token_key = security_config.token_header_name.as_str();
            let token_header_name = match MetadataKey::from_str(token_key) {
                Ok(v) => v,
                Err(err) => {
                    tracing::error!("parse token to header name failed: reason: {err:?}, token_key: {token_key}");
                    return;
                }
            };
            let token_header_value = match MetadataValue::try_from(token_value.as_ref()) {
                Ok(v) => v,
                Err(err) => {
                    tracing::error!("parse token to header value failed: reason: {err:?}, token: {token_value}");
                    return;
                }
            };
            metadata.insert(token_header_name, token_header_value);
        }
    }
}

const HEADER_SIZE: usize = std::mem::size_of::<u8>() + std::mem::size_of::<u32>();

#[async_trait]
impl<D> FromGrpcRequest for GrpcReqData<D>
where
    D: Message + Default + Debug + Send + Sync + 'static,
{
    type Request = Request;

    type Rejection = AppError;

    async fn from_grpc_request(mut req: Self::Request) -> Result<Self, Self::Rejection>
    where
        Self: Sized,
    {
        let data = req.body_mut().data().await;
        match data {
            Some(r) => match r {
                Ok(mut v) => {
                    {
                        let bytes = v.as_ref();
                        tracing::trace!("GrpcReqData receive: {bytes:?}");
                    }
                    let (compress, _) = match v.len() >= HEADER_SIZE {
                        true => {
                            let n1 = v.get_u8();
                            let n2 = v.get_u32();
                            (n1, n2)
                        }
                        false => {
                            let v = v.as_ref();
                            tracing::error!("Invalid data format: {v:?}");
                            return Err(app_system_error!("Invalid data format: {v:?}"));
                        }
                    };
                    if compress > 0 {
                        let v = v.as_ref();
                        tracing::error!("Invalid data format, first bytes must be 0: {v:?}");
                        return Err(app_system_error!("Invalid data format, first bytes must be 0: {v:?}"));
                    }
                    let d = match <D as Message>::decode(&mut v) {
                        Ok(v1) => v1,
                        Err(err) => {
                            let v = v.as_ref();
                            tracing::error!("Decode prost data failed, reason: {err:?}, data: {v:?}");
                            return Err(app_system_error!("Decode prost data failed, reason: {err:?}, data: {v:?}"));
                        }
                    };
                    let mut data = GrpcReqData::new(d);
                    {
                        let headers = req.inner.headers_mut();
                        std::mem::swap(&mut data.metadata, headers);
                    }
                    {
                        let extensions = req.inner.extensions_mut();
                        std::mem::swap(&mut data.extensions, extensions);
                    }
                    Ok(data)
                }
                Err(err) => Err(app_system_error!("Take body data from request failed, reason: {err:?}")),
            },
            None => Err(app_system_error!("No body data found from request")),
        }
    }
}

#[async_trait]
impl<D> IntoGrpcRequest for GrpcReqData<D>
where
    D: Message + Debug + Send + Sync + 'static,
{
    type Request = tonic::Request<D>;

    async fn into_grpc_request(mut self) -> Self::Request {
        let headers = self.metadata;
        let metadata = MetadataMap::from_headers(headers);
        tonic::Request::from_parts(metadata, Extensions::default(), self.data)
    }
}

#[async_trait]
impl<D, T1, T2> IntoGrpcRequest for (T1, T2)
where
    D: Message + Debug + Send + Sync + 'static,
    T1: ToGrpcRequestParts + Debug + Send + Sync + 'static,
    T2: IntoGrpcRequest<Request = tonic::Request<D>> + Debug + Send + Sync + 'static,
{
    type Request = tonic::Request<D>;

    async fn into_grpc_request(self) -> Self::Request {
        let req = self.1.into_grpc_request().await;
        let (mut metadata, mut extensions, message) = req.into_parts();

        self.0.to_request_parts(&mut metadata, &mut extensions).await;
        tonic::Request::from_parts(metadata, extensions, message)
    }
}

#[async_trait]
impl<D, T1, T2, T3> IntoGrpcRequest for (T1, T2, T3)
where
    D: Message + Debug + Send + Sync + 'static,
    T1: ToGrpcRequestParts + Debug + Send + Sync + 'static,
    T2: ToGrpcRequestParts + Debug + Send + Sync + 'static,
    T3: IntoGrpcRequest<Request = tonic::Request<D>> + Debug + Send + Sync + 'static,
{
    type Request = tonic::Request<D>;

    async fn into_grpc_request(self) -> Self::Request {
        let req = self.2.into_grpc_request().await;
        let (mut metadata, mut extensions, message) = req.into_parts();

        self.0.to_request_parts(&mut metadata, &mut extensions).await;
        self.1.to_request_parts(&mut metadata, &mut extensions).await;
        tonic::Request::from_parts(metadata, extensions, message)
    }
}

#[async_trait]
impl<D, T1, T2, T3, T4> IntoGrpcRequest for (T1, T2, T3, T4)
where
    D: Message + Debug + Send + Sync + 'static,
    T1: ToGrpcRequestParts + Debug + Send + Sync + 'static,
    T2: ToGrpcRequestParts + Debug + Send + Sync + 'static,
    T3: ToGrpcRequestParts + Debug + Send + Sync + 'static,
    T4: IntoGrpcRequest<Request = tonic::Request<D>> + Debug + Send + Sync + 'static,
{
    type Request = tonic::Request<D>;

    async fn into_grpc_request(self) -> Self::Request {
        let req = self.3.into_grpc_request().await;
        let (mut metadata, mut extensions, message) = req.into_parts();

        self.0.to_request_parts(&mut metadata, &mut extensions).await;
        self.1.to_request_parts(&mut metadata, &mut extensions).await;
        self.2.to_request_parts(&mut metadata, &mut extensions).await;
        tonic::Request::from_parts(metadata, extensions, message)
    }
}

#[async_trait]
impl<D, T1, T2, T3, T4, T5> IntoGrpcRequest for (T1, T2, T3, T4, T5)
where
    D: Message + Debug + Send + Sync + 'static,
    T1: ToGrpcRequestParts + Debug + Send + Sync + 'static,
    T2: ToGrpcRequestParts + Debug + Send + Sync + 'static,
    T3: ToGrpcRequestParts + Debug + Send + Sync + 'static,
    T4: ToGrpcRequestParts + Debug + Send + Sync + 'static,
    T5: IntoGrpcRequest<Request = tonic::Request<D>> + Debug + Send + Sync + 'static,
{
    type Request = tonic::Request<D>;

    async fn into_grpc_request(self) -> Self::Request {
        let req = self.4.into_grpc_request().await;
        let (mut metadata, mut extensions, message) = req.into_parts();

        self.0.to_request_parts(&mut metadata, &mut extensions).await;
        self.1.to_request_parts(&mut metadata, &mut extensions).await;
        self.2.to_request_parts(&mut metadata, &mut extensions).await;
        self.3.to_request_parts(&mut metadata, &mut extensions).await;
        tonic::Request::from_parts(metadata, extensions, message)
    }
}

#[async_trait]
impl<T1, T2> FromGrpcRequest for (T1, T2)
where
    T1: FromGrpcRequestParts<Rejection = AppError> + Debug + Send + Sync + 'static,
    T2: FromGrpcRequest<Request = Request, Rejection = AppError> + Debug + Send + Sync + 'static,
{
    type Request = Request;

    type Rejection = AppError;

    async fn from_grpc_request(req: Self::Request) -> Result<Self, Self::Rejection>
    where
        Self: Sized,
    {
        let (mut parts, body) = req.inner.into_parts();
        let t1 = T1::from_request_parts(&mut parts).await?;
        let req = Request {
            inner: http::Request::from_parts(parts, body),
        };
        let t2 = T2::from_grpc_request(req).await?;
        Ok((t1, t2))
    }
}

#[async_trait]
impl<T1, T2, T3> FromGrpcRequest for (T1, T2, T3)
where
    T1: FromGrpcRequestParts<Rejection = AppError> + Debug + Send + Sync + 'static,
    T2: FromGrpcRequestParts<Rejection = AppError> + Debug + Send + Sync + 'static,
    T3: FromGrpcRequest<Request = Request, Rejection = AppError> + Debug + Send + Sync + 'static,
{
    type Request = Request;

    type Rejection = AppError;

    async fn from_grpc_request(req: Self::Request) -> Result<Self, Self::Rejection>
    where
        Self: Sized,
    {
        let (mut parts, body) = req.inner.into_parts();
        let t1 = T1::from_request_parts(&mut parts).await?;
        let t2 = T2::from_request_parts(&mut parts).await?;
        let req = Request {
            inner: http::Request::from_parts(parts, body),
        };
        let t3 = T3::from_grpc_request(req).await?;
        Ok((t1, t2, t3))
    }
}

#[async_trait]
impl<T1, T2, T3, T4> FromGrpcRequest for (T1, T2, T3, T4)
where
    T1: FromGrpcRequestParts<Rejection = AppError> + Debug + Send + Sync + 'static,
    T2: FromGrpcRequestParts<Rejection = AppError> + Debug + Send + Sync + 'static,
    T3: FromGrpcRequestParts<Rejection = AppError> + Debug + Send + Sync + 'static,
    T4: FromGrpcRequest<Request = Request, Rejection = AppError> + Debug + Send + Sync + 'static,
{
    type Request = Request;

    type Rejection = AppError;

    async fn from_grpc_request(req: Self::Request) -> Result<Self, Self::Rejection>
    where
        Self: Sized,
    {
        let (mut parts, body) = req.inner.into_parts();
        let t1 = T1::from_request_parts(&mut parts).await?;
        let t2 = T2::from_request_parts(&mut parts).await?;
        let t3 = T3::from_request_parts(&mut parts).await?;
        let req = Request {
            inner: http::Request::from_parts(parts, body),
        };
        let t4 = T4::from_grpc_request(req).await?;
        Ok((t1, t2, t3, t4))
    }
}

#[async_trait]
impl<T1, T2, T3, T4, T5> FromGrpcRequest for (T1, T2, T3, T4, T5)
where
    T1: FromGrpcRequestParts<Rejection = AppError> + Debug + Send + Sync + 'static,
    T2: FromGrpcRequestParts<Rejection = AppError> + Debug + Send + Sync + 'static,
    T3: FromGrpcRequestParts<Rejection = AppError> + Debug + Send + Sync + 'static,
    T4: FromGrpcRequestParts<Rejection = AppError> + Debug + Send + Sync + 'static,
    T5: FromGrpcRequest<Request = Request, Rejection = AppError> + Debug + Send + Sync + 'static,
{
    type Request = Request;

    type Rejection = AppError;

    async fn from_grpc_request(req: Self::Request) -> Result<Self, Self::Rejection>
    where
        Self: Sized,
    {
        let (mut parts, body) = req.inner.into_parts();
        let t1 = T1::from_request_parts(&mut parts).await?;
        let t2 = T2::from_request_parts(&mut parts).await?;
        let t3 = T3::from_request_parts(&mut parts).await?;
        let t4 = T4::from_request_parts(&mut parts).await?;
        let req = Request {
            inner: http::Request::from_parts(parts, body),
        };
        let t5 = T5::from_grpc_request(req).await?;
        Ok((t1, t2, t3, t4, t5))
    }
}