revive :)
This commit is contained in:
parent
54d77c8299
commit
319a407ae3
10 changed files with 495 additions and 440 deletions
875
Cargo.lock
generated
875
Cargo.lock
generated
File diff suppressed because it is too large
Load diff
|
@ -7,21 +7,21 @@ edition = "2021"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
futures = "0.3"
|
futures = "0.3"
|
||||||
tokio = { version = "1.35", features = ["full", "tracing"] }
|
tokio = { version = "1.38", features = ["full", "tracing"] }
|
||||||
tracing = "0.1"
|
tracing = "0.1"
|
||||||
tracing-subscriber = "0.3"
|
tracing-subscriber = "0.3"
|
||||||
tokio-stream = "0.1"
|
tokio-stream = "0.1"
|
||||||
tokio-util = "0.7"
|
tokio-util = "0.7"
|
||||||
axum = { version = "0.7" }
|
axum = { version = "0.7" }
|
||||||
reqwest = { version = "0.11", features = ["stream"] }
|
reqwest = { version = "0.12", features = ["stream"] }
|
||||||
anyhow = "1.0"
|
anyhow = "1.0"
|
||||||
sentry = "0.32"
|
sentry = "0.34"
|
||||||
dotenvy = "0.15"
|
dotenvy = "0.15"
|
||||||
axum-macros = "0.4"
|
axum-macros = "0.4"
|
||||||
magic-crypt = "3.1"
|
magic-crypt = "3.1"
|
||||||
async-trait = "0.1"
|
async-trait = "0.1"
|
||||||
serde = "1.0"
|
serde = "1.0"
|
||||||
serde_yaml = "0.9"
|
serde_yaml = "0.9"
|
||||||
base64 = "0.21"
|
base64 = "0.22"
|
||||||
regex = "1.10"
|
regex = "1.10"
|
||||||
serde_regex = "1.1"
|
serde_regex = "1.1"
|
||||||
|
|
|
@ -1,13 +1,13 @@
|
||||||
use std::io::Write;
|
use std::io::Write;
|
||||||
|
|
||||||
use base64::engine::general_purpose;
|
use base64::engine::general_purpose;
|
||||||
use magic_crypt::{new_magic_crypt, MagicCryptTrait};
|
use magic_crypt::{MagicCrypt256, MagicCryptTrait};
|
||||||
|
|
||||||
pub fn decryt<I>(value: I, secret_key: String) -> anyhow::Result<String>
|
pub fn decryt<I>(value: I, secret_key: String) -> anyhow::Result<String>
|
||||||
where
|
where
|
||||||
I: AsRef<str>,
|
I: AsRef<str>,
|
||||||
{
|
{
|
||||||
let mc = new_magic_crypt!(&secret_key, 256);
|
let mc = MagicCrypt256::new(&secret_key, None::<&str>);
|
||||||
Ok(mc.decrypt_base64_to_string(value)?)
|
Ok(mc.decrypt_base64_to_string(value)?)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,5 +1,4 @@
|
||||||
use axum::{routing::get, Router};
|
use axum::{routing::get, Router};
|
||||||
use tokio::sync::Mutex;
|
|
||||||
|
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
|
|
||||||
|
@ -32,9 +31,9 @@ async fn main() -> anyhow::Result<()> {
|
||||||
None => get(routes::handle_unsecure),
|
None => get(routes::handle_unsecure),
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
.with_state(Arc::new(Mutex::new(app_state)));
|
.with_state(Arc::new(app_state));
|
||||||
|
|
||||||
let listener = tokio::net::TcpListener::bind("0.0.0.0:3000").await?;
|
let listener = tokio::net::TcpListener::bind("0.0.0.0:3100").await?;
|
||||||
axum::serve(listener, app).await?;
|
axum::serve(listener, app).await?;
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
|
|
|
@ -14,11 +14,9 @@ use crate::{crypt, state::AppState};
|
||||||
|
|
||||||
pub async fn handle_secure(
|
pub async fn handle_secure(
|
||||||
Path(data): Path<String>,
|
Path(data): Path<String>,
|
||||||
state: State<Arc<Mutex<AppState>>>,
|
state: State<Arc<AppState>>,
|
||||||
) -> Result<impl IntoResponse, MyAnyhow> {
|
) -> Result<impl IntoResponse, MyAnyhow> {
|
||||||
let app_state = state.lock().await;
|
let decrypt = crypt::decryt(data, state.secret_key.clone().unwrap())?;
|
||||||
let decrypt = crypt::decryt(data, app_state.secret_key.clone().unwrap())?;
|
|
||||||
std::mem::drop(app_state);
|
|
||||||
|
|
||||||
let bytes = handle(decrypt, state).await?;
|
let bytes = handle(decrypt, state).await?;
|
||||||
Ok(axum::body::Body::from_stream(bytes))
|
Ok(axum::body::Body::from_stream(bytes))
|
||||||
|
@ -26,7 +24,7 @@ pub async fn handle_secure(
|
||||||
|
|
||||||
pub async fn handle_unsecure(
|
pub async fn handle_unsecure(
|
||||||
Path(src): Path<String>,
|
Path(src): Path<String>,
|
||||||
state: State<Arc<Mutex<AppState>>>,
|
state: State<Arc<AppState>>,
|
||||||
) -> Result<impl IntoResponse, MyAnyhow> {
|
) -> Result<impl IntoResponse, MyAnyhow> {
|
||||||
let bytes = handle(src, state).await?;
|
let bytes = handle(src, state).await?;
|
||||||
|
|
||||||
|
@ -35,20 +33,17 @@ pub async fn handle_unsecure(
|
||||||
|
|
||||||
async fn handle(
|
async fn handle(
|
||||||
src: String,
|
src: String,
|
||||||
state: State<Arc<Mutex<AppState>>>,
|
state: State<Arc<AppState>>,
|
||||||
) -> anyhow::Result<impl Stream<Item = Result<Bytes, anyhow::Error>> + Send> {
|
) -> anyhow::Result<impl Stream<Item = Result<Bytes, anyhow::Error>> + Send> {
|
||||||
let mut app_state = state.lock().await;
|
let mut storage_pool = state.storage_pool.lock().await;
|
||||||
|
|
||||||
match app_state.storage_pool.retrieve(src.clone()).await {
|
match storage_pool.retrieve(src.clone()).await {
|
||||||
Some(stream) => Ok(stream),
|
Some(stream) => Ok(stream),
|
||||||
None => {
|
None => {
|
||||||
let save_stream = fetch(src.clone()).await?;
|
let save_stream = fetch(src.clone()).await?;
|
||||||
app_state
|
storage_pool.save(src.clone(), save_stream).await?;
|
||||||
.storage_pool
|
|
||||||
.save(src.clone(), save_stream)
|
|
||||||
.await?;
|
|
||||||
|
|
||||||
Ok(app_state.storage_pool.retrieve(src).await.unwrap())
|
Ok(storage_pool.retrieve(src).await.unwrap())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,15 +1,17 @@
|
||||||
|
use tokio::sync::Mutex;
|
||||||
|
|
||||||
use crate::storages::StoragePool;
|
use crate::storages::StoragePool;
|
||||||
|
|
||||||
pub struct AppState {
|
pub struct AppState {
|
||||||
pub secret_key: Option<String>,
|
pub secret_key: Option<String>,
|
||||||
pub storage_pool: StoragePool,
|
pub storage_pool: Mutex<StoragePool>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl AppState {
|
impl AppState {
|
||||||
pub fn new(storage_pool: StoragePool, secret_key: Option<String>) -> Self {
|
pub fn new(storage_pool: StoragePool, secret_key: Option<String>) -> Self {
|
||||||
Self {
|
Self {
|
||||||
secret_key,
|
secret_key,
|
||||||
storage_pool,
|
storage_pool: Mutex::new(storage_pool),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -50,6 +50,10 @@ impl Storage for DiskStorage {
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async fn tick(&mut self) -> anyhow::Result<()> {
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
async fn eligible(&self, _src: String) -> bool {
|
async fn eligible(&self, _src: String) -> bool {
|
||||||
true
|
true
|
||||||
}
|
}
|
||||||
|
|
|
@ -33,6 +33,10 @@ impl Storage for MemoryStorage {
|
||||||
true
|
true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async fn tick(&mut self) -> anyhow::Result<()> {
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
async fn delete(&mut self, key: String) -> anyhow::Result<()> {
|
async fn delete(&mut self, key: String) -> anyhow::Result<()> {
|
||||||
self.items
|
self.items
|
||||||
.remove(&key)
|
.remove(&key)
|
||||||
|
@ -64,6 +68,8 @@ impl Storage for MemoryStorage {
|
||||||
self.items.insert(key.clone(), Vec::new());
|
self.items.insert(key.clone(), Vec::new());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
println!("save {}", key);
|
||||||
|
|
||||||
while let Some(chunk) = stream.next().await {
|
while let Some(chunk) = stream.next().await {
|
||||||
self.items
|
self.items
|
||||||
.get_mut(&key)
|
.get_mut(&key)
|
||||||
|
|
|
@ -36,6 +36,10 @@ impl Storage for MixedStorage {
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async fn tick(&mut self) -> anyhow::Result<()> {
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
async fn eligible(&self, _src: String) -> bool {
|
async fn eligible(&self, _src: String) -> bool {
|
||||||
true
|
true
|
||||||
}
|
}
|
||||||
|
|
|
@ -20,6 +20,7 @@ pub trait Storage {
|
||||||
async fn init(&mut self) -> anyhow::Result<()>;
|
async fn init(&mut self) -> anyhow::Result<()>;
|
||||||
async fn eligible(&self, src: String) -> bool;
|
async fn eligible(&self, src: String) -> bool;
|
||||||
async fn delete(&mut self, key: String) -> anyhow::Result<()>;
|
async fn delete(&mut self, key: String) -> anyhow::Result<()>;
|
||||||
|
async fn tick(&mut self) -> anyhow::Result<()>;
|
||||||
async fn retrieve(
|
async fn retrieve(
|
||||||
&self,
|
&self,
|
||||||
key: String,
|
key: String,
|
||||||
|
@ -44,6 +45,7 @@ impl StoragePool {
|
||||||
|
|
||||||
pub async fn init(&mut self) -> anyhow::Result<()> {
|
pub async fn init(&mut self) -> anyhow::Result<()> {
|
||||||
for item in &mut self.storages {
|
for item in &mut self.storages {
|
||||||
|
println!("init storage");
|
||||||
item.init().await?;
|
item.init().await?;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue