diff --git a/service/server.go b/service/server.go index 6835ffc6..fffe29e7 100644 --- a/service/server.go +++ b/service/server.go @@ -49,7 +49,7 @@ func Init(config *config.Config) *Server { server.initEcho() server.initMiddleware() - server.initUi() + server.initUI() server.initApis() return server @@ -94,22 +94,27 @@ func (s *Server) initMiddleware() { })) } -func (s *Server) initUi() { - loadUi := s.config.Env == "production" - defer logStatus("UI", loadUi) +func (s *Server) initUI() { + loadUI := s.config.Env == "production" + defer logStatus("UI", loadUI) - if !loadUi { + if !loadUI { return } + // Remove LocateAppend and LocateFS because we don't use it and it cause tests issue + riceConfig := rice.Config{ + LocateOrder: []rice.LocateMethod{rice.LocateEmbedded}, + } // Never use constant or variable according to docs : https://github.com/GeertJohan/go.rice#calling-findbox-and-mustfindbox - uiAssets, err := rice.FindBox("../ui/dist") + uiAssets, err := riceConfig.FindBox("../ui/dist") if err != nil { panic("static ui/dist not found. GetBuildStatus them with `cd ui && yarn build` first.") } assetHandler := http.FileServer(uiAssets.HTTPBox()) s.GET("/", echo.WrapHandler(assetHandler)) + s.GET("/favicon*", echo.WrapHandler(assetHandler)) s.GET("/css/*", echo.WrapHandler(http.StripPrefix("/", assetHandler))) s.GET("/js/*", echo.WrapHandler(http.StripPrefix("/", assetHandler))) s.GET("/fonts/*", echo.WrapHandler(http.StripPrefix("/", assetHandler))) diff --git a/service/server_test.go b/service/server_test.go index 292bbaac..832908aa 100644 --- a/service/server_test.go +++ b/service/server_test.go @@ -3,18 +3,49 @@ package service import ( "testing" - . "github.com/monitoror/monitoror/config" - + "github.com/GeertJohan/go.rice/embedded" + "github.com/monitoror/monitoror/config" "github.com/stretchr/testify/assert" ) func TestInit(t *testing.T) { - conf := InitConfig() + conf := config.InitConfig() conf.Env = "test" server := Init(conf) assert.NotNil(t, server.Echo) } +func TestInitFront_Panic(t *testing.T) { + conf := config.InitConfig() + conf.Env = "production" + + server := &Server{ + config: conf, + } + server.initEcho() + + assert.Panics(t, func() { + server.initUI() + }) +} + +func TestInitFront_Success(t *testing.T) { + conf := config.InitConfig() + conf.Env = "production" + + server := &Server{ + config: conf, + } + server.initEcho() + + embedded.RegisterEmbeddedBox("../ui/dist", &embedded.EmbeddedBox{ + Name: "../ui/dist", + }) + defer delete(embedded.EmbeddedBoxes, "../ui/dist") + + server.initUI() +} + func TestRegisterTile(t *testing.T) { registerTile(func(s string) { assert.False(t, true) }, "TEST", false) registerTile(func(s string) { assert.True(t, true) }, "TEST", true)