summaryrefslogtreecommitdiff
path: root/src/authorization/server.rs
blob: 2605bd54854bece9832163b2065fd440f9a99158 (plain)
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
use super::cedar_authorizer::CedarAuthorizer;
use super::check_service::CheckService;
use envoy_types::ext_authz::v3::pb::AuthorizationServer;
use std::sync::Arc;

pub struct Server {
    router: tonic::transport::server::Router,
}

impl Server {
    pub fn new() -> Result<Server, Box<dyn std::error::Error>> {
        Ok(Self::new_with(|mut builder| {
            let (_health_reporter, health_service) = tonic_health::server::health_reporter();
            let authorizer = Arc::new(CedarAuthorizer::default());
            let check_service = CheckService::new(authorizer);
            builder
                .add_service(AuthorizationServer::new(check_service))
                .add_service(health_service)
        }))
    }

    pub fn new_with<F>(f: F) -> Server
    where
        F: FnOnce(tonic::transport::Server) -> tonic::transport::server::Router,
    {
        let router = f(tonic::transport::Server::builder());
        Server { router }
    }

    pub async fn serve(self, addr: std::net::SocketAddr) -> Result<(), tonic::transport::Error> {
        self.router.serve(addr).await
    }
}

impl Default for Server {
    fn default() -> Self {
        Self::new().unwrap()
    }
}