aboutsummaryrefslogtreecommitdiff
path: root/src/main.rs
blob: 97050e87ddc1ed1f31044c829fe86a6eac63eb37 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
use axum::{
    error_handling::HandleErrorLayer,
    handler::Handler,
    http::StatusCode,
    middleware,
    response::IntoResponse,
    routing::{get, post},
    Extension, Json, Router,
};

use rand::{distributions::Alphanumeric, thread_rng, Rng};
use serde::{Deserialize, Serialize};
use solarlib::star::Star;
use std::{net::SocketAddr, str::FromStr, sync::Arc, time::Duration};

use tower::{BoxError, ServiceBuilder};
use tower_http::trace::TraceLayer;

use tracing_subscriber::prelude::*;

mod errors;

mod handlers;

#[derive(Clone)]
pub struct State {
    pub hw_url: String,
    pub secret_key: String,
    pub gen_key: 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 rand_key: String = thread_rng()
        .sample_iter(&Alphanumeric)
        .take(30)
        .map(char::from)
        .collect();

    let shared_state = Arc::new(State {
        hw_url: std::env::var("HOMEWORLD_URL").expect("No Homeworld URL set"),
        secret_key: std::env::var("SECRET_KEY").unwrap_or("bad-key".to_string()),
        gen_key: rand_key,
    });

    if shared_state.secret_key == "bad-key" {
        tracing::warn!("No secret key set! This is a bad idea.");
        tracing::warn!("Using default of `bad-key`");
    }

    tracing::info!("Random Key: {}", shared_state.gen_key);

    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_layer(middleware::from_fn(handlers::auth::requires_auth))
        .route("/planets/:uuid", get(handlers::planets::get))
        .route("/planets/:uuid/shutdown", post(handlers::planets::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_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(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
}