iota_json_rpc/
routing_layer.rs

1// Copyright (c) Mysten Labs, Inc.
2// Modifications Copyright (c) 2024 IOTA Stiftung
3// SPDX-License-Identifier: Apache-2.0
4
5use std::collections::HashMap;
6
7use iota_open_rpc::MethodRouting;
8
9#[derive(Debug, Clone)]
10pub struct RpcRouter {
11    routes: HashMap<String, MethodRouting>,
12    disable_routing: bool,
13}
14
15impl RpcRouter {
16    pub fn new(routes: HashMap<String, MethodRouting>, disable_routing: bool) -> Self {
17        Self {
18            routes,
19            disable_routing,
20        }
21    }
22
23    pub fn route<'c, 'a: 'c, 'b: 'c>(&'a self, method: &'b str, version: Option<&str>) -> &'c str {
24        if self.disable_routing {
25            method
26        } else {
27            // Modify the method name if routing is enabled
28            match (version, self.routes.get(method)) {
29                (Some(v), Some(route)) if route.matches(v) => route.route_to.as_str(),
30                _ => method,
31            }
32        }
33    }
34}