docker and messing with middleware
This commit is contained in:
parent
0a0e975176
commit
2ad8cab9be
4 changed files with 72 additions and 8 deletions
9
Dockerfile
Normal file
9
Dockerfile
Normal 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
13
docker-compose.yml
Normal file
|
@ -0,0 +1,13 @@
|
||||||
|
volumes:
|
||||||
|
data:
|
||||||
|
|
||||||
|
services:
|
||||||
|
fetcher:
|
||||||
|
build: .
|
||||||
|
depends_on:
|
||||||
|
- redis
|
||||||
|
|
||||||
|
redis:
|
||||||
|
image: redis
|
||||||
|
volumes:
|
||||||
|
- data:/data
|
|
@ -21,7 +21,7 @@ pub(crate) fn fetch_api() -> Result<RiotApi, Box<dyn Error>> {
|
||||||
.default_headers(default_headers)
|
.default_headers(default_headers)
|
||||||
.build()?,
|
.build()?,
|
||||||
)
|
)
|
||||||
.with(RedisMiddleware);
|
.with(RedisMiddleware::new("redis://redis/")?);
|
||||||
let config = RiotApiConfig::with_client_builder(c_builder);
|
let config = RiotApiConfig::with_client_builder(c_builder);
|
||||||
|
|
||||||
Ok(RiotApi::new(config))
|
Ok(RiotApi::new(config))
|
||||||
|
|
56
src/cache.rs
56
src/cache.rs
|
@ -1,9 +1,28 @@
|
||||||
|
use std::error::Error;
|
||||||
|
|
||||||
|
use redis::{aio::Connection, AsyncCommands, IntoConnectionInfo, RedisResult};
|
||||||
use reqwest::{Request, Response};
|
use reqwest::{Request, Response};
|
||||||
use reqwest_middleware::{Middleware, Next, Result};
|
use reqwest_middleware::{Middleware, Next};
|
||||||
use task_local_extensions::Extensions;
|
use task_local_extensions::Extensions;
|
||||||
use tracing::info;
|
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]
|
#[async_trait::async_trait]
|
||||||
impl Middleware for RedisMiddleware {
|
impl Middleware for RedisMiddleware {
|
||||||
|
@ -12,11 +31,34 @@ impl Middleware for RedisMiddleware {
|
||||||
req: Request,
|
req: Request,
|
||||||
extensions: &mut Extensions,
|
extensions: &mut Extensions,
|
||||||
next: Next<'_>,
|
next: Next<'_>,
|
||||||
) -> Result<Response> {
|
) -> MiddlewareResult<Response> {
|
||||||
info!("Request started: {}", req.url().as_str());
|
// let con = self
|
||||||
let res = next.run(req, extensions).await;
|
// .get_connection()
|
||||||
info!("Request ended: {:?}", res.as_ref().unwrap().url().as_str());
|
// .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)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue