Skip to content

Commit

Permalink
switch to go embed
Browse files Browse the repository at this point in the history
  • Loading branch information
equinox0815 committed Dec 22, 2023
1 parent ee94da4 commit 351599b
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 401 deletions.
4 changes: 2 additions & 2 deletions cmd/whawty-nginx-sso/web.go
Original file line number Diff line number Diff line change
Expand Up @@ -301,15 +301,15 @@ func runWeb(config *WebConfig, prom *MetricsHandler, cookies *cookie.Store, auth
return
}
} else {
htmlTmplLoader = pongo2.MustNewHttpFileSystemLoader(ui.Assets, "")
htmlTmplLoader = pongo2.NewFSLoader(ui.Assets)
}
r.HTMLRender = pongo2gin.New(pongo2gin.RenderOptions{
TemplateSet: pongo2.NewSet("html", htmlTmplLoader),
ContentType: "text/html; charset=utf-8"})

h := &HandlerContext{conf: config, cookies: cookies, auth: auth}
r.GET("/", func(c *gin.Context) { c.Redirect(http.StatusSeeOther, path.Join(h.getBasePath(c), "login")) })
r.StaticFS("/ui/", ui.StaticAssets)
r.StaticFS("/ui/", http.FS(ui.StaticAssets))
prom.install(r)
g := r.Group("/")
if reg := prom.reg(); reg != nil {
Expand Down
7 changes: 4 additions & 3 deletions ui/assets_dev.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
//go:build dev
// +build dev

//
// Copyright (c) 2023 whawty contributors (see AUTHORS file)
Expand Down Expand Up @@ -34,7 +33,9 @@
package ui

import (
"net/http"
"io/fs"
"os"
)

var Assets http.FileSystem = http.Dir("ui/assets")
var Assets fs.FS = os.DirFS("ui/assets")
var StaticAssets fs.FS = &filteredFilesystem{base: Assets}
30 changes: 15 additions & 15 deletions ui/generate_vfsdata.go → ui/assets_embed.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
//go:build ignore
// +build ignore
//go:build !dev

//
// Copyright (c) 2023 whawty contributors (see AUTHORS file)
Expand Down Expand Up @@ -31,23 +30,24 @@
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//

package main
package ui

import (
"log"
"net/http"

"github.com/shurcooL/vfsgen"
"embed"
"io/fs"
)

func main() {
assets := http.Dir("assets")
err := vfsgen.Generate(assets, vfsgen.Options{
PackageName: "ui",
BuildTags: "!dev",
VariableName: "Assets",
})
//go:embed assets
var embeddedAssets embed.FS

var Assets fs.FS
var StaticAssets fs.FS

func init() {
var err error
Assets, err = fs.Sub(embeddedAssets, "assets")
if err != nil {
log.Fatalln(err)
panic(err)
}
StaticAssets = &filteredFilesystem{base: Assets}
}
375 changes: 0 additions & 375 deletions ui/assets_vfsdata.go

This file was deleted.

10 changes: 4 additions & 6 deletions ui/ui.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
//go:generate go run generate_vfsdata.go
//
// Copyright (c) 2023 whawty contributors (see AUTHORS file)
// All rights reserved.
Expand Down Expand Up @@ -34,7 +33,7 @@ package ui
import (
"errors"
"fmt"
"net/http"
"io/fs"
"os"
"path"
"time"
Expand All @@ -53,17 +52,16 @@ func init() {
}

type filteredFilesystem struct {
base http.FileSystem
base fs.FS
}

func (fs *filteredFilesystem) Open(name string) (http.File, error) {
func (f *filteredFilesystem) Open(name string) (fs.File, error) {
if path.Ext(name) == ".htmpl" {
return nil, &os.PathError{Op: "open", Path: name, Err: os.ErrNotExist}
}
return fs.base.Open(name)
return f.base.Open(name)
}

var StaticAssets http.FileSystem = &filteredFilesystem{base: Assets}

type AlertLevel string

Expand Down

0 comments on commit 351599b

Please sign in to comment.