gitstack

grindlemire/gothem-stack code browser

938 B Go 37 lines 2025-11-29 · 98bbe10 raw
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
package web

import (
	"embed"
	"io/fs"
	"net/http"

	"github.com/labstack/echo/v4"
	"github.com/pkg/errors"
)

//go:embed public/*
var public embed.FS

// RegisterStaticAssets will register the static css, js, and html assets in the public
// directory under the /dist url path in the echo server.
func RegisterStaticAssets(e *echo.Echo) error {
	// embed and register the static files (css, favicon, js, etc.)
	assets, err := fs.Sub(public, "public")
	if err != nil {
		return errors.Wrap(err, "processing public assets")
	}
	e.StaticFS("/dist", assets)

	// read favicon once at startup for efficiency
	favicon, err := public.ReadFile("public/favicon.ico")
	if err != nil {
		return errors.Wrap(err, "reading favicon")
	}

	// independently return the favicon because some robots like to pull from this path
	e.GET("/favicon.ico", func(c echo.Context) error {
		return c.Blob(http.StatusOK, "image/x-icon", favicon)
	})

	return nil
}