Skip to content

Commit

Permalink
feat(config): Add external config
Browse files Browse the repository at this point in the history
  • Loading branch information
AndreyErmilov committed Oct 1, 2023
1 parent 4e6fedb commit 7312d27
Show file tree
Hide file tree
Showing 15 changed files with 389 additions and 127 deletions.
49 changes: 46 additions & 3 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 24 additions & 0 deletions config_hitbox.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
upstreams:
- name: default-upstream
addresses:
- host: 127.0.0.1
port: 8080
scheme: http

backends:
- type: inmemory
name: StrettoBackend
capacity: 10000000

endpoints:
- name: all
path: /{path}*
method: GET
key:
- Method
- !Path
path: /{path}*
predicates:
request: []
response:
- !StatusCode 200
3 changes: 2 additions & 1 deletion hitboxd/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ edition = "2021"

[dependencies]
hitbox = { path = "../../hitbox/hitbox", features = ["metrics"] }
# hitbox-stretto = { path = "../../hitbox/hitbox-stretto" }
hitbox-tower = { path = "../../hitbox/hitbox-tower" }
hitbox-http = { path = "../../hitbox/hitbox-http", version = "0.1" }
hitbox-redis = { path = "../../hitbox/hitbox-redis", version = "0.1" }
hitbox-stretto = { path = "../../hitbox/hitbox-stretto", version = "0.1" }
actix-router = "0.5"
hyper = { version = "0.14", features = ["full"] }
tokio = { version = "1", features = ["full"] }
Expand All @@ -24,3 +24,4 @@ futures = "0.3"
pin-project = "1"
chrono = "0.4"
bytes = "1"
serde_yaml = "0.9.25"
49 changes: 28 additions & 21 deletions hitboxd/src/config.rs
Original file line number Diff line number Diff line change
@@ -1,33 +1,40 @@
use std::collections::HashMap;
use std::fmt::Debug;
use crate::external_configuration;
use hitbox::policy::PolicyConfig;
use hitbox_stretto::StrettoBackend;
use std::sync::Arc;

use hitbox::predicate::Predicate;
use hitbox::Extractor;
use hitbox_http::CacheableHttpRequest;
use http::method::Method;
use hyper::Body;

pub type BoxPredicate = Box<dyn Predicate<Subject = CacheableHttpRequest<Body>> + Send + Sync>;
pub type BoxExtractor = Box<dyn Extractor<Subject = CacheableHttpRequest<Body>> + Send + Sync>;

pub struct Config {
pub endpoints: HashMap<String, Endpoint<BoxPredicate, BoxExtractor>>,
pub endpoints: Vec<crate::Endpoint>,
}

impl Config {
pub fn new() -> Self {
Config {
endpoints: HashMap::new(),
endpoints: Vec::new(),
}
}
}

#[derive(Debug)]
pub struct Endpoint<P, E> {
pub name: String,
pub path: String,
pub methods: Vec<Method>,
pub request_predicate: Arc<P>,
pub extractors: Arc<E>,
pub fn from_external(
config: external_configuration::Config,
backend: Arc<StrettoBackend>,
) -> Self {
let endpoints = config
.endpoints
.into_iter()
.map(|source_endpoint| crate::Endpoint {
name: source_endpoint.name,
routing: crate::endpoint::Routing {
path_pattern: source_endpoint.path,
methods: vec![source_endpoint.method],
},
backend: backend.clone(),
upstreams: Vec::new(),
request_predicates: source_endpoint.predicates.request,
response_predicates: source_endpoint.predicates.response,
extractors: source_endpoint.key,
policy: PolicyConfig::default(),
})
.collect();
Config { endpoints }
}
}
57 changes: 57 additions & 0 deletions hitboxd/src/endpoint.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
use hitbox::policy::PolicyConfig;
use hitbox_stretto::StrettoBackend;
use hitbox_tower::configuration::{RequestExtractor, RequestPredicate, ResponsePredicate};
use hitbox_tower::EndpointConfig;
use hitbox_tower::Method;
use std::sync::Arc;

#[derive(Clone)]
pub struct Routing {
pub path_pattern: String,
pub methods: Vec<Method>,
}

#[derive(Clone)]
pub struct Upstream {
pub address: String,
pub port: u16,
}

