-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #92 from hit-box/tower-trait-configurable
Tower trait configurable
- Loading branch information
Showing
12 changed files
with
253 additions
and
115 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
use hitbox::policy::PolicyConfig; | ||
use hitbox::predicate::Predicate; | ||
use hitbox::Extractor; | ||
use hitbox_http::{CacheableHttpRequest, CacheableHttpResponse}; | ||
|
||
type RequestPredicate<ReqBody> = | ||
Box<dyn Predicate<Subject = CacheableHttpRequest<ReqBody>> + Send + Sync>; | ||
|
||
type ResponsePredicate<ResBody> = | ||
Box<dyn Predicate<Subject = CacheableHttpResponse<ResBody>> + Send + Sync>; | ||
|
||
type RequestExtractor<ResBody> = | ||
Box<dyn Extractor<Subject = CacheableHttpRequest<ResBody>> + Send + Sync>; | ||
|
||
pub trait Configurable { | ||
fn request_predicates<ReqBody>(&self) -> RequestPredicate<ReqBody> | ||
where | ||
ReqBody: Send + 'static; | ||
|
||
fn response_predicates<ResBody>(&self) -> ResponsePredicate<ResBody> | ||
where | ||
ResBody: Send + 'static; | ||
|
||
fn extractors<ReqBody>(&self) -> RequestExtractor<ReqBody> | ||
where | ||
ReqBody: Send + 'static; | ||
|
||
fn policy(&self) -> PolicyConfig; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
use crate::configuration::{ | ||
ExtractorBuilder, RequestExtractor, RequestPredicate, RequestPredicateBuilder, | ||
ResponsePredicate, ResponsePredicateBuilder, | ||
}; | ||
use crate::EndpointConfig; | ||
use hitbox::policy::PolicyConfig; | ||
|
||
#[derive(Debug)] | ||
pub struct EndpointConfigBuilder { | ||
pub request_predicates: Vec<RequestPredicate>, | ||
pub response_predicates: Vec<ResponsePredicate>, | ||
pub extractors: Vec<RequestExtractor>, | ||
pub policy: PolicyConfig, | ||
} | ||
|
||
impl EndpointConfigBuilder { | ||
pub fn new() -> Self { | ||
Self { | ||
request_predicates: Vec::new(), | ||
response_predicates: Vec::new(), | ||
extractors: Vec::new(), | ||
policy: Default::default(), | ||
} | ||
} | ||
|
||
pub fn disable(self) -> Self { | ||
Self { | ||
request_predicates: self.request_predicates, | ||
response_predicates: self.response_predicates, | ||
extractors: self.extractors, | ||
policy: PolicyConfig::Disabled, | ||
} | ||
} | ||
|
||
pub fn request(self, predicates: RequestPredicateBuilder) -> Self { | ||
Self { | ||
request_predicates: predicates.build(), | ||
response_predicates: self.response_predicates, | ||
extractors: self.extractors, | ||
policy: self.policy, | ||
} | ||
} | ||
|
||
pub fn response(self, predicates: ResponsePredicateBuilder) -> Self { | ||
Self { | ||
request_predicates: self.request_predicates, | ||
response_predicates: predicates.build(), | ||
extractors: self.extractors, | ||
policy: self.policy, | ||
} | ||
} | ||
|
||
pub fn cache_key(self, extractors: ExtractorBuilder) -> Self { | ||
Self { | ||
request_predicates: self.request_predicates, | ||
response_predicates: self.response_predicates, | ||
extractors: extractors.build(), | ||
policy: self.policy, | ||
} | ||
} | ||
|
||
pub fn build(self) -> EndpointConfig { | ||
EndpointConfig { | ||
request_predicates: self.request_predicates, | ||
response_predicates: self.response_predicates, | ||
extractors: self.extractors, | ||
policy: self.policy, | ||
} | ||
} | ||
} | ||
|
||
impl Default for EndpointConfigBuilder { | ||
fn default() -> Self { | ||
Self { | ||
request_predicates: Vec::new(), | ||
response_predicates: vec![ResponsePredicate::StatusCode { | ||
code: http::StatusCode::OK, | ||
}], | ||
extractors: vec![ | ||
RequestExtractor::Path { | ||
path: String::from("{path}*"), | ||
}, | ||
RequestExtractor::Method, | ||
], | ||
policy: Default::default(), | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
pub mod builder; | ||
mod endpoint; | ||
mod extractors; | ||
mod predicates; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.