always serve embedded frontend
This commit is contained in:
+28
-40
@@ -30,7 +30,6 @@ struct RawConfig {
|
||||
max_retention_days: i64,
|
||||
rate_limit_requests: usize,
|
||||
rate_limit_window: u64,
|
||||
production: bool,
|
||||
}
|
||||
|
||||
impl Default for RawConfig {
|
||||
@@ -49,7 +48,6 @@ impl Default for RawConfig {
|
||||
max_retention_days: 3650,
|
||||
rate_limit_requests: 60,
|
||||
rate_limit_window: 60,
|
||||
production: false,
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -68,7 +66,6 @@ pub struct Config {
|
||||
pub max_retention_days: i64,
|
||||
pub rate_limit_requests: usize,
|
||||
pub rate_limit_window: u64,
|
||||
pub production: bool,
|
||||
pub base_path: String,
|
||||
}
|
||||
|
||||
@@ -152,7 +149,6 @@ impl Config {
|
||||
max_retention_days: raw.max_retention_days,
|
||||
rate_limit_requests: raw.rate_limit_requests,
|
||||
rate_limit_window: raw.rate_limit_window,
|
||||
production: raw.production,
|
||||
base_path,
|
||||
})
|
||||
}
|
||||
@@ -372,10 +368,7 @@ impl App {
|
||||
|
||||
let raw_path = target_path(&request.target);
|
||||
let bare_base = !self.config.base_path.is_empty() && raw_path == self.config.base_path;
|
||||
if matches!(request.method.as_str(), "GET" | "HEAD")
|
||||
&& !self.config.production
|
||||
&& !bare_base
|
||||
{
|
||||
if matches!(request.method.as_str(), "GET" | "HEAD") && !bare_base {
|
||||
if let Some(path) = local_path(raw_path, &self.config.base_path) {
|
||||
match path.as_str() {
|
||||
"/" | "/static" => return self.serve_static("index.html"),
|
||||
@@ -432,32 +425,14 @@ impl App {
|
||||
|
||||
fn handle_get(&self, path: &str, request: &RequestData) -> ResponseData {
|
||||
match path {
|
||||
"/" => {
|
||||
if self.config.production {
|
||||
health_response()
|
||||
} else {
|
||||
self.serve_static("index.html")
|
||||
}
|
||||
}
|
||||
"/" => self.serve_static("index.html"),
|
||||
"/api/health" => health_response(),
|
||||
"/api/shorten" => self.handle_shorten(request),
|
||||
"/api/urls" => self.handle_list_urls(request),
|
||||
"/api/lookup" => self.handle_lookup(request),
|
||||
"/static" => {
|
||||
if self.config.production {
|
||||
ResponseData::empty(404)
|
||||
} else {
|
||||
self.serve_static("index.html")
|
||||
}
|
||||
}
|
||||
"/static" => self.serve_static("index.html"),
|
||||
_ if path.starts_with("/api/urls/") => self.handle_get_url(&path["/api/urls/".len()..]),
|
||||
_ if path.starts_with("/static/") => {
|
||||
if self.config.production {
|
||||
ResponseData::empty(404)
|
||||
} else {
|
||||
self.serve_static(&path["/static/".len()..])
|
||||
}
|
||||
}
|
||||
_ if path.starts_with("/static/") => self.serve_static(&path["/static/".len()..]),
|
||||
_ => self.handle_redirect(path.trim_start_matches('/')),
|
||||
}
|
||||
}
|
||||
@@ -1477,19 +1452,32 @@ mod tests {
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn production_flag_keeps_legacy_backend_static_behavior() {
|
||||
let path = temp_path("production");
|
||||
let mut cfg = config(path.clone());
|
||||
cfg.production = true;
|
||||
let app = App::new(cfg).unwrap();
|
||||
fn deprecated_production_key_is_ignored_and_frontend_remains_enabled() {
|
||||
let path = temp_path("deprecated-production");
|
||||
let config_path = std::env::temp_dir().join(format!(
|
||||
"ushort-deprecated-production-{}-{}.toml",
|
||||
std::process::id(),
|
||||
rand::random::<u64>()
|
||||
));
|
||||
fs::write(
|
||||
&config_path,
|
||||
format!(
|
||||
"base_url = \"https://example.test/s\"\n\
|
||||
api_key = \"secret\"\n\
|
||||
db_path = \"{}\"\n\
|
||||
production = true\n",
|
||||
path.display()
|
||||
),
|
||||
)
|
||||
.unwrap();
|
||||
let app = App::new(Config::load(&config_path).unwrap()).unwrap();
|
||||
let root = app.handle(RequestData::new("GET", "/s/"));
|
||||
assert_eq!(
|
||||
root.body,
|
||||
br#"{"status": "ok", "service": "url-shortener"}"#
|
||||
);
|
||||
assert_eq!(root.status, 200);
|
||||
assert_eq!(root.body, include_bytes!("../static/index.html"));
|
||||
let asset = app.handle(RequestData::new("GET", "/s/static/app.js"));
|
||||
assert_eq!(asset.status, 404);
|
||||
assert!(asset.body.is_empty());
|
||||
assert_eq!(asset.status, 200);
|
||||
assert_eq!(asset.body, include_bytes!("../static/app.js"));
|
||||
let _ = fs::remove_file(config_path);
|
||||
let _ = fs::remove_file(path);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user