mirror of
https://github.com/pomerium/pomerium.git
synced 2025-05-28 08:27:26 +02:00
internal/frontend : serve static assets (#392)
Signed-off-by: Bobby DeSimone <bobbydesimone@gmail.com>
This commit is contained in:
parent
f20d913abe
commit
ebee64b70b
24 changed files with 700 additions and 502 deletions
55
internal/frontend/templates.go
Normal file
55
internal/frontend/templates.go
Normal file
|
@ -0,0 +1,55 @@
|
|||
//go:generate statik -src=./assets -include=*.svg,*.html,*.css,*.js
|
||||
|
||||
package frontend // import "github.com/pomerium/pomerium/internal/frontend"
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"html/template"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"os"
|
||||
|
||||
"github.com/rakyll/statik/fs"
|
||||
|
||||
_ "github.com/pomerium/pomerium/internal/frontend/statik" // load static assets
|
||||
)
|
||||
|
||||
// NewTemplates loads pomerium's templates. Panics on failure.
|
||||
func NewTemplates() (*template.Template, error) {
|
||||
t := template.New("pomerium-templates")
|
||||
statikFS, err := fs.New()
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("internal/frontend: error creating new file system: %w", err)
|
||||
}
|
||||
|
||||
err = fs.Walk(statikFS, "/html", func(filePath string, fileInfo os.FileInfo, err error) error {
|
||||
if !fileInfo.IsDir() {
|
||||
file, err := statikFS.Open(filePath)
|
||||
if err != nil {
|
||||
return fmt.Errorf("internal/frontend: error opening %s: %w", filePath, err)
|
||||
}
|
||||
|
||||
buf, err := ioutil.ReadAll(file)
|
||||
if err != nil {
|
||||
return fmt.Errorf("internal/frontend: error reading %s: %w", filePath, err)
|
||||
}
|
||||
t.Parse(string(buf))
|
||||
}
|
||||
return nil
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return t, nil
|
||||
}
|
||||
|
||||
// MustAssetHandler wraps a call to the embedded static file system and panics
|
||||
// if the error is non-nil. It is intended for use in variable initializations
|
||||
func MustAssetHandler() http.Handler {
|
||||
statikFS, err := fs.New()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return http.FileServer(statikFS)
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue