-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: organize into modules and add api endpoints
- Loading branch information
Showing
6 changed files
with
194 additions
and
49 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
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,68 @@ | ||
use std::str::FromStr; | ||
|
||
use crate::index_cache::IndexCache; | ||
use crate::AppState; | ||
use axum::extract::Path; | ||
use axum::http::StatusCode; | ||
use axum::{extract::State, response::IntoResponse, Json}; | ||
use serde::Serialize; | ||
use tracing::{debug, warn}; | ||
use uuid::Uuid; | ||
|
||
// handle get cache endpoint | ||
pub async fn cache_stats(State(state): State<AppState>) -> impl IntoResponse { | ||
debug!("new request to get cache stats"); | ||
let stats = CacheStats { | ||
name: state.cache.name().unwrap_or_default().to_string(), | ||
entries: state.cache.entry_count(), | ||
size: state.cache.weighted_size(), | ||
}; | ||
(StatusCode::OK, Json(stats)) | ||
} | ||
#[derive(Serialize)] | ||
struct CacheStats { | ||
name: String, | ||
entries: u64, | ||
size: u64, | ||
} | ||
|
||
// handle delete endpoint | ||
// will also delete from index by iterating over the entries to find the method/path | ||
pub async fn delete_entry( | ||
Path(path): Path<String>, | ||
State(state): State<AppState>, | ||
) -> impl IntoResponse { | ||
debug!("new request to delete a cache entry"); | ||
if let Ok(uuid) = Uuid::from_str(&path) { | ||
state.cache.invalidate(&uuid).await; | ||
state.index_cache.lock().await.delete_uuid_from_index(&uuid); | ||
debug!("cache entry removed"); | ||
return StatusCode::OK; | ||
} | ||
warn!("deletion request for invalid uuid"); | ||
StatusCode::NOT_FOUND | ||
} | ||
// handle raw entry endpoint | ||
// will return the raw data of a cache entry | ||
// it is present for debugging purposes. | ||
pub async fn get_cache_entry( | ||
Path(path): Path<String>, | ||
State(state): State<AppState>, | ||
) -> impl IntoResponse { | ||
debug!("new request to return a raw cache entry"); | ||
if let Ok(uuid) = Uuid::from_str(&path) { | ||
if let Some(entry) = state.cache.get(&uuid).await { | ||
return entry.into_response(); | ||
} | ||
} | ||
warn!("deletion request for invalid uuid"); | ||
StatusCode::NOT_FOUND.into_response() | ||
} | ||
// handle delete_all endpoint | ||
pub async fn delete_entries(State(state): State<AppState>) -> impl IntoResponse { | ||
debug!("new request to delete all cache entries"); | ||
state.cache.invalidate_all(); | ||
*state.index_cache.lock().await = IndexCache::new(); | ||
debug!("all cache cleared"); | ||
StatusCode::OK | ||
} |
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,78 @@ | ||
use axum::{ | ||
extract::{Path, State}, | ||
response::IntoResponse, | ||
}; | ||
use reqwest::StatusCode; | ||
use tracing::debug; | ||
use url::Url; | ||
|
||
use crate::AppState; | ||
|
||
// handle delete endpoint | ||
pub async fn delete_endpoint( | ||
Path(path): Path<String>, | ||
State(state): State<AppState>, | ||
) -> impl IntoResponse { | ||
debug!("new request to delete an endpoint in configuration"); | ||
if let Some(index) = state | ||
.config | ||
.lock() | ||
.await | ||
.endpoints | ||
.iter() | ||
.position(|x| *x.0 == path) | ||
{ | ||
// delete endpoint | ||
state.config.lock().await.endpoints.remove(index); | ||
// write config | ||
|
||
// return success | ||
return StatusCode::OK; | ||
} | ||
// return not found | ||
StatusCode::NOT_FOUND | ||
} | ||
// handle add endpoint | ||
pub async fn add_endpoint( | ||
Path(path): Path<String>, | ||
State(state): State<AppState>, | ||
) -> impl IntoResponse { | ||
debug!("new request to delete an endpoint in configuration"); | ||
if let Some(index) = state | ||
.config | ||
.lock() | ||
.await | ||
.endpoints | ||
.iter() | ||
.position(|x| *x.0 == path) | ||
{ | ||
// delete endpoint | ||
state.config.lock().await.endpoints.remove(index); | ||
// write config | ||
|
||
// return success | ||
return StatusCode::OK; | ||
} | ||
// return not found | ||
StatusCode::NOT_FOUND | ||
} | ||
pub async fn set_fallback_value(State(state): State<AppState>, body: String) -> impl IntoResponse { | ||
debug!("new request to set the fallback in configuration"); | ||
if let Ok(url) = Url::parse(&body) { | ||
state.config.lock().await.fall_back_endpoint = url; | ||
} | ||
// return not found | ||
StatusCode::NOT_FOUND | ||
} | ||
pub async fn get_fallback_value(State(state): State<AppState>) -> impl IntoResponse { | ||
debug!("new request to get the fallback in configuration"); | ||
let body = &state.config.lock().await.fall_back_endpoint; | ||
// return not found | ||
(StatusCode::NOT_FOUND, body.to_string()) | ||
} | ||
// handle delete all endpoints | ||
pub async fn delete_endpoints(State(state): State<AppState>) -> impl IntoResponse { | ||
debug!("new request to delete all endpoints in configuration"); | ||
state.config.lock().await.endpoints = Vec::new(); | ||
StatusCode::OK | ||
} |
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