From bc981db565d96e19a20d817935f838610c3b0f10 Mon Sep 17 00:00:00 2001 From: Robert Iannucci Date: Mon, 25 Oct 2021 21:19:09 -0700 Subject: [PATCH] Use go:embed to serve static resources. --- goconvey.go | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/goconvey.go b/goconvey.go index 686b7195..ac746204 100644 --- a/goconvey.go +++ b/goconvey.go @@ -5,8 +5,10 @@ package main import ( + "embed" "flag" "fmt" + "io/fs" "log" "net" "net/http" @@ -54,8 +56,7 @@ func flags() { func folders() { _, file, _, _ := runtime.Caller(0) here := filepath.Dir(file) - static = filepath.Join(here, "/web/client") - reports = filepath.Join(static, "reports") + reports = filepath.Join(filepath.Join(here, "/web/client"), "reports") } func main() { @@ -186,8 +187,15 @@ func serveHTTP(server contract.Server, listener net.Listener) { activateServer(listener) } +//go:embed web/client +var static embed.FS + func serveStaticResources() { - http.Handle("/", http.FileServer(http.Dir(static))) + webclient, err := fs.Sub(static, "web/client") + if err != nil { + panic(err) + } + http.Handle("/", http.FileServer(http.FS(webclient))) } func serveAjaxMethods(server contract.Server) { @@ -303,7 +311,6 @@ var ( excludedDirs string autoLaunchBrowser bool - static string reports string quarterSecond = time.Millisecond * 250