aboutsummaryrefslogtreecommitdiff
path: root/src/main.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/main.rs')
-rw-r--r--src/main.rs37
1 files changed, 26 insertions, 11 deletions
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::<tower::timeout::error::Elapsed>() {
Ok(StatusCode::REQUEST_TIMEOUT)
@@ -106,7 +121,7 @@ async fn health_check() -> &'static str {
fn get_star() -> Result<Star, errors::ServiceError> {
let con_url = std::env::var("QEMU_URL").unwrap_or("qemu:///system".to_string());
-
+
Ok(Star::new(con_url)?)
}