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> { 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) -> 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() } }