Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tower config (builder) #86

Merged
merged 6 commits into from
Aug 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 37 additions & 6 deletions examples/examples/axum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,18 +39,49 @@ async fn main() {
tracing::subscriber::set_global_default(subscriber).unwrap();

let backend = RedisBackend::new().unwrap();
let inmemory = StrettoBackend::builder(2 ^ 16)
.finalize()
.unwrap();
// build our application with a single route
let inmemory = StrettoBackend::builder(2 ^ 16).finalize().unwrap();
//let request_predicate = predicate::RequestBuilder::new()
//.query("cache", "true")
//.build();
//let response_predicate = predicate::ResponseBuilder::new()
//.status_code(200)
//.body(operations::NE(operations::EmptyVec))
//.build();
//let cache_key = CacheKeyBuilder::new()
//.path(Full)
//.method()
//.build();
//let endpoint_config = Config::builder()
//.request_predicate(response_predicate)
//.response_predicate(response_predicate)
//.cache_key(cache_key)
//.build();
use hitbox_tower::config::request;
let app = Router::new()
.route("/greet/:name/", get(handler_result))
.route("/", get(handler))
.route("/json/", get(handler_json))
.layer(
ServiceBuilder::new()
.layer(Cache::builder().backend(inmemory).build())
.layer(Cache::builder().backend(backend).build()),
//.layer(
//Cache::builder()
////.config(config)
//.backend(inmemory)
//.build(),
//)
.layer(
Cache::builder()
//.config(config)
.backend(backend)
.request(
request::query("cache", "true")
.query("x-cache", "true")
.path("/{path}*"),
)
// .response(response::header())
// .key(path("/{}*"))
.build(),
),
);

// run it with hyper on localhost:3000
Expand Down
2 changes: 1 addition & 1 deletion examples/examples/tower.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use hitbox_stretto::StrettoBackend;
use hitbox_redis::RedisBackend;
use hitbox_stretto::builder::StrettoBackendBuilder;
use hitbox_stretto::StrettoBackend;
use hitbox_tower::Cache;
use hyper::{Body, Server};
use std::{convert::Infallible, net::SocketAddr};
Expand Down
2 changes: 1 addition & 1 deletion hitbox-http/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
mod body;
pub mod extractors;
pub mod predicates;
mod query;
pub mod query;
mod request;
mod response;

Expand Down
46 changes: 46 additions & 0 deletions hitbox-http/src/predicates/method.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
use crate::CacheableHttpRequest;
use async_trait::async_trait;
use hitbox::predicates::{Predicate, PredicateResult};

pub struct Method<P> {
method: http::Method,
inner: P,
}

pub trait MethodPredicate: Sized {
fn method(self, method: http::Method) -> Method<Self>;
}

impl<P> MethodPredicate for P
where
P: Predicate,
{
fn method(self, method: http::Method) -> Method<Self> {
Method {
method,
inner: self,
}
}
}

#[async_trait]
impl<P, ReqBody> Predicate for Method<P>
where
P: Predicate<Subject = CacheableHttpRequest<ReqBody>> + Send + Sync,
ReqBody: Send + 'static,
{
type Subject = P::Subject;

async fn check(&self, request: Self::Subject) -> PredicateResult<Self::Subject> {
match self.inner.check(request).await {
PredicateResult::Cacheable(request) => {
if self.method == request.parts().method {
PredicateResult::Cacheable(request)
} else {
PredicateResult::NonCacheable(request)
}
}
PredicateResult::NonCacheable(request) => PredicateResult::NonCacheable(request),
}
}
}
7 changes: 3 additions & 4 deletions hitbox-http/src/predicates/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
use std::{
marker::PhantomData,
sync::{atomic::AtomicPtr, Arc, RwLock},
};
use std::{marker::PhantomData, sync::atomic::AtomicPtr};

use async_trait::async_trait;
use hitbox_backend::predicates::{Predicate, PredicateResult};
Expand All @@ -10,8 +7,10 @@ use crate::{CacheableHttpRequest, CacheableHttpResponse};

pub mod body;
pub mod header;
pub mod method;
pub mod path;
pub mod query;
pub mod status_code;

pub struct NeutralPredicate<ReqBody> {
_req: PhantomData<AtomicPtr<Box<ReqBody>>>, // FIX: NOT HEHE
Expand Down
23 changes: 9 additions & 14 deletions hitbox-http/src/predicates/path.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use std::marker::PhantomData;

use crate::CacheableHttpRequest;
use actix_router::ResourceDef;
use async_trait::async_trait;
Expand All @@ -11,16 +9,16 @@ pub struct Path<P> {
}

pub trait PathPredicate: Sized {
fn path(self, resource: ResourceDef) -> Path<Self>;
fn path(self, resource: String) -> Path<Self>;
}

impl<P> PathPredicate for P
where
P: Predicate,
{
fn path(self, resource: ResourceDef) -> Path<Self> {
fn path(self, resource: String) -> Path<Self> {
Path {
resource,
resource: ResourceDef::from(resource),
inner: self,
}
}
Expand All @@ -36,16 +34,13 @@ where

async fn check(&self, request: Self::Subject) -> PredicateResult<Self::Subject> {
match self.inner.check(request).await {
PredicateResult::Cacheable(request) => match self.inner.check(request).await {
PredicateResult::Cacheable(request) => {
if self.resource.is_match(request.parts().uri.path()) {
PredicateResult::Cacheable(request)
} else {
PredicateResult::NonCacheable(request)
}
PredicateResult::Cacheable(request) => {
if self.resource.is_match(request.parts().uri.path()) {
PredicateResult::Cacheable(request)
} else {
PredicateResult::NonCacheable(request)
}
PredicateResult::NonCacheable(request) => PredicateResult::NonCacheable(request),
},
}
PredicateResult::NonCacheable(request) => PredicateResult::NonCacheable(request),
}
}
Expand Down
11 changes: 11 additions & 0 deletions hitbox-http/src/predicates/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,17 @@ pub struct Query<P> {
inner: P,
}

impl<P> Query<P> {
pub fn new(name: String, value: crate::query::Value, operation: Operation, inner: P) -> Self {
Self {
name,
value,
operation,
inner,
}
}
}

pub trait QueryPredicate: Sized {
fn query(self, name: String, value: String) -> Query<Self>;
}
Expand Down
46 changes: 46 additions & 0 deletions hitbox-http/src/predicates/status_code.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
use crate::CacheableHttpResponse;
use async_trait::async_trait;
use hitbox::predicates::{Predicate, PredicateResult};

pub struct StatusCode<P> {
status_code: http::StatusCode,
inner: P,
}

pub trait StatusCodePredicate: Sized {
fn status_code(self, status_code: http::StatusCode) -> StatusCode<Self>;
}

impl<P> StatusCodePredicate for P
where
P: Predicate,
{
fn status_code(self, status_code: http::StatusCode) -> StatusCode<Self> {
StatusCode {
status_code,
inner: self,
}
}
}

#[async_trait]
impl<P, ReqBody> Predicate for StatusCode<P>
where
P: Predicate<Subject = CacheableHttpResponse<ReqBody>> + Send + Sync,
ReqBody: Send + 'static,
{
type Subject = P::Subject;

async fn check(&self, response: Self::Subject) -> PredicateResult<Self::Subject> {
match self.inner.check(response).await {
PredicateResult::Cacheable(response) => {
if self.status_code == response.parts.status {
PredicateResult::Cacheable(response)
} else {
PredicateResult::NonCacheable(response)
}
}
PredicateResult::NonCacheable(response) => PredicateResult::NonCacheable(response),
}
}
}
4 changes: 2 additions & 2 deletions hitbox-http/src/response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ where
}

pub struct CacheableHttpResponse<ResBody> {
parts: Parts,
body: ResponseBody<ResBody>,
pub parts: Parts,
pub body: ResponseBody<ResBody>,
}

impl<ResBody> CacheableHttpResponse<ResBody>
Expand Down
1 change: 1 addition & 0 deletions hitbox-tower/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,5 @@ serde = "1"
futures = "0.3"
pin-project = "1"
chrono = "0.4"
axum = "0.6"
bytes = "1"
Loading
Loading