0

Is there a way to use the new go embed feature with echo? I would like to serve files with the echo static middleware from files embedded with go embed. https://tip.golang.org/pkg/embed/ https://echo.labstack.com/guide/static-files

jub0bs
  • 60,866
  • 25
  • 183
  • 186
Ahmed
  • 367
  • 2
  • 12

1 Answers1

1

This issue links to a gist from Mark Wolfe that demonstrates a pretty straight forward approach. The core of the approach is:

// content holds our static web server content.
//go:embed static/*
var content embed.FS

var contentHandler = echo.WrapHandler(http.FileServer(http.FS(content)))
// The embedded files will all be in the '/static' folder so need to rewrite the request (could also do this with fs.Sub)
var contentRewrite = middleware.Rewrite(map[string]string{"/*": "/static/$1"})

func SetupRoutes(...) {
   e.GET("/*", contentHandler, contentRewrite)
}
Brits
  • 14,829
  • 2
  • 18
  • 31
  • Thank you. I was already trying Mark Wolf's suggestion, I didn't succeed in the beginning. But your answer helped me try one more time, so when I build the executable via `` go build '' the templates folder is included but the assets folder is not included. – Ahmed Mar 26 '21 at 02:30