servers/
configurator.rs

1// Copyright 2023 Greptime Team
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15use std::sync::Arc;
16
17use axum::Router as HttpRouter;
18use common_error::ext::BoxedError;
19use tonic::transport::server::Router as GrpcRouter;
20
21use crate::grpc::builder::GrpcServerBuilder;
22
23/// A configurator that customizes or enhances an HTTP router.
24#[async_trait::async_trait]
25pub trait HttpConfigurator<C>: Send + Sync {
26    /// Configures the given HTTP router using the provided context.
27    async fn configure_http(
28        &self,
29        route: HttpRouter,
30        ctx: C,
31    ) -> std::result::Result<HttpRouter, BoxedError>;
32}
33
34pub type HttpConfiguratorRef<C> = Arc<dyn HttpConfigurator<C>>;
35
36/// A configurator that customizes or enhances a gRPC router.
37#[async_trait::async_trait]
38pub trait GrpcRouterConfigurator<C>: Send + Sync {
39    /// Configures the given gRPC router using the provided context.
40    async fn configure_grpc_router(
41        &self,
42        route: GrpcRouter,
43        ctx: C,
44    ) -> std::result::Result<GrpcRouter, BoxedError>;
45}
46
47pub type GrpcRouterConfiguratorRef<C> = Arc<dyn GrpcRouterConfigurator<C>>;
48
49/// A configurator that customizes or enhances a [`GrpcServerBuilder`].
50#[async_trait::async_trait]
51pub trait GrpcBuilderConfigurator<C>: Send + Sync {
52    async fn configure(
53        &self,
54        builder: GrpcServerBuilder,
55        ctx: C,
56    ) -> std::result::Result<GrpcServerBuilder, BoxedError>;
57}
58
59pub type GrpcBuilderConfiguratorRef<C> = Arc<dyn GrpcBuilderConfigurator<C>>;