Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

serve Bootstrap using static embedded #48

Merged
merged 1 commit into from
Nov 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added jawsboot/assets/static/bootstrap.bundle.min.js.gz
Binary file not shown.
Binary file added jawsboot/assets/static/bootstrap.min.css.gz
Binary file not shown.
67 changes: 56 additions & 11 deletions jawsboot/jawsboot.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,63 @@
package jawsboot

import "github.com/linkdata/jaws"
import (
"embed"
"net/http"
"path"

const DefaultBootstrapVersion = "5.3.1"
const DefaultBootstrapCDN = "https://cdn.jsdelivr.net/npm/bootstrap"
"github.com/linkdata/jaws"
"github.com/linkdata/jaws/staticserve"
)

// SetupUsing adds URL's for the given Bootstrap version and CDN along with `extra` and calls Jaws.GenerateHeadHTML.
func SetupUsing(jw *jaws.Jaws, version, cdn string, extra ...string) error {
return jw.GenerateHeadHTML(append(extra,
cdn+"@"+version+"/dist/js/bootstrap.bundle.min.js",
cdn+"@"+version+"/dist/css/bootstrap.min.css")...)
// HandleFunc matches the signature of http.ServeMux.Handle(), but is called without
// method or parameters for the pattern. E.g. ("/static/filename.1234567.js").
type HandleFunc = func(uri string, handler http.Handler)

//go:embed assets
var assetsFS embed.FS

// Files returns the staticserve.StaticServe entries for the embedded Bootstrap JS and CSS.
func Files() (files []*staticserve.StaticServe, err error) {
err = staticserve.WalkDir(assetsFS, "assets/static", func(filename string, ss *staticserve.StaticServe) (err error) {
files = append(files, ss)
return
})
return
}

// GenerateHeadHTML calls jw.GenerateHeadHTML with URL's for the staticserve files
// prefixed with the given path prefix and any extra URL's you provide.
func GenerateHeadHTML(jw *jaws.Jaws, prefix string, files []*staticserve.StaticServe, extra ...string) (err error) {
var extraFiles []string
for _, ss := range files {
extraFiles = append(extraFiles, path.Join(prefix, ss.Name))
}
extraFiles = append(extraFiles, extra...)
return jw.GenerateHeadHTML(extraFiles...)
}

// SetupUsing sets up Jaws to serve the Bootstrap files from the prefix path,
// calling handleFn for each URI and staticserve.StaticServe.
// If handleFn is nil, http.DefaultServeMux.Handle is used instead.
// Any extra URL's given are passed to GenerateHeadHTML.
func SetupUsing(jw *jaws.Jaws, prefix string, handleFn HandleFunc, extra ...string) (err error) {
var files []*staticserve.StaticServe
if handleFn == nil {
handleFn = http.DefaultServeMux.Handle
}
if files, err = Files(); err == nil {
if err = GenerateHeadHTML(jw, prefix, files, extra...); err == nil {
for _, ss := range files {
handleFn(path.Join(prefix, ss.Name), ss)
}
handleFn(path.Join(prefix, "bootstrap.bundle.min.js.map"), http.NotFoundHandler())
handleFn(path.Join(prefix, "bootstrap.min.css.map"), http.NotFoundHandler())
}
}
return
}

// Setup calls SetupUsing with DefaultBootstrapVersion and DefaultBootstrapCDN and `extra`.
func Setup(jw *jaws.Jaws, extra ...string) error {
return SetupUsing(jw, DefaultBootstrapVersion, DefaultBootstrapCDN, extra...)
// Setup calls SetupUsing with a prefix of "/static".
func Setup(jw *jaws.Jaws, handleFn HandleFunc, extra ...string) (err error) {
return SetupUsing(jw, "/static", handleFn, extra...)
}
9 changes: 6 additions & 3 deletions jawsboot/jawsboot_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
func TestJawsBoot_Setup(t *testing.T) {
jw := jaws.New()
defer jw.Close()
if err := jawsboot.Setup(jw); err != nil {
if err := jawsboot.Setup(jw, nil, "/other/foobar.js"); err != nil {
t.Fatal(err)
}

Expand All @@ -25,10 +25,13 @@ func TestJawsBoot_Setup(t *testing.T) {
if !strings.Contains(txt, jaws.JavascriptPath) {
t.Error(txt)
}
if !strings.Contains(txt, jawsboot.DefaultBootstrapVersion) {
if !strings.Contains(txt, "/static/bootstrap.bundle.min") {
t.Error(txt)
}
if !strings.Contains(txt, jawsboot.DefaultBootstrapCDN) {
if !strings.Contains(txt, "/static/bootstrap.min") {
t.Error(txt)
}
if !strings.Contains(txt, "/other/foobar.js") {
t.Error(txt)
}
}
Loading