Skip to content

armgit / kwant

Public Imported from GitHub
0

Your own team of AI bots in one self-hosted binary: spaces, every account you have, handoffs, approvals and routines. Open-source, self-hosted alternative to Grok Bot, Claude Duet and OpenAI dots. Rust + React.

feat: embed frontend assets into single kwnt executable

Oleksandr Ivanchenko3350750b7d94
diff --git a/Cargo.lock b/Cargo.lock
index 136af894..262db1db 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -1147,6 +1147,7 @@ dependencies = [
"clap",
"dirs",
"futures",
+ "mime_guess",
"reqwest",
"rust-embed",
"serde",
diff --git a/Cargo.toml b/Cargo.toml
index e4e9afd7..7dc68e1f 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -24,6 +24,7 @@ reqwest = { version = "0.12", features = ["json", "stream"] }
futures = "0.3"
tokio-stream = { version = "0.1", features = ["sync"] }
rust-embed = "8.5"
+mime_guess = "2.0"
clap = { version = "4.5", features = ["derive"] }
tracing = "0.1"
tracing-subscriber = { version = "0.3", features = ["env-filter"] }
diff --git a/Makefile b/Makefile
new file mode 100644
index 00000000..3a55b3fd
--- /dev/null
+++ b/Makefile
@@ -0,0 +1,19 @@
+.PHONY: all build frontend test run clean
+
+all: build
+
+frontend:
+ cd frontend && npm install && npm run build
+
+build: frontend
+ cargo build --release
+
+test:
+ cargo test
+
+run:
+ cargo run -- serve
+
+clean:
+ cargo clean
+ rm -rf frontend/dist
diff --git a/src/embedded.rs b/src/embedded.rs
new file mode 100644
index 00000000..4a2c44f0
--- /dev/null
+++ b/src/embedded.rs
@@ -0,0 +1,42 @@
+use axum::{
+ body::Body,
+ http::{header, Response, StatusCode, Uri},
+ response::IntoResponse,
+};
+use rust_embed::Embed;
+
+#[derive(Embed)]
+#[folder = "frontend/dist/"]
+pub struct Assets;
+
+pub async fn static_handler(uri: Uri) -> impl IntoResponse {
+ let mut path = uri.path().trim_start_matches('/').to_string();
+
+ if path.is_empty() {
+ path = "index.html".to_string();
+ }
+
+ match Assets::get(&path) {
+ Some(content) => {
+ let mime = mime_guess::from_path(&path).first_or_octet_stream();
+ Response::builder()
+ .header(header::CONTENT_TYPE, mime.as_ref())
+ .body(Body::from(content.data))
+ .unwrap()
+ }
+ None => {
+ // SPA fallback: return index.html for client-side routing
+ if let Some(index) = Assets::get("index.html") {
+ Response::builder()
+ .header(header::CONTENT_TYPE, "text/html")
+ .body(Body::from(index.data))
+ .unwrap()
+ } else {
+ Response::builder()
+ .status(StatusCode::NOT_FOUND)
+ .body(Body::from("404 Not Found"))
+ .unwrap()
+ }
+ }
+ }
+}
diff --git a/src/lib.rs b/src/lib.rs
index 06351a39..41a90e3a 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -2,6 +2,7 @@ pub mod broker;
pub mod config;
pub mod db;
pub mod drivers;
+pub mod embedded;
pub mod router;
pub mod server;
pub mod tools;
@@ -10,6 +11,7 @@ pub use broker::{ApprovalDecision, ApprovalRequest, PermissionBroker};
pub use config::{Config, ConfigError};
pub use db::{Database, DbError};
pub use drivers::{create_driver_for_kwant, ApiDriver, CliDriver, Driver, DriverError, RuntimeEvent, TurnInput, TurnOutput};
+pub use embedded::{static_handler, Assets};
pub use router::{LayaRouter, RouteDecision, RouterError};
pub use server::{create_router, AppState};
pub use tools::{ToolError, ToolRunner};
diff --git a/src/server/mod.rs b/src/server/mod.rs
index 6ad43a39..4ea548b9 100644
--- a/src/server/mod.rs
+++ b/src/server/mod.rs
@@ -58,6 +58,7 @@ pub fn create_router(state: AppState) -> Router {
.route("/api/chat/send", post(routes::send_chat))
.route("/api/approvals/:id/resolve", post(routes::resolve_approval))
.route("/api/events", get(sse::sse_handler))
+ .fallback(crate::embedded::static_handler)
.layer(cors)
.with_state(state)
}
diff --git a/tests/e2e_test.rs b/tests/e2e_test.rs
new file mode 100644
index 00000000..30b36f26
--- /dev/null
+++ b/tests/e2e_test.rs
@@ -0,0 +1,36 @@
+use axum::{
+ body::{to_bytes, Body},
+ http::{Request, StatusCode},
+};
+use kwnt::{
+ broker::PermissionBroker,
+ config::Config,
+ db::Database,
+ router::LayaRouter,
+ server::{create_router, AppState},
+ tools::ToolRunner,
+};
+use tempfile::tempdir;
+use tower::ServiceExt;
+
+#[tokio::test]
+async fn test_embedded_assets_served() {
+ let tmp = tempdir().unwrap();
+ let db = Database::init(tmp.path()).await.unwrap();
+ let config = Config::default();
+ let tools = ToolRunner::new(tmp.path().join("workspace"));
+ let broker = PermissionBroker::new();
+ let router = LayaRouter::new(config.laya_url.clone());
+
+ let state = AppState::new(config, db, tools, broker, router);
+ let app = create_router(state);
+
+ // Test GET / returns index.html
+ let req = Request::builder().uri("/").body(Body::empty()).unwrap();
+ let resp = app.clone().oneshot(req).await.unwrap();
+ assert_eq!(resp.status(), StatusCode::OK);
+
+ let body_bytes = to_bytes(resp.into_body(), usize::MAX).await.unwrap();
+ let html = String::from_utf8_lossy(&body_bytes);
+ assert!(html.contains("KWNTS") || html.contains("root"));
+}