docker and messing with middleware

main
Zynh0722 2024-02-09 22:19:50 -08:00
parent 0a0e975176
commit 2ad8cab9be
4 changed files with 72 additions and 8 deletions

9
Dockerfile Normal file
View File

@ -0,0 +1,9 @@
FROM rust:1.76
WORKDIR /usr/src/lol-pairing-stat-fetcher-rs
COPY . .
RUN cargo install --path .
CMD ["lol-pairing-stat-fetcher-rs"]

13
docker-compose.yml Normal file
View File

@ -0,0 +1,13 @@
volumes:
data:
services:
fetcher:
build: .
depends_on:
- redis
redis:
image: redis
volumes:
- data:/data

View File

@ -21,7 +21,7 @@ pub(crate) fn fetch_api() -> Result<RiotApi, Box<dyn Error>> {
.default_headers(default_headers)
.build()?,
)
.with(RedisMiddleware);
.with(RedisMiddleware::new("redis://redis/")?);
let config = RiotApiConfig::with_client_builder(c_builder);
Ok(RiotApi::new(config))

View File

@ -1,9 +1,28 @@
use std::error::Error;
use redis::{aio::Connection, AsyncCommands, IntoConnectionInfo, RedisResult};
use reqwest::{Request, Response};
use reqwest_middleware::{Middleware, Next, Result};
use reqwest_middleware::{Middleware, Next};
use task_local_extensions::Extensions;
use tracing::info;
pub(crate) struct RedisMiddleware;
type MiddlewareResult<T> = reqwest_middleware::Result<T>;
type MiddlewareError = reqwest_middleware::Error;
pub(crate) struct RedisMiddleware {
pub redis_client: redis::Client,
}
impl RedisMiddleware {
pub(crate) fn new<T: IntoConnectionInfo>(params: T) -> Result<Self, Box<dyn Error>> {
Ok(Self {
redis_client: redis::Client::open(params)?,
})
}
pub async fn get_connection(&self) -> RedisResult<Connection> {
self.redis_client.get_async_connection().await
}
}
#[async_trait::async_trait]
impl Middleware for RedisMiddleware {
@ -12,11 +31,34 @@ impl Middleware for RedisMiddleware {
req: Request,
extensions: &mut Extensions,
next: Next<'_>,
) -> Result<Response> {
info!("Request started: {}", req.url().as_str());
let res = next.run(req, extensions).await;
info!("Request ended: {:?}", res.as_ref().unwrap().url().as_str());
) -> MiddlewareResult<Response> {
// let con = self
// .get_connection()
// .await
// .map_err(|e| MiddlewareError::Middleware(e.into()))?;
res
// let url = req.url().as_str();
// let data = con.get(url).await;
// match data {
// Ok(_) => todo!(),
// Err(_) => todo!(),
// }
info!("Request started: {}", req.url().as_str());
let res = next.run(req, extensions).await?;
// let mut con = self
// .get_connection()
// .await
// .map_err(|e| MiddlewareError::Middleware(e.into()))?;
let url = res.url().to_owned();
// let body = res.text().await?;
// let data: String = con
// .set(url.as_str(), &body)
// .await
// .map_err(|e| MiddlewareError::Middleware(e.into()))?;
info!("Request ended: {:?}", url.as_str());
Ok(res)
}
}