apigate_core/routing/
mod.rs1mod header_sticky;
2mod no_route_key;
3mod path_sticky;
4
5use std::borrow::Cow;
6
7use crate::backend::BackendPool;
8
9pub use header_sticky::HeaderSticky;
10pub use no_route_key::NoRouteKey;
11pub use path_sticky::PathSticky;
12
13pub struct RouteCtx<'a> {
14 pub service: &'a str,
15 pub prefix: &'a str,
16 pub route_path: &'a str,
17 pub method: &'a http::Method,
18 pub uri: &'a http::Uri,
19 pub headers: &'a http::HeaderMap,
20}
21
22#[derive(Clone, Debug, Eq, PartialEq, Hash)]
23pub struct AffinityKey<'a>(Cow<'a, str>);
24
25impl<'a> AffinityKey<'a> {
26 pub fn borrowed(value: &'a str) -> Self {
27 Self(Cow::Borrowed(value))
28 }
29
30 pub fn owned(value: impl Into<String>) -> Self {
31 Self(Cow::Owned(value.into()))
32 }
33
34 pub fn as_str(&self) -> &str {
35 self.0.as_ref()
36 }
37
38 pub fn into_owned(self) -> String {
39 self.0.into_owned()
40 }
41}
42
43#[derive(Clone, Debug)]
44pub enum CandidateSet<'a> {
45 All,
46 Indices(&'a [usize]),
47}
48
49#[derive(Clone, Debug)]
50pub struct RoutingDecision<'a> {
51 pub affinity: Option<AffinityKey<'a>>,
52 pub candidates: CandidateSet<'a>,
53}
54
55pub trait RouteStrategy: Send + Sync + 'static {
56 fn route<'a>(&self, ctx: &RouteCtx<'a>, pool: &'a BackendPool) -> RoutingDecision<'a>;
57}