title: Embed Resources url: /cookbook/embed-resources menu: side: name: 资源嵌入 parent: cookbook

  1. weight: 17

资源嵌入

使用 go.rice

server.go

  1. package main
  2. import (
  3. "net/http"
  4. rice "github.com/GeertJohan/go.rice"
  5. "github.com/labstack/echo"
  6. )
  7. func main() {
  8. e := echo.New()
  9. // the file server for rice. "app" is the folder where the files come from.
  10. assetHandler := http.FileServer(rice.MustFindBox("app").HTTPBox())
  11. // serves the index.html from rice
  12. e.GET("/", echo.WrapHandler(assetHandler))
  13. // servers other static files
  14. e.GET("/static/*", echo.WrapHandler(http.StripPrefix("/static/", assetHandler)))
  15. e.Logger.Fatal(e.Start(":1323"))
  16. }