Skip to content

Commit

Permalink
Merge pull request #147 from monitoror/fix-missing-favicon
Browse files Browse the repository at this point in the history
fix(back): add route to expose favicon from rice bundle
  • Loading branch information
jsdidierlaurent committed Feb 6, 2020
2 parents 7bd1328 + a4cdf06 commit 7f95988
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 9 deletions.
17 changes: 11 additions & 6 deletions service/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func Init(config *config.Config) *Server {

server.initEcho()
server.initMiddleware()
server.initUi()
server.initUI()
server.initApis()

return server
Expand Down Expand Up @@ -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)))
Expand Down
37 changes: 34 additions & 3 deletions service/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 7f95988

Please sign in to comment.