#[derive(Clone)]
pub struct Endpoint {
pub name: String,
pub routing: Routing,
pub backend: Arc<StrettoBackend>,
pub upstreams: Vec<Upstream>,
pub request_predicates: Vec<RequestPredicate>,
pub response_predicates: Vec<ResponsePredicate>,
pub extractors: Vec<RequestExtractor>,
pub policy: PolicyConfig,
}

impl Endpoint {
pub fn new(backend: Arc<StrettoBackend>) -> Self {
Self {
name: String::new(),
routing: Routing {
path_pattern: String::new(),
methods: Vec::new(),
},
backend,
upstreams: Vec::new(),
request_predicates: Vec::new(),
response_predicates: Vec::new(),
extractors: Vec::new(),
policy: Default::default(),
}
}

pub fn to_endpoint_config(self) -> EndpointConfig {
EndpointConfig {
request_predicates: self.request_predicates,
response_predicates: self.response_predicates,
extractors: self.extractors,
policy: self.policy,
}
}
}
29 changes: 29 additions & 0 deletions hitboxd/src/external_configuration/backend.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
use serde::{Deserialize, Serialize};

#[derive(Debug, Serialize, Deserialize)]
pub struct InMemory {
pub name: String,
pub capacity: u32,
}

#[derive(Debug, Serialize, Deserialize)]
#[serde(tag = "type")]
#[serde(rename_all = "lowercase")]
pub enum Backend {
InMemory(InMemory),
}

impl Default for InMemory {
fn default() -> Self {
Self {
name: String::from("StrettoBackend"),
capacity: 10_000_000,
}
}
}

impl Default for Backend {
fn default() -> Self {
Self::InMemory(InMemory::default())
}
}
51 changes: 51 additions & 0 deletions hitboxd/src/external_configuration/endpoint.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
use hitbox_tower::{
configuration::serializers::method,
configuration::{RequestExtractor, RequestPredicate, ResponsePredicate},
Method, StatusCode,
};
use serde::{Deserialize, Serialize};

#[derive(Debug, Serialize, Deserialize)]
pub struct Predicates {
pub request: Vec<RequestPredicate>,
pub response: Vec<ResponsePredicate>,
}

#[derive(Debug, Serialize, Deserialize)]
pub struct Endpoint {
pub name: String,
pub path: String,
#[serde(with = "method")]
pub method: Method,
pub key: Vec<RequestExtractor>,
pub predicates: Predicates,
}

impl Default for Predicates {
fn default() -> Self {
Self {
request: Vec::new(),
response: vec![ResponsePredicate::StatusCode {
code: StatusCode::OK,
}],
}
}
}

impl Default for Endpoint {
fn default() -> Self {
let default_extractors = vec![
RequestExtractor::Method,
RequestExtractor::Path {
path: String::from("/{path}*"),
},
];
Self {
name: String::from("all"),
path: String::from("/{path}*"),
method: Method::GET,
key: default_extractors,
predicates: Predicates::default(),
}
}
}
1 change: 1 addition & 0 deletions hitboxd/src/external_configuration/group.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

27 changes: 27 additions & 0 deletions hitboxd/src/external_configuration/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
mod backend;
mod endpoint;
mod group;
mod policy;
mod upstream;

pub use backend::Backend;
pub use endpoint::Endpoint;
pub use policy::Policy;
pub use upstream::Upstream;

#[derive(Debug, serde::Serialize, serde::Deserialize)]
pub struct Config {
pub upstreams: Vec<Upstream>,
pub backends: Vec<Backend>,
pub endpoints: Vec<Endpoint>,
}

impl Default for Config {
fn default() -> Self {
Self {
upstreams: vec![Upstream::default()],
backends: vec![Backend::default()],
endpoints: vec![Endpoint::default()],
}
}
}
13 changes: 13 additions & 0 deletions hitboxd/src/external_configuration/policy.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
use serde::{Deserialize, Serialize};

#[derive(Debug, Serialize, Deserialize)]
pub struct Enabled {
ttl: u16,
}

#[derive(Debug, Serialize, Deserialize)]
#[serde(untagged)]
pub enum Policy {
Enabled(Enabled),
Disabled,
}
Loading

0 comments on commit 7312d27

Please sign in to comment.