aboutsummaryrefslogtreecommitdiff
path: root/src/main.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/main.rs')
-rw-r--r--src/main.rs21
1 files changed, 21 insertions, 0 deletions
diff --git a/src/main.rs b/src/main.rs
index 4dc78a6..3f889cd 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -7,6 +7,7 @@ use axum::{
Json, Router, Extension
};
+use rand::{thread_rng, Rng, distributions::Alphanumeric};
use serde::{Deserialize, Serialize};
use solarlib::star::Star;
use std::{net::SocketAddr, time::Duration, str::FromStr, sync::Arc};
@@ -20,8 +21,11 @@ mod errors;
mod handlers;
+#[derive(Clone)]
pub struct State {
pub hw_url: String,
+ pub secret_key: String,
+ pub gen_key: String,
}
#[tokio::main]
@@ -36,10 +40,25 @@ async fn main() {
.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))
@@ -52,6 +71,8 @@ async fn main() {
.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))
+ // 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>() {