diff --git a/recipes/rice/.gitignore b/recipes/rice/.gitignore new file mode 100644 index 00000000..9524d94f --- /dev/null +++ b/recipes/rice/.gitignore @@ -0,0 +1,2 @@ +rice +app.rice-box.go diff --git a/recipes/rice/app/index.html b/recipes/rice/app/index.html new file mode 100644 index 00000000..66aac446 --- /dev/null +++ b/recipes/rice/app/index.html @@ -0,0 +1,11 @@ + + + + + go.rice Example + + + +

go.rice Example

+ + diff --git a/recipes/rice/app/main.js b/recipes/rice/app/main.js new file mode 100644 index 00000000..f888dc5c --- /dev/null +++ b/recipes/rice/app/main.js @@ -0,0 +1 @@ +alert("main.js"); diff --git a/recipes/rice/server.go b/recipes/rice/server.go new file mode 100644 index 00000000..7dcc4b8e --- /dev/null +++ b/recipes/rice/server.go @@ -0,0 +1,26 @@ +package main + +import ( + "net/http" + + "github.com/GeertJohan/go.rice" + "github.com/labstack/echo" +) + +func main() { + e := echo.New() + // the file server for rice. "app" is the folder where the files come from. + assetHandler := http.FileServer(rice.MustFindBox("app").HTTPBox()) + // serves the index.html from rice + e.Get("/", func(c *echo.Context) error { + assetHandler.ServeHTTP(c.Response().Writer(), c.Request()) + return nil + }) + // servers other static files + e.Get("/static/*", func(c *echo.Context) error { + http.StripPrefix("/static/", assetHandler). + ServeHTTP(c.Response().Writer(), c.Request()) + return nil + }) + e.Run(":3000") +} diff --git a/website/content/recipes/rice.md b/website/content/recipes/rice.md new file mode 100644 index 00000000..5c7e65ba --- /dev/null +++ b/website/content/recipes/rice.md @@ -0,0 +1,27 @@ +--- +title: go.rice integration +menu: + side: + identifier: "recipe-rice" + parent: recipes + weight: 14 +--- + +[go.rice](https://github.com/GeertJohan/go.rice) is a library that can be used +to package the assets (js, css, etc) inside the binary file, so your app +can still be a single binary. + +This folder contains a simple example serving an `index.html` file and a simple +`.js` file with go.rice. + +### Server + +`server.go` + +{{< embed "rice/server.go" >}} + +### Maintainers + +- [vishr](https://github.com/caarlos0) + +### [Source Code](https://github.com/labstack/echo/blob/master/recipes/rice)