From 5121355d79135298a4fa418191e233310147740a Mon Sep 17 00:00:00 2001 From: lennoxlotl Date: Wed, 23 Oct 2024 22:52:19 +0200 Subject: [PATCH] feat(api): slightly change error handling and index page --- Cargo.lock | 391 +++++++++++++++++++++++++++++++++++ api/Cargo.toml | 4 + api/build.rs | 1 + api/src/endpoint/index.rs | 28 ++- api/src/endpoint/mod.rs | 14 ++ api/src/endpoint/v1/error.rs | 9 +- 6 files changed, 442 insertions(+), 5 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index bc5e205..879a32a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -44,6 +44,27 @@ version = "0.2.18" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5c6cb57a04249c6480766f7f7cef5467412af1490f8d1e243141daddada3264f" +[[package]] +name = "android-tzdata" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e999941b234f3131b00bc13c22d06e8c5ff726d1b6318ac7eb276997bbb4fef0" + +[[package]] +name = "android_system_properties" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "819e7219dbd41043ac279b19830f2efc897156490d7fd6ea916720117ee66311" +dependencies = [ + "libc", +] + +[[package]] +name = "anyhow" +version = "1.0.91" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c042108f3ed77fd83760a5fd79b53be043192bb3b9dba91d8c574c0ada7850c8" + [[package]] name = "api" version = "0.1.0" @@ -52,6 +73,8 @@ dependencies = [ "aws-config", "aws-credential-types", "aws-sdk-s3", + "build-info", + "build-info-build", "log", "macros", "rand", @@ -555,6 +578,15 @@ version = "0.1.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "383d29d513d8764dcdc42ea295d979eb99c3c9f00607b3692cf68a431f7dca72" +[[package]] +name = "bincode" +version = "1.3.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b1f45e9417d87227c7a56d22e471c6206462cba514c7590c09aff4cf6d1ddcad" +dependencies = [ + "serde", +] + [[package]] name = "bitflags" version = "2.6.0" @@ -573,6 +605,76 @@ dependencies = [ "generic-array", ] +[[package]] +name = "build-info" +version = "0.0.39" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "24c8169feaff1e5d9686706c0a56a54ed0422d2df41fa1c543e53f7cea0e70d4" +dependencies = [ + "bincode", + "build-info-common", + "build-info-proc", +] + +[[package]] +name = "build-info-build" +version = "0.0.39" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e73a4a2bdb1e3fae84472b5a9f806208331eb89783bf9b19819cb1d8fbc6d5ec" +dependencies = [ + "anyhow", + "base64 0.22.1", + "bincode", + "build-info-common", + "cargo_metadata", + "chrono", + "git2", + "glob", + "pretty_assertions", + "rustc_version", + "serde_json", + "zstd", +] + +[[package]] +name = "build-info-common" +version = "0.0.39" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8b6fa54101dfbd88efc3981859e92c3d47c729ff54d5df73ec36505c337e5c5e" +dependencies = [ + "chrono", + "derive_more", + "semver", + "serde", +] + +[[package]] +name = "build-info-proc" +version = "0.0.39" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5c5458d2f0ed8bb88f7f6b5706460ca55fab08db16456ea03b920691b4cac163" +dependencies = [ + "anyhow", + "base64 0.22.1", + "bincode", + "build-info-common", + "chrono", + "num-bigint", + "num-traits", + "proc-macro-error2", + "proc-macro2", + "quote", + "serde_json", + "syn", + "zstd", +] + +[[package]] +name = "bumpalo" +version = "3.16.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "79296716171880943b8470b5f8d03aa55eb2e645a4874bdbb28adb49162e012c" + [[package]] name = "bytemuck" version = "1.19.0" @@ -601,12 +703,46 @@ dependencies = [ "either", ] +[[package]] +name = "camino" +version = "1.1.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8b96ec4966b5813e2c0507c1f86115c8c5abaadc3980879c3424042a02fd1ad3" +dependencies = [ + "serde", +] + +[[package]] +name = "cargo-platform" +version = "0.1.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "24b1f0365a6c6bb4020cd05806fd0d33c44d38046b8bd7f0e40814b9763cabfc" +dependencies = [ + "serde", +] + +[[package]] +name = "cargo_metadata" +version = "0.18.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2d886547e41f740c616ae73108f6eb70afe6d940c7bc697cb30f13daec073037" +dependencies = [ + "camino", + "cargo-platform", + "semver", + "serde", + "serde_json", + "thiserror", +] + [[package]] name = "cc" version = "1.1.31" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c2e7962b54006dcfcc61cb72735f4d89bb97061dd6a7ed882ec6b8ee53714c6f" dependencies = [ + "jobserver", + "libc", "shlex", ] @@ -616,6 +752,19 @@ version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" +[[package]] +name = "chrono" +version = "0.4.38" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a21f936df1771bf62b77f047b726c4625ff2e8aa607c01ec06e5a05bd8463401" +dependencies = [ + "android-tzdata", + "iana-time-zone", + "num-traits", + "serde", + "windows-targets 0.52.6", +] + [[package]] name = "concurrent-queue" version = "2.5.0" @@ -777,6 +926,27 @@ dependencies = [ "powerfmt", ] +[[package]] +name = "derive_more" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4a9b99b9cbbe49445b21764dc0625032a89b145a2642e67603e1c936f5458d05" +dependencies = [ + "derive_more-impl", +] + +[[package]] +name = "derive_more-impl" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cb7330aeadfbe296029522e6c40f315320aba36fc43a5b3632f3795348f3bd22" +dependencies = [ + "proc-macro2", + "quote", + "syn", + "unicode-xid", +] + [[package]] name = "devise" version = "0.4.2" @@ -810,6 +980,12 @@ dependencies = [ "syn", ] +[[package]] +name = "diff" +version = "0.1.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "56254986775e3233ffa9c4d7d3faaf6d36a2c09d30b20687e9f88bc8bafc16c8" + [[package]] name = "digest" version = "0.10.7" @@ -1120,6 +1296,19 @@ version = "0.31.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "07e28edb80900c19c28f1072f2e8aeca7fa06b23cd4169cefe1af5aa3260783f" +[[package]] +name = "git2" +version = "0.19.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b903b73e45dc0c6c596f2d37eccece7c1c8bb6e4407b001096387c63d0d93724" +dependencies = [ + "bitflags", + "libc", + "libgit2-sys", + "log", + "url", +] + [[package]] name = "glob" version = "0.3.1" @@ -1345,6 +1534,29 @@ dependencies = [ "tokio-rustls", ] +[[package]] +name = "iana-time-zone" +version = "0.1.61" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "235e081f3925a06703c2d0117ea8b91f042756fd6e7a6e5d901e8ca1a996b220" +dependencies = [ + "android_system_properties", + "core-foundation-sys", + "iana-time-zone-haiku", + "js-sys", + "wasm-bindgen", + "windows-core", +] + +[[package]] +name = "iana-time-zone-haiku" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f31827a206f56af32e590ba56d5d2d085f558508192593743f16b2306495269f" +dependencies = [ + "cc", +] + [[package]] name = "idna" version = "0.5.0" @@ -1389,6 +1601,24 @@ version = "1.0.11" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "49f1f14873335454500d59611f1cf4a4b0f786f9ac11f4312a78e4cf2566695b" +[[package]] +name = "jobserver" +version = "0.1.32" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "48d1dbcbbeb6a7fec7e059840aa538bd62aaccf972c7346c4d9d2059312853d0" +dependencies = [ + "libc", +] + +[[package]] +name = "js-sys" +version = "0.3.72" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6a88f1bda2bd75b0452a14784937d796722fdebfe50df998aeb3f0b7603019a9" +dependencies = [ + "wasm-bindgen", +] + [[package]] name = "lazy_static" version = "1.5.0" @@ -1404,6 +1634,18 @@ version = "0.2.161" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8e9489c2807c139ffd9c1794f4af0ebe86a828db53ecdc7fea2111d0fed085d1" +[[package]] +name = "libgit2-sys" +version = "0.17.0+1.8.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "10472326a8a6477c3c20a64547b0059e4b0d086869eee31e6d7da728a8eb7224" +dependencies = [ + "cc", + "libc", + "libz-sys", + "pkg-config", +] + [[package]] name = "libm" version = "0.2.8" @@ -1421,6 +1663,18 @@ dependencies = [ "vcpkg", ] +[[package]] +name = "libz-sys" +version = "1.1.20" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d2d16453e800a8cf6dd2fc3eb4bc99b786a9b90c663b8559a5b1a041bf89e472" +dependencies = [ + "cc", + "libc", + "pkg-config", + "vcpkg", +] + [[package]] name = "linux-raw-sys" version = "0.4.14" @@ -1594,6 +1848,16 @@ dependencies = [ "winapi", ] +[[package]] +name = "num-bigint" +version = "0.4.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a5e44f723f1133c9deac646763579fdb3ac745e418f2a7af9cd0c431da1f20b9" +dependencies = [ + "num-integer", + "num-traits", +] + [[package]] name = "num-bigint-dig" version = "0.8.4" @@ -1886,6 +2150,38 @@ dependencies = [ "zerocopy", ] +[[package]] +name = "pretty_assertions" +version = "1.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3ae130e2f271fbc2ac3a40fb1d07180839cdbbe443c7a27e1e3c13c5cac0116d" +dependencies = [ + "diff", + "yansi", +] + +[[package]] +name = "proc-macro-error-attr2" +version = "2.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "96de42df36bb9bba5542fe9f1a054b8cc87e172759a1868aa05c1f3acc89dfc5" +dependencies = [ + "proc-macro2", + "quote", +] + +[[package]] +name = "proc-macro-error2" +version = "2.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "11ec05c52be0a07b08061f7dd003e7d7092e0472bc731b4af7bb1ef876109802" +dependencies = [ + "proc-macro-error-attr2", + "proc-macro2", + "quote", + "syn", +] + [[package]] name = "proc-macro2" version = "1.0.89" @@ -2310,6 +2606,9 @@ name = "semver" version = "1.0.23" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "61697e0a1c7e512e84a621326239844a24d8207b4669b41bc18b32ea5cbf988b" +dependencies = [ + "serde", +] [[package]] name = "serde" @@ -3138,6 +3437,61 @@ version = "0.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b8dad83b4f25e74f184f64c43b150b91efe7647395b42289f38e50566d82855b" +[[package]] +name = "wasm-bindgen" +version = "0.2.95" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "128d1e363af62632b8eb57219c8fd7877144af57558fb2ef0368d0087bddeb2e" +dependencies = [ + "cfg-if", + "once_cell", + "wasm-bindgen-macro", +] + +[[package]] +name = "wasm-bindgen-backend" +version = "0.2.95" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cb6dd4d3ca0ddffd1dd1c9c04f94b868c37ff5fac97c30b97cff2d74fce3a358" +dependencies = [ + "bumpalo", + "log", + "once_cell", + "proc-macro2", + "quote", + "syn", + "wasm-bindgen-shared", +] + +[[package]] +name = "wasm-bindgen-macro" +version = "0.2.95" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e79384be7f8f5a9dd5d7167216f022090cf1f9ec128e6e6a482a2cb5c5422c56" +dependencies = [ + "quote", + "wasm-bindgen-macro-support", +] + +[[package]] +name = "wasm-bindgen-macro-support" +version = "0.2.95" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "26c6ab57572f7a24a4985830b120de1594465e5d500f24afe89e16b4e833ef68" +dependencies = [ + "proc-macro2", + "quote", + "syn", + "wasm-bindgen-backend", + "wasm-bindgen-shared", +] + +[[package]] +name = "wasm-bindgen-shared" +version = "0.2.95" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "65fc09f10666a9f147042251e0dda9c18f166ff7de300607007e96bdebc1068d" + [[package]] name = "whoami" version = "1.5.2" @@ -3179,6 +3533,15 @@ dependencies = [ "windows-targets 0.48.5", ] +[[package]] +name = "windows-core" +version = "0.52.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "33ab640c8d7e35bf8ba19b884ba838ceb4fba93a4e8c65a9059d08afcfc683d9" +dependencies = [ + "windows-targets 0.52.6", +] + [[package]] name = "windows-sys" version = "0.48.0" @@ -3377,3 +3740,31 @@ name = "zeroize" version = "1.8.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ced3678a2879b30306d323f4542626697a464a97c0a07c9aebf7ebca65cd4dde" + +[[package]] +name = "zstd" +version = "0.13.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fcf2b778a664581e31e389454a7072dab1647606d44f7feea22cd5abb9c9f3f9" +dependencies = [ + "zstd-safe", +] + +[[package]] +name = "zstd-safe" +version = "7.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "54a3ab4db68cea366acc5c897c7b4d4d1b8994a9cd6e6f841f8964566a419059" +dependencies = [ + "zstd-sys", +] + +[[package]] +name = "zstd-sys" +version = "2.0.13+zstd.1.5.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "38ff0f21cfee8f97d94cef41359e0c89aa6113028ab0291aa8ca0038995a95aa" +dependencies = [ + "cc", + "pkg-config", +] diff --git a/api/Cargo.toml b/api/Cargo.toml index 4d0cb39..a68c81d 100644 --- a/api/Cargo.toml +++ b/api/Cargo.toml @@ -22,3 +22,7 @@ sqlx = { version = "0.8.2", features = [ ] } uuid = { version = "1.11.0", features = ["v4"] } log = "0.4.22" +build-info = "0.0.39" + +[build-dependencies] +build-info-build = "0.0.39" diff --git a/api/build.rs b/api/build.rs index 3a8149e..7cf0676 100644 --- a/api/build.rs +++ b/api/build.rs @@ -1,3 +1,4 @@ fn main() { println!("cargo:rerun-if-changed=migrations"); + build_info_build::build_script(); } diff --git a/api/src/endpoint/index.rs b/api/src/endpoint/index.rs index 8578270..3edea50 100644 --- a/api/src/endpoint/index.rs +++ b/api/src/endpoint/index.rs @@ -1,14 +1,18 @@ use super::{ fairing::{database::PostgresDb, storage::StorageDriverGuard}, v1::{error::Error, UploaderResult}, + SuccessReporter, }; use crate::{database::query::file::find_file_by_id, GlobalConfig}; +use build_info::BuildInfo; use rocket::{ get, http::ContentType, response::{self, Responder}, + serde::json::Json, Request, Response, State, }; +use serde::Serialize; use std::{io::Cursor, str::FromStr}; pub struct FileShowResponse { @@ -17,6 +21,22 @@ pub struct FileShowResponse { cache_time: usize, } +#[derive(Debug, Serialize)] +pub struct ServerInfoResponse { + #[serde(flatten)] + success: SuccessReporter, + version: String, +} + +impl ServerInfoResponse { + pub fn new(version: String) -> Self { + Self { + success: SuccessReporter::new(true), + version, + } + } +} + impl FileShowResponse { pub fn new(data: Vec, content_type: String, cache_time: usize) -> Self { Self { @@ -45,10 +65,10 @@ impl<'r> Responder<'r, 'static> for FileShowResponse { } } -// TODO: come up with something better #[get("/")] -pub async fn index() -> &'static str { - "hi :wave:" +pub async fn index() -> Json { + let info: &BuildInfo = build_info(); + Json(ServerInfoResponse::new(info.crate_info.version.to_string())) } #[get("/")] @@ -69,3 +89,5 @@ pub async fn show_file( config.cache_length.unwrap_or(0), )) } + +build_info::build_info!(fn build_info); diff --git a/api/src/endpoint/mod.rs b/api/src/endpoint/mod.rs index 9d7ae6b..5804a20 100644 --- a/api/src/endpoint/mod.rs +++ b/api/src/endpoint/mod.rs @@ -1,3 +1,17 @@ +use serde::Serialize; + pub mod fairing; pub mod index; pub mod v1; + +/// Responsible for displaying the success status of JSON responses +#[derive(Debug, Serialize)] +pub struct SuccessReporter { + success: bool, +} + +impl SuccessReporter { + pub fn new(success: bool) -> Self { + Self { success } + } +} diff --git a/api/src/endpoint/v1/error.rs b/api/src/endpoint/v1/error.rs index 80fc797..29b5994 100644 --- a/api/src/endpoint/v1/error.rs +++ b/api/src/endpoint/v1/error.rs @@ -7,7 +7,7 @@ use rocket::{ }; use serde::Serialize; -use crate::storage::driver::StorageError; +use crate::{endpoint::SuccessReporter, storage::driver::StorageError}; /// Stores attributes about an error pub struct ErrorAttributes { @@ -41,12 +41,17 @@ pub enum Error { #[derive(Debug, Serialize)] pub struct RocketErrorResponse { + #[serde(flatten)] + success: SuccessReporter, message: String, } impl RocketErrorResponse { pub fn new(message: String) -> Self { - Self { message } + Self { + success: SuccessReporter::new(false), + message, + } } }