Add example to build single binary with templates (#1328)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by JINNOUCHI Yasushi · committed by Bo-Yi Wu · parent dfe37ea · Browse files · 8 files·+175 added−1 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
8 files+175 added−1 removed
- other:+175 added−1 removed
Provenance
- Commit
814ac94- Tree
238712e- Author
JINNOUCHI Yasushi <delphinus@remora.cx>- Committer
Bo-Yi Wu <appleboy.tw@gmail.com>- Parent
dfe37ea
8 files changed+175 added−1 removed
added:examples/assets-in-binary/html/bar.tmpl+4 added
1+<!doctype html>2+<body>3+ <p>Can you see this? → {{.Bar}}</p>4+</body>added:examples/assets-in-binary/html/index.tmpl+4 added
1+<!doctype html>2+<body>3+ <p>Hello, {{.Foo}}</p>4+</body>added:examples/assets-in-binary/assets.go+34 added
1+package main2+3+import (4+ "time"5+6+ "github.com/jessevdk/go-assets"7+)8+9+var _Assetsbfa8d115ce0617d89507412d5393a462f8e9b003 = "<!doctype html>\n<body>\n <p>Can you see this? → {{.Bar}}</p>\n</body>\n"10+var _Assets3737a75b5254ed1f6d588b40a3449721f9ea86c2 = "<!doctype html>\n<body>\n <p>Hello, {{.Foo}}</p>\n</body>\n"11+12+// Assets returns go-assets FileSystem13+var Assets = assets.NewFileSystem(map[string][]string{"/": {"html"}, "/html": {"bar.tmpl", "index.tmpl"}}, map[string]*assets.File{14+ "/": {15+ Path: "/",16+ FileMode: 0x800001ed,17+ Mtime: time.Unix(1524365738, 1524365738517125470),18+ Data: nil,19+ }, "/html": {20+ Path: "/html",21+ FileMode: 0x800001ed,22+ Mtime: time.Unix(1524365491, 1524365491289799093),23+ Data: nil,24+ }, "/html/bar.tmpl": {25+ Path: "/html/bar.tmpl",26+ FileMode: 0x1a4,27+ Mtime: time.Unix(1524365491, 1524365491289611557),28+ Data: []byte(_Assetsbfa8d115ce0617d89507412d5393a462f8e9b003),29+ }, "/html/index.tmpl": {30+ Path: "/html/index.tmpl",31+ FileMode: 0x1a4,32+ Mtime: time.Unix(1524365491, 1524365491289995821),33+ Data: []byte(_Assets3737a75b5254ed1f6d588b40a3449721f9ea86c2),34+ }}, "")added:examples/assets-in-binary/main.go+48 added
1+package main2+3+import (4+ "html/template"5+ "io/ioutil"6+ "net/http"7+ "strings"8+9+ "github.com/gin-gonic/gin"10+)11+12+func main() {13+ r := gin.New()14+ t, err := loadTemplate()15+ if err != nil {16+ panic(err)17+ }18+ r.SetHTMLTemplate(t)19+ r.GET("/", func(c *gin.Context) {20+ c.HTML(http.StatusOK, "/html/index.tmpl", gin.H{21+ "Foo": "World",22+ })23+ })24+ r.GET("/bar", func(c *gin.Context) {25+ c.HTML(http.StatusOK, "/html/bar.tmpl", gin.H{26+ "Bar": "World",27+ })28+ })29+ r.Run(":8080")30+}31+32+func loadTemplate() (*template.Template, error) {33+ t := template.New("")34+ for name, file := range Assets.Files {35+ if file.IsDir() || !strings.HasSuffix(name, ".tmpl") {36+ continue37+ }38+ h, err := ioutil.ReadAll(file)39+ if err != nil {40+ return nil, err41+ }42+ t, err = t.New(name).Parse(string(h))43+ if err != nil {44+ return nil, err45+ }46+ }47+ return t, nil48+}added:examples/assets-in-binary/README.md+33 added
123456789101112131415161718192021222324252627282930313233# Building a single binary containing templates
This is a complete example to create a single binary with the[gin-gonic/gin][gin] Web Server with HTML templates.
[gin]: https://github.com/gin-gonic/gin
## How to use
### Prepare Packages
```go get github.com/gin-gonic/gingo get github.com/jessevdk/go-assets-builder```
### Generate assets.go
```go-assets-builder html -o assets.go```
### Build the server
```go build -o assets-in-binary```
### Run
```./assets-in-binary```modified:vendor/vendor.json+6 added
32 unmodified lines333435363738394041424344454687 unmodified lines32 unmodified lines "revision": "5a0f697c9ed9d68fef0116532c6e05cfeae00e55", "revisionTime": "2017-06-01T23:02:30Z" }, { "checksumSHA1": "Cq9h7eDNXXyR/qJPvO8/Rk4pmFg=", "path": "github.com/jessevdk/go-assets", "revision": "4f4301a06e153ff90e17793577ab6bf79f8dc5c5", "revisionTime": "2016-09-21T14:41:39Z" }, { "checksumSHA1": "Ajh8TemnItg4nn+jKmVcsMRALBc=", "path": "github.com/json-iterator/go", "revision": "36b14963da70d11297d313183d7e6388c8510e1e", "revisionTime": "2017-08-29T15:58:51Z"87 unmodified linesmodified:.travis.yml+1 added-1 removed
7 unmodified lines89101112121314151619 unmodified lines7 unmodified lines - 1.10.x - master
git: depth: 3 depth: 10
install: - make install
19 unmodified linesmodified:README.md+45 added
47 unmodified lines48 - [Custom HTTP configuration](#custom-http-configuration)49 - [Support Let's Encrypt](#support-lets-encrypt)50 - [Run multiple service using Gin](#run-multiple-service-using-gin)51 - [Graceful restart or stop](#graceful-restart-or-stop)52+ - [Build a single binary with templates](#build-a-single-binary-with-templates)53 - [Testing](#testing)54 - [Users](#users--)55 56 ## Quick start1336 unmodified lines1393 log.Println("Server exiting")1394 }1395 ```1396 1397+### Build a single binary with templates1398+1399+You can build a server into a single binary containing templates by using [go-assets][].1400+1401+[go-assets]: https://github.com/jessevdk/go-assets1402+1403+```go1404+func main() {1405+ r := gin.New()1406+1407+ t, err := loadTemplate()1408+ if err != nil {1409+ panic(err)1410+ }1411+ r.SetHTMLTemplate(t)1412+1413+ r.GET("/", func(c *gin.Context) {1414+ c.HTML(http.StatusOK, "/html/index.tmpl",nil)1415+ })1416+ r.Run(":8080")1417+}1418+1419+// loadTemplate loads templates embedded by go-assets-builder1420+func loadTemplate() (*template.Template, error) {1421+ t := template.New("")1422+ for name, file := range Assets.Files {1423+ if file.IsDir() || !strings.HasSuffix(name, ".tmpl") {1424+ continue1425+ }1426+ h, err := ioutil.ReadAll(file)1427+ if err != nil {1428+ return nil, err1429+ }1430+ t, err = t.New(name).Parse(string(h))1431+ if err != nil {1432+ return nil, err1433+ }1434+ }1435+ return t, nil1436+}1437+```1438+1439+See a complete example in the `examples/assets-in-binary` directory.1440+1441 ## Testing1442 1443 The `net/http/httptest` package is preferable way for HTTP testing.1444 48 unmodified lines