Skip to content

Commit

Permalink
chore(ui): add reloading templates after changes in dev mode (#170)
Browse files Browse the repository at this point in the history
Signed-off-by: Jakob Steiner <jakob.steiner@glasskube.eu>
  • Loading branch information
kosmoz committed Mar 25, 2024
1 parent d08abc0 commit 09c09ac
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 11 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ require (
github.com/fatih/color v1.16.0
github.com/fluxcd/helm-controller/api v0.37.4
github.com/fluxcd/source-controller/api v1.2.4
github.com/fsnotify/fsnotify v1.7.0
github.com/go-logr/logr v1.4.1
github.com/google/go-containerregistry v0.19.1
github.com/gorilla/mux v1.8.1
Expand Down Expand Up @@ -38,7 +39,6 @@ require (
github.com/fluxcd/pkg/apis/acl v0.1.0 // indirect
github.com/fluxcd/pkg/apis/kustomize v1.3.0 // indirect
github.com/fluxcd/pkg/apis/meta v1.3.0 // indirect
github.com/fsnotify/fsnotify v1.7.0 // indirect
github.com/go-logr/zapr v1.3.0 // indirect
github.com/go-openapi/jsonpointer v0.20.2 // indirect
github.com/go-openapi/jsonreference v0.20.4 // indirect
Expand Down
22 changes: 20 additions & 2 deletions internal/web/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"strings"
"syscall"

"github.com/glasskube/glasskube/internal/config"
"github.com/glasskube/glasskube/internal/controller/owners"
"github.com/glasskube/glasskube/internal/dependency"
"github.com/glasskube/glasskube/internal/dependency/adapter/goclient"
Expand Down Expand Up @@ -49,7 +50,17 @@ import (

//go:embed root
//go:embed templates
var embededFs embed.FS
var embeddedFs embed.FS
var webFs fs.FS = embeddedFs

func init() {
if config.IsDevBuild() {
if _, err := os.Lstat(templatesBaseDir); err == nil {
fmt.Println("using DirFS")
webFs = os.DirFS(templatesBaseDir)
}
}
}

type ServerOptions struct {
Host string
Expand Down Expand Up @@ -135,7 +146,14 @@ func (s *server) Start(ctx context.Context) error {
s.startInformer(ctx)
}

root, err := fs.Sub(embededFs, "root")
if config.IsDevBuild() {
if err := watchTemplates(); err != nil {
fmt.Fprintf(os.Stderr, "templates will not be parsed after changes: %v\n", err)
}
}
parseTemplates()

root, err := fs.Sub(webFs, "root")
if err != nil {
return err
}
Expand Down
34 changes: 26 additions & 8 deletions internal/web/templates.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,14 @@ import (
"os"
"path"

"github.com/glasskube/glasskube/internal/web/components/alert"

"github.com/fsnotify/fsnotify"
"github.com/glasskube/glasskube/api/v1alpha1"

"github.com/glasskube/glasskube/internal/repo"

"github.com/glasskube/glasskube/internal/web/components/alert"
"github.com/glasskube/glasskube/internal/web/components/pkg_detail_btns"
"github.com/glasskube/glasskube/internal/web/components/pkg_overview_btn"
"github.com/glasskube/glasskube/internal/web/components/pkg_update_alert"
"go.uber.org/multierr"
)

var (
Expand All @@ -30,12 +29,31 @@ var (
pkgUpdateModalTmpl *template.Template
pkgUpdateAlertTmpl *template.Template
alertTmpl *template.Template
templatesBaseDir = "internal/web"
templatesDir = "templates"
componentsDir = path.Join(templatesDir, "components")
pagesDir = path.Join(templatesDir, "pages")
)

func init() {
func watchTemplates() error {
watcher, err := fsnotify.NewWatcher()
err = multierr.Combine(
err,
watcher.Add(path.Join(templatesBaseDir, componentsDir)),
watcher.Add(path.Join(templatesBaseDir, templatesDir, "layout")),
watcher.Add(path.Join(templatesBaseDir, pagesDir)),
)
if err == nil {
go func() {
for range watcher.Events {
parseTemplates()
}
}()
}
return err
}

func parseTemplates() {
templateFuncs := template.FuncMap{
"ForPkgOverviewBtn": pkg_overview_btn.ForPkgOverviewBtn,
"ForPkgDetailBtns": pkg_detail_btns.ForPkgDetailBtns,
Expand All @@ -57,7 +75,7 @@ func init() {
}
baseTemplate = template.Must(template.New("base.html").
Funcs(templateFuncs).
ParseFS(embededFs, path.Join(templatesDir, "layout", "base.html")))
ParseFS(webFs, path.Join(templatesDir, "layout", "base.html")))
pkgsPageTmpl = pageTmpl("packages.html")
pkgPageTmpl = pageTmpl("package.html")
supportPageTmpl = pageTmpl("support.html")
Expand All @@ -74,15 +92,15 @@ func init() {
func pageTmpl(fileName string) *template.Template {
return template.Must(
template.Must(baseTemplate.Clone()).ParseFS(
embededFs,
webFs,
path.Join(pagesDir, fileName),
path.Join(componentsDir, "*.html")))
}

func componentTmpl(id string, fileName string) *template.Template {
return template.Must(
template.New(id).ParseFS(
embededFs,
webFs,
path.Join(componentsDir, fileName)))
}

Expand Down

0 comments on commit 09c09ac

Please sign in to comment.