Skip to content

Commit

Permalink
feat: request caching
Browse files Browse the repository at this point in the history
  • Loading branch information
RouHim committed Feb 14, 2022
1 parent 1418e5d commit f74a77d
Show file tree
Hide file tree
Showing 7 changed files with 100 additions and 24 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
/target
.idea
cache
72 changes: 62 additions & 10 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ regex = "1.5"
base64 = "0.13"
image = "0.23"
cacache = "10"
rand = "0.8"

[dev-dependencies]
assertor = "0.0"
Expand Down
11 changes: 6 additions & 5 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,13 @@ async fn main() -> std::io::Result<()> {
.wrap(middleware::Logger::default()) // enable logger
.service(
web::scope("/api/resources")
.service(resource_endpoint::list_resources)
.service(resource_endpoint::list_all_resources)
.service(resource_endpoint::list_this_week_resources)
.service(resource_endpoint::get_resource)
.service(resource_endpoint::get_resource_base64)
.service(resource_endpoint::get_resource_metadata)
.service(resource_endpoint::get_resource_metadata_description)
.service(resource_endpoint::random_resource)
.service(resource_endpoint::get_resource_by_id_and_resolution)
.service(resource_endpoint::get_resource_base64_by_id_and_resolution)
.service(resource_endpoint::get_resource_metadata_by_id)
.service(resource_endpoint::get_resource_metadata_description_by_id)
)
.service(Files::new("/", "./static/").index_file("index.html"))
})
Expand Down
23 changes: 18 additions & 5 deletions src/resource_endpoint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use crate::{get, image_processor, resource_processor, WebDavClient, WebDavResour
const CACHE_DIR: &'static str = "./cache";

#[get("")]
pub async fn list_resources(kv_reader: web::Data<ReadHandle<String, String>>) -> HttpResponse {
pub async fn list_all_resources(kv_reader: web::Data<ReadHandle<String, String>>) -> HttpResponse {
let keys: Vec<String> = resource_processor::get_all(kv_reader.as_ref());

HttpResponse::Ok()
Expand All @@ -24,8 +24,21 @@ pub async fn list_this_week_resources(kv_reader: web::Data<ReadHandle<String, St
.body(serde_json::to_string(&keys).unwrap())
}

#[get("random")]
pub async fn random_resource(kv_reader: web::Data<ReadHandle<String, String>>) -> HttpResponse {
let resource_id: Option<String> = resource_processor::random_entry(kv_reader.as_ref());

if let Some(resource_id) = resource_id {
HttpResponse::Ok()
.content_type("plain/text")
.body(resource_id)
} else {
HttpResponse::InternalServerError().finish()
}
}

#[get("{resource_id}/{display_width}/{display_height}")]
pub async fn get_resource(
pub async fn get_resource_by_id_and_resolution(
resources_id: web::Path<(String, u32, u32)>,
kv_reader: web::Data<ReadHandle<String, String>>,
web_dav_client: web::Data<WebDavClient>,
Expand Down Expand Up @@ -60,7 +73,7 @@ pub async fn get_resource(
}

#[get("{resource_id}/{display_width}/{display_height}/base64")]
pub async fn get_resource_base64(
pub async fn get_resource_base64_by_id_and_resolution(
resources_id: web::Path<(String, u32, u32)>,
kv_reader: web::Data<ReadHandle<String, String>>,
web_dav_client: web::Data<WebDavClient>,
Expand Down Expand Up @@ -112,7 +125,7 @@ pub async fn get_resource_base64(
}

#[get("{resource_id}/metadata")]
pub async fn get_resource_metadata(
pub async fn get_resource_metadata_by_id(
resources_id: web::Path<String>,
kv_reader: web::Data<ReadHandle<String, String>>,
) -> HttpResponse {
Expand All @@ -129,7 +142,7 @@ pub async fn get_resource_metadata(
}

#[get("{resource_id}/description")]
pub async fn get_resource_metadata_description(
pub async fn get_resource_metadata_description_by_id(
resources_id: web::Path<String>,
kv_reader: web::Data<ReadHandle<String, String>>,
) -> HttpResponse {
Expand Down
8 changes: 8 additions & 0 deletions src/resource_processor.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::collections::HashMap;

use evmap::ReadHandle;
use rand::Rng;
use serde_json::Value;

use crate::geo_location::GeoLocation;
Expand Down Expand Up @@ -76,4 +77,11 @@ fn get_string_value(field_name: &str, json_data: &HashMap<String, Value>) -> Opt
json_data.get(field_name)
.and_then(|field_value| field_value.as_str())
.map(|field_string_value| field_string_value.to_string())
}

pub fn random_entry(kv_reader: &ReadHandle<String, String>) -> Option<String> {
kv_reader.read().unwrap().len().checked_sub(1).and_then(|len| {
let random_index = rand::thread_rng().gen_range(0..len);
kv_reader.read().unwrap().iter().nth(random_index).map(|(k, _)| k.clone())
})
}
8 changes: 4 additions & 4 deletions src/web_dav_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ impl WebDavClient {
}

pub fn list_all_resources(&self) -> Vec<WebDavResource> {
let body = r#"<?xml version="1.0" encoding="utf-8" ?>
let request_body_xml = r#"<?xml version="1.0" encoding="utf-8" ?>
<D:propfind xmlns:D="DAV:">
<D:allprop/>
</D:propfind>
Expand All @@ -92,17 +92,17 @@ impl WebDavClient {
format!("{}{}", self.base_url, sub_path),
)
.basic_auth(&self.username, Some(&self.password))
.body(Body::from(body))
.body(Body::from(request_body_xml))
.send();

if response.is_err() {
return vec![];
}

let response_body = response.unwrap()
let response_body_xml = response.unwrap()
.text().unwrap();

parse_propfind_result(self, response_body)
parse_propfind_result(self, response_body_xml)
}
}

Expand Down

0 comments on commit f74a77d

Please sign in to comment.