82 lines
1.5 KiB
Go
82 lines
1.5 KiB
Go
package frontend
|
|
|
|
import (
|
|
"context"
|
|
"embed"
|
|
"io"
|
|
"io/fs"
|
|
"net/http"
|
|
"sync"
|
|
|
|
"dynatron.me/x/blasphem/pkg/blas"
|
|
"dynatron.me/x/blasphem/pkg/blas/core"
|
|
"dynatron.me/x/blasphem/pkg/components"
|
|
|
|
"github.com/labstack/echo/v4"
|
|
)
|
|
|
|
const FrontendKey = "frontend"
|
|
|
|
//go:embed frontend/hass_frontend
|
|
var root embed.FS
|
|
|
|
type Frontend struct {
|
|
fsHandler echo.HandlerFunc
|
|
rootFS fs.FS
|
|
|
|
routeInstall sync.Once
|
|
}
|
|
|
|
func (fe *Frontend) InstallRoutes(e *echo.Echo) {
|
|
fe.routeInstall.Do(func() {
|
|
e.GET("/*", fe.fsHandler)
|
|
e.GET("/auth/authorize", fe.AliasHandler("authorize.html"))
|
|
})
|
|
}
|
|
|
|
func (fe *Frontend) AliasHandler(toFile string) echo.HandlerFunc {
|
|
return func(c echo.Context) error {
|
|
file, err := fe.rootFS.Open(toFile)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer file.Close()
|
|
|
|
b, err := io.ReadAll(file)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
return c.HTML(http.StatusOK, string(b))
|
|
}
|
|
}
|
|
|
|
func (*Frontend) Shutdown() {}
|
|
|
|
func newData(_ []string) interface{} {
|
|
return map[string]interface{}{}
|
|
}
|
|
|
|
func wsHand(ctx context.Context, wss core.WebSocketSession, msgID int, cmd []string, msg interface{}) error {
|
|
return nil
|
|
}
|
|
|
|
func Setup(b core.Blas) (components.Component, error) {
|
|
fe := &Frontend{}
|
|
var err error
|
|
|
|
fe.rootFS, err = fs.Sub(root, "frontend/hass_frontend")
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
fe.fsHandler = echo.StaticDirectoryHandler(fe.rootFS, false)
|
|
|
|
b.RegisterWSCommand("frontend", wsHand, newData)
|
|
|
|
return fe, nil
|
|
}
|
|
|
|
func init() {
|
|
blas.Register(FrontendKey, Setup)
|
|
}
|