-
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: Switch from axum to Rocket for better ergonomics and correctnes…
…s with blocking/sync libraries
- Loading branch information
1 parent
7e1f2ef
commit e7b4fe8
Showing
26 changed files
with
2,000 additions
and
1,475 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
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
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,13 @@ | ||
[default] | ||
log_level = "debug" | ||
|
||
[default.databases.demo] | ||
url = "data/demo.db" | ||
timeout = 10 | ||
|
||
[docker] | ||
address = "0.0.0.0" | ||
|
||
[docker.databases.demo] | ||
url = "/data/demo.db" | ||
timeout = 10 |
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 |
---|---|---|
@@ -1,54 +1,48 @@ | ||
use axum::{ | ||
body::Body, | ||
http::{header, StatusCode, Uri}, | ||
response::{IntoResponse, Response}, | ||
use rocket::{ | ||
fairing::AdHoc, | ||
http::{ContentType, Header}, | ||
response::Responder, | ||
}; | ||
use rust_embed::RustEmbed; | ||
use tracing::info; | ||
use std::borrow::Cow; | ||
use std::ffi::OsStr; | ||
use std::path::PathBuf; | ||
|
||
#[derive(RustEmbed)] | ||
#[folder = "$CARGO_MANIFEST_DIR/ui/target/public/"] | ||
pub(crate) struct Assets; | ||
|
||
pub(crate) struct StaticFile<T>(pub(crate) T); | ||
pub(crate) struct Asset; | ||
|
||
#[derive(Responder)] | ||
#[response(status = 200)] | ||
struct AssetResponse { | ||
content: Cow<'static, [u8]>, | ||
content_type: ContentType, | ||
max_age: Header<'static>, | ||
} | ||
|
||
#[cfg(debug_assertions)] | ||
const MAX_AGE: &str = "max-age=120"; | ||
#[cfg(not(debug_assertions))] | ||
const MAX_AGE: &str = "max-age=31536000"; | ||
|
||
impl<T> IntoResponse for StaticFile<T> | ||
where | ||
T: Into<String>, | ||
{ | ||
fn into_response(self) -> Response { | ||
let path = self.0.into(); | ||
|
||
match Assets::get(path.as_str()) { | ||
Some(content) => { | ||
info!("Retrieving asset with path: {path}"); | ||
let body = Body::from(content.data); | ||
let mime = mime_guess::from_path(path).first_or_octet_stream(); | ||
Response::builder() | ||
.header(header::CONTENT_TYPE, mime.as_ref()) | ||
.header(header::CACHE_CONTROL, MAX_AGE) | ||
.body(body) | ||
.unwrap() | ||
} | ||
None => Response::builder() | ||
.status(StatusCode::NOT_FOUND) | ||
.body(Body::from("Not Found")) | ||
.unwrap(), | ||
} | ||
} | ||
pub(crate) fn stage() -> AdHoc { | ||
AdHoc::on_ignite("Assets Stage", |rocket| async { | ||
rocket.mount("/dist", routes![asset_handler]) | ||
}) | ||
} | ||
|
||
pub(crate) async fn asset_handler(uri: Uri) -> impl IntoResponse { | ||
let mut path = uri.path().trim_start_matches('/').to_string(); | ||
|
||
if path.starts_with("dist/") { | ||
path = path.replace("dist/", ""); | ||
} | ||
|
||
StaticFile(path) | ||
#[get("/<file..>")] | ||
fn asset_handler(file: PathBuf) -> Option<AssetResponse> { | ||
let filename = file.display().to_string(); | ||
let asset = Asset::get(&filename)?; | ||
let content_type = file | ||
.extension() | ||
.and_then(OsStr::to_str) | ||
.and_then(ContentType::from_extension) | ||
.unwrap_or(ContentType::Bytes); | ||
Some(AssetResponse { | ||
content: asset.data, | ||
content_type, | ||
max_age: Header::new("Cache-Control", MAX_AGE), | ||
}) | ||
} |
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,56 @@ | ||
use crate::{currency::FORM_CURRENCY_REGEX, time::DATE_REGEX}; | ||
use once_cell::sync::Lazy; | ||
use regex::Regex; | ||
use rocket::form::{Contextual, Form}; | ||
|
||
pub(crate) static QUANTITY_REGEX: Lazy<Regex> = Lazy::new(|| Regex::new(r"^\d+$").unwrap()); | ||
|
||
pub(crate) fn css_for_field<'b, T>( | ||
form: &Form<Contextual<'_, T>>, | ||
field: &'b str, | ||
default_class: &'b str, | ||
error_class: &'b str, | ||
) -> String { | ||
if form.context.exact_field_errors(field).count() == 0 { | ||
default_class.to_string() | ||
} else { | ||
format!("{} {}", default_class, error_class) | ||
} | ||
} | ||
|
||
pub(crate) fn validate_date<'v>(date: &str) -> rocket::form::Result<'v, ()> { | ||
if date.is_empty() { | ||
Err(rocket::form::Error::validation("Please enter a date"))?; | ||
} | ||
if !DATE_REGEX.is_match(date) { | ||
Err(rocket::form::Error::validation("Please enter a valid date"))?; | ||
} | ||
|
||
Ok(()) | ||
} | ||
|
||
pub(crate) fn validate_amount<'v>(amount: &str) -> rocket::form::Result<'v, ()> { | ||
if amount.is_empty() { | ||
Err(rocket::form::Error::validation("Please enter an amount"))?; | ||
} | ||
if !FORM_CURRENCY_REGEX.is_match(amount) { | ||
Err(rocket::form::Error::validation( | ||
"Please enter a valid amount", | ||
))?; | ||
} | ||
|
||
Ok(()) | ||
} | ||
|
||
pub(crate) fn validate_quantity<'v>(quantity: &str) -> rocket::form::Result<'v, ()> { | ||
if quantity.is_empty() { | ||
Err(rocket::form::Error::validation("Please enter a quantity"))?; | ||
} | ||
if !QUANTITY_REGEX.is_match(quantity) { | ||
Err(rocket::form::Error::validation( | ||
"Please enter a valid quantity", | ||
))?; | ||
} | ||
|
||
Ok(()) | ||
} |
Oops, something went wrong.