From a739b00bc366758be9c452979cc4b4c831dd8a75 Mon Sep 17 00:00:00 2001 From: Cara Salter Date: Tue, 28 Jun 2022 09:42:00 -0400 Subject: tool: FMT --- src/main.rs | 37 ++++++++++++++++++++++++++----------- 1 file changed, 26 insertions(+), 11 deletions(-) (limited to 'src/main.rs') diff --git a/src/main.rs b/src/main.rs index 3f889cd..97050e8 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,16 +1,17 @@ use axum::{ error_handling::HandleErrorLayer, + handler::Handler, http::StatusCode, + middleware, response::IntoResponse, routing::{get, post}, - handler::Handler, - Json, Router, Extension + Extension, Json, Router, }; -use rand::{thread_rng, Rng, distributions::Alphanumeric}; +use rand::{distributions::Alphanumeric, thread_rng, Rng}; use serde::{Deserialize, Serialize}; use solarlib::star::Star; -use std::{net::SocketAddr, time::Duration, str::FromStr, sync::Arc}; +use std::{net::SocketAddr, str::FromStr, sync::Arc, time::Duration}; use tower::{BoxError, ServiceBuilder}; use tower_http::trace::TraceLayer; @@ -32,10 +33,9 @@ pub struct State { async fn main() { kankyo::init(); color_eyre::install().unwrap(); - tracing_subscriber::registry() + tracing_subscriber::registry() .with(tracing_subscriber::EnvFilter::new( - std::env::var("RUST_LOG") - .unwrap_or_else(|_| "solard=info,tower_http=debug".into()), + std::env::var("RUST_LOG").unwrap_or_else(|_| "solard=info,tower_http=debug".into()), )) .with(tracing_subscriber::fmt::layer()) .init(); @@ -63,17 +63,32 @@ async fn main() { .route("/health", get(health_check)) .route("/planets/list", get(handlers::planets::list)) .route("/planets/new", post(handlers::planets::new_planet)) + .route_layer(middleware::from_fn(handlers::auth::requires_auth)) .route("/planets/:uuid", get(handlers::planets::get)) .route("/planets/:uuid/shutdown", post(handlers::planets::shutdown)) - .route("/planets/:uuid/shutdown/hard", post(handlers::planets::force_shutdown)) + .route_layer(middleware::from_fn(handlers::auth::requires_auth)) + .route( + "/planets/:uuid/shutdown/hard", + post(handlers::planets::force_shutdown), + ) + .route_layer(middleware::from_fn(handlers::auth::requires_auth)) .route("/planets/:uuid/start", post(handlers::planets::start)) + .route_layer(middleware::from_fn(handlers::auth::requires_auth)) .route("/planets/:uuid/pause", post(handlers::planets::pause)) + .route_layer(middleware::from_fn(handlers::auth::requires_auth)) .route("/planets/:uuid/reboot", post(handlers::planets::reboot)) - .route("/planets/:uuid/reboot/hard", post(handlers::planets::force_reboot)) + .route_layer(middleware::from_fn(handlers::auth::requires_auth)) + .route( + "/planets/:uuid/reboot/hard", + post(handlers::planets::force_reboot), + ) + .route_layer(middleware::from_fn(handlers::auth::requires_auth)) .route("/planets/:uuid/destroy", post(handlers::planets::no_planet)) + .route_layer(middleware::from_fn(handlers::auth::requires_auth)) // Authentication .route("/auth/begin", post(handlers::auth::begin)) - .layer( ServiceBuilder::new() + .layer( + ServiceBuilder::new() .layer(HandleErrorLayer::new(|error: BoxError| async move { if error.is::() { Ok(StatusCode::REQUEST_TIMEOUT) @@ -106,7 +121,7 @@ async fn health_check() -> &'static str { fn get_star() -> Result { let con_url = std::env::var("QEMU_URL").unwrap_or("qemu:///system".to_string()); - + Ok(Star::new(con_url)?) } -- cgit v1.2.3