Skip to content

Commit

Permalink
Reworked the upload route to be PUT (gh-8)
Browse files Browse the repository at this point in the history
  • Loading branch information
Bowarc committed Aug 8, 2024
1 parent 18f863b commit ff01450
Show file tree
Hide file tree
Showing 6 changed files with 431 additions and 135 deletions.
24 changes: 18 additions & 6 deletions back/src/cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@ use {
shared::data::{CacheEntry, Metadata},
std::sync::{atomic::Ordering, Arc},
};

#[cfg(not(test))]
const CACHE_DIRECTORY: &'static str = "./cache";
#[cfg(test)]
const CACHE_DIRECTORY: &'static str = "../cache"; // For some reason, tests launch path is ./back
const COMPRESSION_LEVEL: i32 = 5; // 1..=11

#[derive(Default)]
Expand All @@ -21,7 +23,7 @@ impl Cache {
pub fn new() -> Option<Self> {
use std::str::FromStr as _;
let files = std::fs::read_dir(CACHE_DIRECTORY)
.map_err(|e| format!("Could not open cache dir due to: {e}"))
.map_err(|e| error!("Could not open cache dir due to: {e}"))
.ok()?;

// The default one is bad
Expand All @@ -34,7 +36,7 @@ impl Cache {
let metadata = entry
.metadata()
.map_err(|e| {
format!(
error!(
"Could not read metadata from cache file '{p}' due to: {e}",
p = display_path(entry.path())
)
Expand Down Expand Up @@ -71,10 +73,10 @@ impl Cache {

let file_content: serde_json::Value = serde_json::from_str(
&std::fs::read_to_string(path.clone())
.map_err(|e| format!("Could not open cache file '{id}' due to: {e}"))
.map_err(|e| error!("Could not open cache file '{id}' due to: {e}"))
.ok()?,
)
.map_err(|e| format!("Could not deserialize cache file '{id}' due to: {e}"))
.map_err(|e| error!("Could not deserialize cache file '{id}' due to: {e}"))
.ok()?;

let Some(username) = file_content
Expand Down Expand Up @@ -158,10 +160,20 @@ impl Cache {
})
}

pub async fn get_meta(&self, id: uuid::Uuid) -> Result<Metadata, CacheError> {
Ok(self
.data
.iter()
.find(|e| e.id == id)
.ok_or(CacheError::NotFound)?
.metadata
.clone())
}

pub async fn load(&self, id: uuid::Uuid) -> Result<(Metadata, Vec<u8>), CacheError> {
use tokio::io::AsyncReadExt;

// Load and decompress the given cache entry

let entry = self
.data
.iter()
Expand Down
71 changes: 40 additions & 31 deletions back/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ extern crate thiserror;
#[macro_use(trace, debug, info, warn, error)]
extern crate log;

#[macro_use(lazy_static)]
extern crate lazy_static;

mod cache;
mod catchers;
mod error;
Expand All @@ -13,25 +16,15 @@ mod routes;

static mut JSON_REQ_LIMIT: rocket::data::ByteUnit = rocket::data::ByteUnit::Byte(0);

#[rocket::main]
async fn main() {
let logcfg = logger::LoggerConfig::new()
.set_level(log::LevelFilter::Trace)
.add_filter("rocket", log::LevelFilter::Warn);
logger::init(logcfg, Some("log/server.log"));

// Small print to show the start of the program log in the file
trace!(
"\n╭{line}╮\n│{message:^30}│\n╰{line}╯",
line = "─".repeat(30),
message = "Program start"
);

let cache =
rocket::tokio::sync::RwLock::new(cache::Cache::new().expect("Could not load cache"));
// Needed for tests
pub async fn build_rocket() -> rocket::Rocket<rocket::Ignite> {
let Some(cache) = cache::Cache::new() else {
error!("Failled to load cache");
std::process::exit(1)
};

let rocket = rocket::build()
.manage(cache)
.manage(rocket::tokio::sync::RwLock::new(cache))
.register("/", rocket::catchers![catchers::root_403])
.register(
"/upload",
Expand All @@ -44,25 +37,20 @@ async fn main() {
routes::front_js,
routes::front_bg_wasm,
routes::index_html,

routes::static_resource,
routes::static_css,
routes::static_lib_live,
routes::static_lib_zoom,

routes::favicon_ico,

routes::api_upload,
routes::api_upload2,

// routes::api_upload_put,
routes::api_download,
routes::api_download_get,
routes::api_download_get_proxy,
routes::api_download_head,
],
)
.ignite()
.await
.unwrap();
).ignite().await.unwrap();

display_config(rocket.config(), rocket.routes(), rocket.catchers());

// Safety:
// This will only be writen once and at the reads are not yet loaded because the sever is not yet launched
Expand All @@ -74,6 +62,27 @@ async fn main() {
.expect("Failled to read the normal and default config")
}

rocket
}

#[rocket::main]
async fn main() {
let logcfg = logger::LoggerConfig::new()
.set_level(log::LevelFilter::Trace)
.add_filter("rocket", log::LevelFilter::Warn);
logger::init(logcfg, Some("log/server.log"));

// Small print to show the start of the program log in the file
trace!(
"\n╭{line}╮\n│{message:^30}│\n╰{line}╯",
line = "─".repeat(30),
message = "Program start"
);

let rocket = build_rocket().await;

display_config(rocket.config(), rocket.routes(), rocket.catchers());

rocket.launch().await.unwrap();
}

Expand Down Expand Up @@ -115,10 +124,10 @@ fn display_config<'a>(
let name = route
.name
.as_ref()
.map(|name| name.as_ref())
.map(std::borrow::Cow::as_ref)
.unwrap_or("[ERROR] Undefined");
let method = route.method.as_str();
format!("{method:<7} {uri:<15} {name}")
format!("{method:<5} {uri:<20} {name}")
})
.collect::<Vec<String>>();

Expand All @@ -128,14 +137,14 @@ fn display_config<'a>(
let name = catcher
.name
.as_ref()
.map(|name| name.as_ref())
.map(std::borrow::Cow::as_ref)
.unwrap_or("[ERROR] Undefined");
let code = catcher
.code
.map(|code| code.to_string())
.unwrap_or("[ERROR] Undefined".to_string());

format!("{code:<7} {base:<15} {name}")
format!("{code:<5} {base:<20} {name}")
})
.collect::<Vec<String>>();

Expand Down
86 changes: 63 additions & 23 deletions back/src/response.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use rocket::{
http::Status,
http::{ContentType, Status},
serde::json::serde_json::{json, Value as JsonValue},
};

Expand Down Expand Up @@ -62,20 +62,6 @@ impl Default for JsonApiResponseBuilder {
headers: {
let mut h = std::collections::HashMap::new();
h.insert("Content-Type".to_string(), "application/json".to_string());

// Unstable be carefull
h.insert(
"Access-Control-Allow-Origin".to_string(),
"http://localhost:3000".to_string(),
);
h.insert(
"Access-Control-Allow-Method".to_string(),
"POST,GET,OPTIONS".to_string(),
);
h.insert(
"Access-Control-Allow-Headers".to_string(),
"X-PINGOTHER, Content-Type".to_string(),
);
h
},
},
Expand All @@ -84,17 +70,71 @@ impl Default for JsonApiResponseBuilder {
}

pub struct Response {
pub status: Status,
pub content: Vec<u8>,
pub content_type: rocket::http::ContentType, // C TYPE badeu :D
status: Status,
headers: std::collections::HashMap<String, String>,
content: Vec<u8>,
content_type: rocket::http::ContentType,
}

impl<'r> rocket::response::Responder<'r, 'static> for Response {
fn respond_to(self, _: &'r rocket::Request<'_>) -> rocket::response::Result<'static> {
rocket::Response::build()
.header(self.content_type)
.status(self.status)
.sized_body(self.content.len(), std::io::Cursor::new(self.content))
.ok()

let mut resp = rocket::response::Builder::new(rocket::Response::default());

resp.status(self.status);

for (name, value) in self.headers {
resp.raw_header(name, value);
}

resp.sized_body(self.content.len(), std::io::Cursor::new(self.content));

resp.ok()

}
}

pub struct ResponseBuilder {
inner: Response,
}

impl ResponseBuilder {
pub fn with_content(mut self, value: impl Into<Vec<u8>> ) -> Self {
self.inner.content = value.into();
self
}

pub fn with_content_type(mut self, ctype /*C TYPE badeu :D*/: ContentType ) -> Self {
self.inner.content_type = ctype;
self
}

pub fn with_status(mut self, status: Status) -> Self {
self.inner.status = status;
self
}

pub fn with_header(mut self, name: &str, value: &str) -> Self {
self.inner
.headers
.insert(name.to_string(), value.to_string());
self
}

pub fn build(self) -> Response {
self.inner
}
}

impl Default for ResponseBuilder {
fn default() -> Self {
ResponseBuilder {
inner: Response {
status: Status::Ok,
headers: std::collections::HashMap::new(),
content: Vec::new(),
content_type: ContentType::Any,
},
}
}
}
Loading

0 comments on commit ff01450

Please sign in to comment.