1
Fork 0
mirror of https://github.com/redstrate/Kawari.git synced 2025-04-27 17:07:46 +00:00
kawari/src/bin/kawari-admin.rs

76 lines
2 KiB
Rust
Raw Normal View History

2023-10-05 12:09:05 -04:00
use std::net::SocketAddr;
use axum::response::{Html, Redirect};
use axum::routing::post;
2025-03-08 13:27:41 -05:00
use axum::{Json, Router, extract::Form, routing::get};
2024-05-11 13:41:00 -04:00
use kawari::config::{Config, get_config};
use kawari::setup_default_environment;
2025-03-08 13:27:41 -05:00
use minijinja::{Environment, context};
use serde::{Deserialize, Serialize};
2023-10-05 12:09:05 -04:00
#[derive(Debug, Clone, Serialize, Deserialize)]
struct GateStatus {
status: i32,
}
async fn root() -> Html<String> {
tracing::info!("Requesting gate status...");
let config = get_config();
2023-10-05 12:09:05 -04:00
let environment = setup_default_environment();
let template = environment.get_template("admin.html").unwrap();
Html(template.render(context! { worlds_open => config.worlds_open, login_open => config.login_open, boot_patch_location => config.boot_patches_location }).unwrap())
2023-10-05 12:09:05 -04:00
}
#[derive(Deserialize, Debug)]
#[allow(dead_code)]
struct Input {
2024-06-29 14:06:44 -04:00
worlds_open: Option<String>,
login_open: Option<String>,
boot_patch_location: Option<String>,
2023-10-05 12:09:05 -04:00
}
async fn apply(Form(input): Form<Input>) -> Redirect {
tracing::info!("Apply config changes...");
let mut config = get_config();
2023-10-05 12:09:05 -04:00
2024-06-29 14:06:44 -04:00
if let Some(gate_open) = input.worlds_open {
config.worlds_open = gate_open == "on";
} else {
2024-06-29 14:06:44 -04:00
config.worlds_open = false;
}
if let Some(gate_open) = input.login_open {
config.login_open = gate_open == "on";
} else {
config.login_open = false;
2023-10-05 12:09:05 -04:00
}
if let Some(boot_patch_location) = input.boot_patch_location {
config.boot_patches_location = boot_patch_location;
}
2025-03-08 13:27:41 -05:00
serde_json::to_writer(&std::fs::File::create("config.json").unwrap(), &config)
.expect("TODO: panic message");
2023-10-05 12:09:05 -04:00
Redirect::to("/")
}
#[tokio::main]
async fn main() {
tracing_subscriber::fmt::init();
let app = Router::new()
.route("/", get(root))
.route("/apply", post(apply));
let addr = SocketAddr::from(([127, 0, 0, 1], 5800));
tracing::info!("Admin server started on {}", addr);
axum::Server::bind(&addr)
.serve(app.into_make_service())
.await
.unwrap();
2025-03-08 13:27:41 -05:00
}