diff options
-rw-r--r-- | Cargo.lock | 129 | ||||
-rw-r--r-- | Cargo.toml | 7 | ||||
-rw-r--r-- | flake.nix | 7 | ||||
-rw-r--r-- | src/errors.rs | 30 | ||||
-rw-r--r-- | src/handlers/mod.rs | 2 | ||||
-rw-r--r-- | src/handlers/waifus.rs | 18 | ||||
-rw-r--r-- | src/main.rs | 7 |
7 files changed, 198 insertions, 2 deletions
@@ -3,6 +3,21 @@ version = 3 [[package]] +name = "addr2line" +version = "0.17.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b9ecd88a8c8378ca913a680cd98f0f13ac67383d35993f86c90a70e3f137816b" +dependencies = [ + "gimli", +] + +[[package]] +name = "adler" +version = "1.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f26201604c87b1e01bd3d98f8d5d9a8fcbb815e8cedb41ffccbeb4bf593a35fe" + +[[package]] name = "ansi_term" version = "0.12.1" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -74,6 +89,21 @@ dependencies = [ ] [[package]] +name = "backtrace" +version = "0.3.65" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "11a17d453482a265fd5f8479f2a3f405566e6ca627837aaddb85af8b1ab8ef61" +dependencies = [ + "addr2line", + "cc", + "cfg-if", + "libc", + "miniz_oxide", + "object", + "rustc-demangle", +] + +[[package]] name = "base64" version = "0.13.0" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -110,12 +140,49 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" [[package]] +name = "color-eyre" +version = "0.6.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8ebf286c900a6d5867aeff75cfee3192857bb7f24b547d4f0df2ed6baa812c90" +dependencies = [ + "backtrace", + "color-spantrace", + "eyre", + "indenter", + "once_cell", + "owo-colors", + "tracing-error", +] + +[[package]] +name = "color-spantrace" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1ba75b3d9449ecdccb27ecbc479fdc0b87fa2dd43d2f8298f9bf0e59aacc8dce" +dependencies = [ + "once_cell", + "owo-colors", + "tracing-core", + "tracing-error", +] + +[[package]] name = "either" version = "1.6.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e78d4f1cc4ae33bbfc157ed5d5a5ef3bc29227303d595861deb238fcec4e9457" [[package]] +name = "eyre" +version = "0.6.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4c2b6b5a29c02cdc822728b7d7b8ae1bab3e3b05d44522770ddd49722eeac7eb" +dependencies = [ + "indenter", + "once_cell", +] + +[[package]] name = "fnv" version = "1.0.7" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -182,6 +249,12 @@ dependencies = [ ] [[package]] +name = "gimli" +version = "0.26.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "78cc372d058dcf6d5ecd98510e7fbc9e5aec4d21de70f65fea8fecebcd881bd4" + +[[package]] name = "h2" version = "0.3.13" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -280,6 +353,12 @@ dependencies = [ ] [[package]] +name = "indenter" +version = "0.3.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ce23b50ad8242c51a442f3ff322d56b02f08852c77e4c0b4d3fd684abc89c683" + +[[package]] name = "indexmap" version = "1.8.1" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -305,6 +384,12 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1aab8fc367588b89dcee83ab0fd66b72b50b72fa1904d7095045ace2b0c81c35" [[package]] +name = "kankyo" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "325a11231fa70c1d1b562655db757cefb6022876d62f173831f35bd670ae0c40" + +[[package]] name = "lazy_static" version = "1.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -400,6 +485,15 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "68354c5c6bd36d73ff3feceb05efa59b6acb7626617f4962be322a825e61f79a" [[package]] +name = "miniz_oxide" +version = "0.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d2b29bd4bc3f33391105ebee3589c19197c4271e3e5a9ec9bfe8127eeff8f082" +dependencies = [ + "adler", +] + +[[package]] name = "mio" version = "0.8.2" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -465,12 +559,27 @@ dependencies = [ ] [[package]] +name = "object" +version = "0.28.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "40bec70ba014595f99f7aa110b84331ffe1ee9aece7fe6f387cc7e3ecda4d456" +dependencies = [ + "memchr", +] + +[[package]] name = "once_cell" version = "1.10.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "87f3e037eac156d1775da914196f0f37741a274155e34a0b7e427c35d2a2ecb9" [[package]] +name = "owo-colors" +version = "3.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "decf7381921fea4dcb2549c5667eda59b3ec297ab7e2b5fc33eac69d2e7da87b" + +[[package]] name = "parking_lot" version = "0.12.0" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -632,6 +741,12 @@ dependencies = [ ] [[package]] +name = "rustc-demangle" +version = "0.1.21" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7ef03e0a2b150c7a90d01faf6254c9c48a41e95fb2a8c2ac1c6f0d2b9aefc342" + +[[package]] name = "ryu" version = "1.0.9" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -919,6 +1034,16 @@ dependencies = [ ] [[package]] +name = "tracing-error" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d686ec1c0f384b1277f097b2f279a2ecc11afe8c133c1aabf036a27cb4cd206e" +dependencies = [ + "tracing", + "tracing-subscriber", +] + +[[package]] name = "tracing-log" version = "0.1.3" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -988,8 +1113,12 @@ name = "waifud" version = "0.1.0" dependencies = [ "axum", + "color-eyre", + "eyre", "hyper", + "kankyo", "serde", + "thiserror", "tokio", "tower", "tower-http 0.3.0", @@ -12,6 +12,13 @@ tracing-subscriber = { version = "0.3", features = [ "env-filter" ] } tower = { version = "0.4", features = [ "util", "timeout" ] } tower-http = { version = "0.3", features = [ "add-extension", "trace" ] } +color-eyre = "0.6" +eyre = "0.6" + +thiserror = "1" + +kankyo = "0.3" + [dependencies.waifulib] git = "https://git.carathe.dev/muirrum/waifulib" branch = "master" @@ -9,12 +9,17 @@ system: let pkgs = nixpkgs.legacyPackages."${system}"; naersk-lib = naersk.lib."${system}"; + deps = with pkgs; [ + libvirt + ]; in rec { # `nix build` packages.waifud = naersk-lib.buildPackage { pname = "waifud"; root = ./.; + nativeBuildInputs = deps; + buildInputs = deps; }; defaultPackage = packages.waifud; @@ -71,7 +76,7 @@ # `nix develop` devShell = pkgs.mkShell { - nativeBuildInputs = with pkgs; [ rustc cargo ]; + nativeBuildInputs = with pkgs; [ rustc cargo ] ++ deps; }; } ); diff --git a/src/errors.rs b/src/errors.rs new file mode 100644 index 0000000..a538e4a --- /dev/null +++ b/src/errors.rs @@ -0,0 +1,30 @@ +use thiserror::Error; + +use axum::response::{Response, IntoResponse}; +use axum::http::StatusCode; +use axum::body; +use axum::Json; + +#[derive(Debug, Error)] +pub enum ServiceError { + #[error("Waifulib error: {0}")] + Waifulib(#[from] waifulib::errors::Error), + + #[error("Axum error: {0}")] + Axum(#[from] axum::Error), +} + +pub type StringResult<T = &'static str> = std::result::Result<T, ServiceError>; + +pub type JsonResult<T> = std::result::Result<T, ServiceError>; + +impl IntoResponse for ServiceError { + fn into_response(self) -> Response { + + let body = body::boxed(body::Full::from(self.to_string())); + Response::builder() + .status(StatusCode::INTERNAL_SERVER_ERROR) + .body(body) + .unwrap() + } +} diff --git a/src/handlers/mod.rs b/src/handlers/mod.rs index 097d8a6..addf742 100644 --- a/src/handlers/mod.rs +++ b/src/handlers/mod.rs @@ -1 +1 @@ -pub waifus; +pub mod waifus; diff --git a/src/handlers/waifus.rs b/src/handlers/waifus.rs new file mode 100644 index 0000000..117ba15 --- /dev/null +++ b/src/handlers/waifus.rs @@ -0,0 +1,18 @@ +use axum::{response::IntoResponse, Json}; + +use tracing::{error, instrument}; + +use waifulib::house::House; +use waifulib::waifu::Waifu; + +use crate::errors::*; + +pub async fn list() -> JsonResult<Json<Vec<Waifu>>> { + let con_url = std::env::var("QEMU_URL").unwrap_or("qemu:///system".to_string()); + let mut house: House = House::new(con_url)?; + + let inhabitants = house.inhabitants()?; + + Ok(Json(inhabitants)) +} + diff --git a/src/main.rs b/src/main.rs index 5777fa6..d2f83a4 100644 --- a/src/main.rs +++ b/src/main.rs @@ -14,8 +14,14 @@ use tower_http::trace::TraceLayer; use tracing_subscriber::prelude::*; +mod errors; + +mod handlers; + #[tokio::main] async fn main() { + kankyo::init(); + color_eyre::install().unwrap(); tracing_subscriber::registry() .with(tracing_subscriber::EnvFilter::new( std::env::var("RUST_LOG") @@ -26,6 +32,7 @@ async fn main() { let app = Router::new() .route("/health", get(health_check)) + .route("/waifus/list", get(handlers::waifus::list)) .layer( ServiceBuilder::new() .layer(HandleErrorLayer::new(|error: BoxError| async move { if error.is::<tower::timeout::error::Elapsed>() { |