From 759c37d76c88d711a455c67aeddfcccdb9696a6c Mon Sep 17 00:00:00 2001 From: Joakim Hulthe Date: Mon, 29 Dec 2025 19:25:33 +0100 Subject: [PATCH] Serve gui from server The server will serve all files in the ./www directory relative to itself. The ./www/pkg is populated when running wasm-pack build --release --target web from the /gui directory --- srv/src/main.rs | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/srv/src/main.rs b/srv/src/main.rs index 4a0d74b..6246bd8 100644 --- a/srv/src/main.rs +++ b/srv/src/main.rs @@ -1,15 +1,27 @@ #[macro_use] extern crate rocket; -use std::env; +use std::{env, path::Path}; use inkopslista_lib::{Item, ItemData}; -use rocket::{serde::json::Json, State}; +use rocket::{ + fs::{FileServer, NamedFile}, + serde::json::Json, + State, +}; use sqlite::ConnectionThreadSafe; +/// The directory where the web files for the gui is stored. +fn www_path() -> &'static Path { + Path::new("./www") +} + +/// Serve `index.html` from [`www_path`]. #[get("/")] -fn index() -> &'static str { - "hello world" +async fn index() -> NamedFile { + NamedFile::open(www_path().join("index.html")) + .await + .expect("index.html") } #[get("/api/list")] @@ -67,4 +79,5 @@ fn rocket() -> _ { rocket::build() .manage(db) .mount("/", routes![index, get_list, put_list, delete_item]) + .mount("/", FileServer::from(www_path())) }