use axum::{
error_handling::HandleErrorLayer,
http::StatusCode,
response::IntoResponse,
routing::{get, post},
handler::Handler,
Json, Router, Extension
};
use serde::{Deserialize, Serialize};
use solarlib::star::Star;
use std::{net::SocketAddr, time::Duration, str::FromStr, sync::Arc};
use tower::{BoxError, ServiceBuilder};
use tower_http::trace::TraceLayer;
use tracing_subscriber::prelude::*;
mod errors;
mod handlers;
pub struct State {
pub hw_url: String,
}
#[tokio::main]
async fn main() {
kankyo::init();
color_eyre::install().unwrap();
tracing_subscriber::registry()
.with(tracing_subscriber::EnvFilter::new(
std::env::var("RUST_LOG")
.unwrap_or_else(|_| "solard=info,tower_http=debug".into()),
))
.with(tracing_subscriber::fmt::layer())
.init();
let shared_state = Arc::new(State {
hw_url: std::env::var("HOMEWORLD_URL").expect("No Homeworld URL set"),
});
let app = Router::new()
.route("/health", get(health_check))
.route("/planets/list", get(handlers::planets::list))
.route("/planets/new", post(handlers::planets::new_planet))
.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("/planets/:uuid/start", post(handlers::planets::start))
.route("/planets/:uuid/pause", post(handlers::planets::pause))
.route("/planets/:uuid/reboot", post(handlers::planets::reboot))
.route("/planets/:uuid/reboot/hard", post(handlers::planets::force_reboot))
.route("/planets/:uuid/destroy", post(handlers::planets::no_planet))
.layer( ServiceBuilder::new()
.layer(HandleErrorLayer::new(|error: BoxError| async move {
if error.is::<tower::timeout::error::Elapsed>() {
Ok(StatusCode::REQUEST_TIMEOUT)
} else {
Err((
StatusCode::INTERNAL_SERVER_ERROR,
format!("Unhandled internal error: {}", error),
))
}
}))
.timeout(Duration::from_secs(10))
.layer(TraceLayer::new_for_http())
.into_inner(),
)
.layer(Extension(shared_state))
.fallback(handler_404.into_service());
let addr = SocketAddr::from_str(std::env::var("BIND_ADDR").unwrap().as_str().into()).unwrap();
tracing::info!("Listening on {}", addr);
axum::Server::bind(&addr)
.serve(app.into_make_service())
.await
.unwrap();
}
async fn health_check() -> &'static str {
"OK"
}
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)?)
}
async fn handler_404() -> impl IntoResponse {
StatusCode::NOT_FOUND
}