Skip to content
Myr

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

8 files+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
1package main23import (4 "time"56 "github.com/jessevdk/go-assets"7)89var _Assetsbfa8d115ce0617d89507412d5393a462f8e9b003 = "<!doctype html>\n<body>\n <p>Can you see this? → {{.Bar}}</p>\n</body>\n"10var _Assets3737a75b5254ed1f6d588b40a3449721f9ea86c2 = "<!doctype html>\n<body>\n <p>Hello, {{.Foo}}</p>\n</body>\n"1112// Assets returns go-assets FileSystem13var 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
1package main23import (4 "html/template"5 "io/ioutil"6 "net/http"7 "strings"89 "github.com/gin-gonic/gin"10)1112func 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}3132func 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
modified:vendor/vendor.json+6 added
modified:.travis.yml+1 added-1 removed
modified:README.md+45 added
47 unmodified lines
48 - [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--)5556## Quick start
1336 unmodified lines
1393 log.Println("Server exiting")1394}1395```13961397### Build a single binary with templates13981399You can build a server into a single binary containing templates by using [go-assets][].14001401[go-assets]: https://github.com/jessevdk/go-assets14021403```go1404func main() {1405 r := gin.New()14061407 t, err := loadTemplate()1408 if err != nil {1409 panic(err)1410 }1411 r.SetHTMLTemplate(t)14121413 r.GET("/", func(c *gin.Context) {1414 c.HTML(http.StatusOK, "/html/index.tmpl",nil)1415 })1416 r.Run(":8080")1417}14181419// loadTemplate loads templates embedded by go-assets-builder1420func 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```14381439See a complete example in the `examples/assets-in-binary` directory.14401441## Testing14421443The `net/http/httptest` package is preferable way for HTTP testing.1444
48 unmodified lines