aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorCara Salter <cara@devcara.com>2022-04-27 14:44:33 -0400
committerCara Salter <cara@devcara.com>2022-04-27 14:44:33 -0400
commitbc3132f22f3470d8b1d8189e23393d3bb4577480 (patch)
treeb8d6466e4191a4e7e5e4a755102e82ffbff56b36 /src
parent9696669ff126cd2bcbc31e90651b464432b7947f (diff)
downloadsolard-bc3132f22f3470d8b1d8189e23393d3bb4577480.tar.gz
solard-bc3132f22f3470d8b1d8189e23393d3bb4577480.zip
List waifus
Also use environment variables and new error types
Diffstat (limited to 'src')
-rw-r--r--src/errors.rs30
-rw-r--r--src/handlers/mod.rs2
-rw-r--r--src/handlers/waifus.rs18
-rw-r--r--src/main.rs7
4 files changed, 56 insertions, 1 deletions
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>() {