1
Fork 0
mirror of https://github.com/redstrate/Kawari.git synced 2025-04-21 07:07:44 +00:00

Add admin panel, config

This commit is contained in:
Joshua Goins 2023-10-05 12:09:05 -04:00
parent 96179f0ba0
commit 1625179a74
7 changed files with 264 additions and 147 deletions

View file

@ -13,3 +13,4 @@ tasks:
- upload: | - upload: |
echo "StrictHostKeyChecking=no" >> ~/.ssh/config echo "StrictHostKeyChecking=no" >> ~/.ssh/config
rsync -e 'ssh -p 38901' -Wvr kawari/target/release/kawari-frontier deploy@ryne.moe:/opt/kawari/ rsync -e 'ssh -p 38901' -Wvr kawari/target/release/kawari-frontier deploy@ryne.moe:/opt/kawari/
rsync -e 'ssh -p 38901' -Wvr kawari/target/release/kawari-admin deploy@ryne.moe:/opt/kawari/

22
Cargo.lock generated
View file

@ -52,6 +52,7 @@ dependencies = [
"serde", "serde",
"serde_json", "serde_json",
"serde_path_to_error", "serde_path_to_error",
"serde_urlencoded",
"sync_wrapper", "sync_wrapper",
"tokio", "tokio",
"tower", "tower",
@ -124,6 +125,15 @@ version = "1.0.7"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3f9eec918d3f24069decb9af1554cad7c880e2da24a9afd88aca000531ab82c1" checksum = "3f9eec918d3f24069decb9af1554cad7c880e2da24a9afd88aca000531ab82c1"
[[package]]
name = "form_urlencoded"
version = "1.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a62bc1cf6f830c2ec14a513a9fb124d0a213a629668a4186f329db21fe045652"
dependencies = [
"percent-encoding",
]
[[package]] [[package]]
name = "futures-channel" name = "futures-channel"
version = "0.3.28" version = "0.3.28"
@ -434,6 +444,18 @@ dependencies = [
"serde", "serde",
] ]
[[package]]
name = "serde_urlencoded"
version = "0.7.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d3491c14715ca2294c4d6a88f15e84739788c1d030eed8c110436aafdaa2f3fd"
dependencies = [
"form_urlencoded",
"itoa",
"ryu",
"serde",
]
[[package]] [[package]]
name = "sharded-slab" name = "sharded-slab"
version = "0.1.7" version = "0.1.7"

View file

@ -6,6 +6,9 @@ edition = "2021"
[[bin]] [[bin]]
name = "kawari-frontier" name = "kawari-frontier"
[[bin]]
name = "kawari-admin"
[profile.release] [profile.release]
lto = true lto = true
strip = true strip = true
@ -14,7 +17,7 @@ codegen-units = 1
panic = "abort" panic = "abort"
[dependencies] [dependencies]
axum = { version = "0.6.20", features = ["json", "tokio", "http1"], default-features = false } axum = { version = "0.6.20", features = ["json", "tokio", "http1", "form"], default-features = false }
serde_json = { version = "1.0.91", default-features = false } serde_json = { version = "1.0.91", default-features = false }
tokio = { version = "1.32.0", features = ["macros", "rt", "rt-multi-thread"], default-features = false } tokio = { version = "1.32.0", features = ["macros", "rt", "rt-multi-thread"], default-features = false }
tracing = { version = "0.1.37", default-features = false } tracing = { version = "0.1.37", default-features = false }

72
src/bin/kawari-admin.rs Normal file
View file

@ -0,0 +1,72 @@
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;
use kawari::config::Config;
#[derive(Debug, Clone, Serialize, Deserialize)]
struct GateStatus {
status: i32,
}
async fn root() -> Html<String> {
tracing::info!("Requesting gate status...");
// read config
if let Ok(data) = std::fs::read_to_string("config.json") {
let config: Config = serde_json::from_str(&data).expect("Failed to parse");
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))
} else {
Html(format!("unknown error"))
}
}
#[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...");
if let Ok(data) = std::fs::read_to_string("config.json") {
let mut config: Config = serde_json::from_str(&data).expect("Failed to parse");
if let Some(gate_open) = input.gate_open {
config.gate_open = gate_open == "on";
} else {
config.gate_open = false;
}
serde_json::to_writer(
&std::fs::File::create("config.json").unwrap(),
&config,
)
.expect("TODO: panic message");
}
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();
}

View file

@ -5,6 +5,7 @@ use axum::{
Router, routing::get, Router, routing::get,
}; };
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use kawari::config::Config;
#[derive(Debug, Clone, Serialize, Deserialize)] #[derive(Debug, Clone, Serialize, Deserialize)]
struct GateStatus { struct GateStatus {
@ -14,8 +15,19 @@ struct GateStatus {
async fn get_gate_status() -> Json<GateStatus> { async fn get_gate_status() -> Json<GateStatus> {
tracing::info!("Requesting gate status..."); tracing::info!("Requesting gate status...");
let mut is_open = 0;
// read config
if let Ok(data) = std::fs::read_to_string("config.json") {
let config: Config = serde_json::from_str(&data).expect("Failed to parse");
if config.gate_open {
is_open = 1;
}
}
Json(GateStatus { Json(GateStatus {
status: 1 status: is_open
}) })
} }

6
src/config.rs Normal file
View file

@ -0,0 +1,6 @@
use serde::{Deserialize, Serialize};
#[derive(Serialize, Deserialize)]
pub struct Config {
pub gate_open: bool
}

1
src/lib.rs Normal file
View file

@ -0,0 +1 @@
pub mod config;