sui_rpc_api/grpc/
mod.rs

1// Copyright (c) Mysten Labs, Inc.
2// SPDX-License-Identifier: Apache-2.0
3
4use std::convert::Infallible;
5use tonic::server::NamedService;
6use tower::Service;
7
8pub mod deadline;
9pub mod v2;
10pub mod v2alpha;
11
12pub type BoxError = Box<dyn std::error::Error + Send + Sync + 'static>;
13
14#[derive(Default)]
15pub struct Services {
16    router: axum::Router,
17}
18
19impl Services {
20    pub fn new() -> Self {
21        Self::default()
22    }
23
24    /// Add a new service.
25    pub fn add_service<S>(mut self, svc: S) -> Self
26    where
27        S: Service<
28                axum::extract::Request,
29                Response: axum::response::IntoResponse,
30                Error = Infallible,
31            > + NamedService
32            + Clone
33            + Send
34            + Sync
35            + 'static,
36        S::Future: Send + 'static,
37        S::Error: Into<BoxError> + Send,
38    {
39        self.router = self
40            .router
41            .route_service(&format!("/{}/{{*rest}}", S::NAME), svc);
42        self
43    }
44
45    pub fn merge_router(mut self, router: axum::Router) -> Self {
46        self.router = self.router.merge(router);
47        self
48    }
49
50    pub fn into_router(self) -> axum::Router {
51        self.router.layer(tonic_web::GrpcWebLayer::new())
52    }
53}