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

66 lines
1.6 KiB
Rust
Raw Normal View History

2023-10-05 12:09:05 -04:00
use std::net::SocketAddr;
use axum::{
Json,
Router, routing::get,
extract::Form
};
use serde::{Deserialize, Serialize};
use axum::response::{Html, Redirect};
use axum::routing::post;
2024-05-11 13:41:00 -04:00
use kawari::config::{Config, get_config};
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
Html(format!("<p>Gate open:{}</p><form action='apply' method='post'><input type='checkbox' id='gate_open' name='gate_open' checked /><button type='submit'>Apply</button></form>", config.gate_open))
2023-10-05 12:09:05 -04:00
}
#[derive(Deserialize, Debug)]
#[allow(dead_code)]
struct Input {
gate_open: Option<String>,
}
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
if let Some(gate_open) = input.gate_open {
config.gate_open = gate_open == "on";
} else {
config.gate_open = false;
2023-10-05 12:09:05 -04: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();
}