From 88a2ba4297e8a1dc276589de35cb51c6c2986d6f Mon Sep 17 00:00:00 2001 From: Tom Wiesing Date: Thu, 6 Oct 2022 15:41:46 +0200 Subject: [PATCH] component/static: Seperate out static file handling --- internal/component/control/extras_info.go | 23 ++----------- internal/component/control/html/index.html | 6 ++-- internal/component/control/html/instance.html | 8 ++--- .../out/control}/autolink.css | 0 .../static => static/out/control}/autolink.js | 0 .../static => static/out/control}/dis.css | 0 .../static => static/out/control}/instance.js | 0 internal/component/static/static.go | 32 +++++++++++++++++++ internal/dis/component.go | 2 ++ 9 files changed, 43 insertions(+), 28 deletions(-) rename internal/component/{control/html/static => static/out/control}/autolink.css (100%) rename internal/component/{control/html/static => static/out/control}/autolink.js (100%) rename internal/component/{control/html/static => static/out/control}/dis.css (100%) rename internal/component/{control/html/static => static/out/control}/instance.js (100%) create mode 100644 internal/component/static/static.go diff --git a/internal/component/control/extras_info.go b/internal/component/control/extras_info.go index 297fbe2..0203ffd 100644 --- a/internal/component/control/extras_info.go +++ b/internal/component/control/extras_info.go @@ -2,13 +2,13 @@ package control import ( "context" - "embed" "html/template" - "io/fs" "net/http" "strings" "time" + _ "embed" + "github.com/FAU-CDI/wisski-distillery/internal/component" "github.com/FAU-CDI/wisski-distillery/internal/component/instances" "github.com/FAU-CDI/wisski-distillery/internal/config" @@ -40,13 +40,6 @@ func (info *Info) Handler(route string, context context.Context, io stream.IOStr http.NotFound(w, r) }) - // static stuff - static, err := info.disStatic() - if err != nil { - return nil, err - } - mux.Handle("/dis/static/", static) - // render everything mux.Handle("/dis/index", httpx.HTMLHandler[disIndex]{ Handler: info.disIndex, @@ -159,18 +152,6 @@ func (info *Info) disInstance(r *http.Request) (is disInstance, err error) { return } -//go:embed html/static -var htmlStaticFS embed.FS - -func (*Info) disStatic() (http.Handler, error) { - fs, err := fs.Sub(htmlStaticFS, "html/static") - if err != nil { - return nil, err - } - - return http.StripPrefix("/dis/static/", http.FileServer(http.FS(fs))), nil -} - //go:embed "html/index.html" var indexTemplateStr string var indexTemplate = template.Must(template.New("index.html").Parse(indexTemplateStr)) diff --git a/internal/component/control/html/index.html b/internal/component/control/html/index.html index 7e52a1a..b756697 100644 --- a/internal/component/control/html/index.html +++ b/internal/component/control/html/index.html @@ -1,6 +1,6 @@ - - + + Distillery Status Page

Distillery Status Page

@@ -77,4 +77,4 @@ Generated at {{ .Time }} - \ No newline at end of file + \ No newline at end of file diff --git a/internal/component/control/html/instance.html b/internal/component/control/html/instance.html index 6ef2b82..c62f53f 100644 --- a/internal/component/control/html/instance.html +++ b/internal/component/control/html/instance.html @@ -1,6 +1,6 @@ - - + + Distillery Status Page - {{ .Info.Slug }}

Distillery Status Page - {{ .Info.Slug }}

@@ -70,5 +70,5 @@ Generated at {{ .Time }} - - \ No newline at end of file + + \ No newline at end of file diff --git a/internal/component/control/html/static/autolink.css b/internal/component/static/out/control/autolink.css similarity index 100% rename from internal/component/control/html/static/autolink.css rename to internal/component/static/out/control/autolink.css diff --git a/internal/component/control/html/static/autolink.js b/internal/component/static/out/control/autolink.js similarity index 100% rename from internal/component/control/html/static/autolink.js rename to internal/component/static/out/control/autolink.js diff --git a/internal/component/control/html/static/dis.css b/internal/component/static/out/control/dis.css similarity index 100% rename from internal/component/control/html/static/dis.css rename to internal/component/static/out/control/dis.css diff --git a/internal/component/control/html/static/instance.js b/internal/component/static/out/control/instance.js similarity index 100% rename from internal/component/control/html/static/instance.js rename to internal/component/static/out/control/instance.js diff --git a/internal/component/static/static.go b/internal/component/static/static.go new file mode 100644 index 0000000..6e91b11 --- /dev/null +++ b/internal/component/static/static.go @@ -0,0 +1,32 @@ +// Package static implements serving of fully static resources +package static + +import ( + "context" + "embed" + "io/fs" + "net/http" + + "github.com/FAU-CDI/wisski-distillery/internal/component" + "github.com/tkw1536/goprogram/stream" +) + +type Static struct { + component.ComponentBase +} + +func (*Static) Name() string { return "static" } + +func (*Static) Routes() []string { return []string{"/static/"} } + +func (static *Static) Handler(route string, context context.Context, io stream.IOStream) (http.Handler, error) { + fs, err := fs.Sub(htmlStaticFS, "out") + if err != nil { + return nil, err + } + + return http.StripPrefix(route, http.FileServer(http.FS(fs))), nil +} + +//go:embed out +var htmlStaticFS embed.FS diff --git a/internal/dis/component.go b/internal/dis/component.go index 094ce71..751c7e9 100644 --- a/internal/dis/component.go +++ b/internal/dis/component.go @@ -11,6 +11,7 @@ import ( "github.com/FAU-CDI/wisski-distillery/internal/component/snapshots" "github.com/FAU-CDI/wisski-distillery/internal/component/sql" "github.com/FAU-CDI/wisski-distillery/internal/component/ssh" + "github.com/FAU-CDI/wisski-distillery/internal/component/static" "github.com/FAU-CDI/wisski-distillery/internal/component/triplestore" "github.com/FAU-CDI/wisski-distillery/internal/component/web" ) @@ -44,6 +45,7 @@ func (dis *Distillery) register(context *component.PoolContext) []component.Comp // Control server ra[*control.Control](dis, context), + ra[*static.Static](dis, context), r(dis, context, func(home *home.Home) { home.RefreshInterval = time.Minute }),