component/static: Seperate out static file handling

This commit is contained in:
Tom Wiesing 2022-10-06 15:41:46 +02:00
parent 3d4db1744b
commit 88a2ba4297
No known key found for this signature in database
9 changed files with 43 additions and 28 deletions

View file

@ -2,13 +2,13 @@ package control
import ( import (
"context" "context"
"embed"
"html/template" "html/template"
"io/fs"
"net/http" "net/http"
"strings" "strings"
"time" "time"
_ "embed"
"github.com/FAU-CDI/wisski-distillery/internal/component" "github.com/FAU-CDI/wisski-distillery/internal/component"
"github.com/FAU-CDI/wisski-distillery/internal/component/instances" "github.com/FAU-CDI/wisski-distillery/internal/component/instances"
"github.com/FAU-CDI/wisski-distillery/internal/config" "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) http.NotFound(w, r)
}) })
// static stuff
static, err := info.disStatic()
if err != nil {
return nil, err
}
mux.Handle("/dis/static/", static)
// render everything // render everything
mux.Handle("/dis/index", httpx.HTMLHandler[disIndex]{ mux.Handle("/dis/index", httpx.HTMLHandler[disIndex]{
Handler: info.disIndex, Handler: info.disIndex,
@ -159,18 +152,6 @@ func (info *Info) disInstance(r *http.Request) (is disInstance, err error) {
return 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" //go:embed "html/index.html"
var indexTemplateStr string var indexTemplateStr string
var indexTemplate = template.Must(template.New("index.html").Parse(indexTemplateStr)) var indexTemplate = template.Must(template.New("index.html").Parse(indexTemplateStr))

View file

@ -1,6 +1,6 @@
<!DOCTYPE html> <!DOCTYPE html>
<link rel="stylesheet" href="/dis/static/dis.css"> <link rel="stylesheet" href="/static/control/dis.css">
<link rel="stylesheet" href="/dis/static/autolink.css"> <link rel="stylesheet" href="/static/control/autolink.css">
<title>Distillery Status Page</title> <title>Distillery Status Page</title>
<h1 id="top">Distillery Status Page</h1> <h1 id="top">Distillery Status Page</h1>
@ -77,4 +77,4 @@
Generated at <code>{{ .Time }}</code> Generated at <code>{{ .Time }}</code>
</footer> </footer>
<script src="/dis/static/autolink.js"></script> <script src="/static/control/autolink.js"></script>

View file

@ -1,6 +1,6 @@
<!DOCTYPE html> <!DOCTYPE html>
<link rel="stylesheet" href="/dis/static/dis.css"> <link rel="stylesheet" href="/static/control/dis.css">
<link rel="stylesheet" href="/dis/static/autolink.css"> <link rel="stylesheet" href="/static/control/autolink.css">
<title>Distillery Status Page - {{ .Info.Slug }}</title> <title>Distillery Status Page - {{ .Info.Slug }}</title>
<h1 id="top">Distillery Status Page - {{ .Info.Slug }}</h1> <h1 id="top">Distillery Status Page - {{ .Info.Slug }}</h1>
@ -70,5 +70,5 @@
Generated at <code>{{ .Time }}</code> Generated at <code>{{ .Time }}</code>
</footer> </footer>
<script src="/dis/static/autolink.js"></script> <script src="/static/control/autolink.js"></script>
<script src="/dis/static/instance.js"></script> <script src="/static/control/instance.js"></script>

View file

@ -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

View file

@ -11,6 +11,7 @@ import (
"github.com/FAU-CDI/wisski-distillery/internal/component/snapshots" "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/sql"
"github.com/FAU-CDI/wisski-distillery/internal/component/ssh" "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/triplestore"
"github.com/FAU-CDI/wisski-distillery/internal/component/web" "github.com/FAU-CDI/wisski-distillery/internal/component/web"
) )
@ -44,6 +45,7 @@ func (dis *Distillery) register(context *component.PoolContext) []component.Comp
// Control server // Control server
ra[*control.Control](dis, context), ra[*control.Control](dis, context),
ra[*static.Static](dis, context),
r(dis, context, func(home *home.Home) { r(dis, context, func(home *home.Home) {
home.RefreshInterval = time.Minute home.RefreshInterval = time.Minute
}), }),