Add example to build single binary with templates (#1328)
This commit is contained in:
		
				
					committed by
					
						
						Bo-Yi Wu
					
				
			
			
				
	
			
			
			
						parent
						
							dfe37ea6f1
						
					
				
				
					commit
					814ac9490a
				
			@ -9,7 +9,7 @@ go:
 | 
				
			|||||||
  - master
 | 
					  - master
 | 
				
			||||||
 | 
					
 | 
				
			||||||
git:
 | 
					git:
 | 
				
			||||||
  depth: 3
 | 
					  depth: 10
 | 
				
			||||||
 | 
					
 | 
				
			||||||
install:
 | 
					install:
 | 
				
			||||||
  - make install
 | 
					  - make install
 | 
				
			||||||
 | 
				
			|||||||
							
								
								
									
										45
									
								
								README.md
									
									
									
									
									
								
							
							
						
						
									
										45
									
								
								README.md
									
									
									
									
									
								
							@ -49,6 +49,7 @@ Gin is a web framework written in Go (Golang). It features a martini-like API wi
 | 
				
			|||||||
    - [Support Let's Encrypt](#support-lets-encrypt)
 | 
					    - [Support Let's Encrypt](#support-lets-encrypt)
 | 
				
			||||||
    - [Run multiple service using Gin](#run-multiple-service-using-gin)
 | 
					    - [Run multiple service using Gin](#run-multiple-service-using-gin)
 | 
				
			||||||
    - [Graceful restart or stop](#graceful-restart-or-stop)
 | 
					    - [Graceful restart or stop](#graceful-restart-or-stop)
 | 
				
			||||||
 | 
					    - [Build a single binary with templates](#build-a-single-binary-with-templates)
 | 
				
			||||||
- [Testing](#testing)
 | 
					- [Testing](#testing)
 | 
				
			||||||
- [Users](#users--)
 | 
					- [Users](#users--)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -1393,6 +1394,50 @@ func main() {
 | 
				
			|||||||
}
 | 
					}
 | 
				
			||||||
```
 | 
					```
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					### Build a single binary with templates
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					You can build a server into a single binary containing templates by using [go-assets][].
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					[go-assets]: https://github.com/jessevdk/go-assets
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					```go
 | 
				
			||||||
 | 
					func main() {
 | 
				
			||||||
 | 
						r := gin.New()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						t, err := loadTemplate()
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
							panic(err)
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						r.SetHTMLTemplate(t)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						r.GET("/", func(c *gin.Context) {
 | 
				
			||||||
 | 
							c.HTML(http.StatusOK, "/html/index.tmpl",nil)
 | 
				
			||||||
 | 
						})
 | 
				
			||||||
 | 
						r.Run(":8080")
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// loadTemplate loads templates embedded by go-assets-builder
 | 
				
			||||||
 | 
					func loadTemplate() (*template.Template, error) {
 | 
				
			||||||
 | 
						t := template.New("")
 | 
				
			||||||
 | 
						for name, file := range Assets.Files {
 | 
				
			||||||
 | 
							if file.IsDir() || !strings.HasSuffix(name, ".tmpl") {
 | 
				
			||||||
 | 
								continue
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
							h, err := ioutil.ReadAll(file)
 | 
				
			||||||
 | 
							if err != nil {
 | 
				
			||||||
 | 
								return nil, err
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
							t, err = t.New(name).Parse(string(h))
 | 
				
			||||||
 | 
							if err != nil {
 | 
				
			||||||
 | 
								return nil, err
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						return t, nil
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					```
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					See a complete example in the `examples/assets-in-binary` directory.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
## Testing
 | 
					## Testing
 | 
				
			||||||
 | 
					
 | 
				
			||||||
The `net/http/httptest` package is preferable way for HTTP testing.
 | 
					The `net/http/httptest` package is preferable way for HTTP testing.
 | 
				
			||||||
 | 
				
			|||||||
							
								
								
									
										33
									
								
								examples/assets-in-binary/README.md
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										33
									
								
								examples/assets-in-binary/README.md
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,33 @@
 | 
				
			|||||||
 | 
					# 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/gin
 | 
				
			||||||
 | 
					go 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
 | 
				
			||||||
 | 
					```
 | 
				
			||||||
							
								
								
									
										34
									
								
								examples/assets-in-binary/assets.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										34
									
								
								examples/assets-in-binary/assets.go
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,34 @@
 | 
				
			|||||||
 | 
					package main
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import (
 | 
				
			||||||
 | 
						"time"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						"github.com/jessevdk/go-assets"
 | 
				
			||||||
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					var _Assetsbfa8d115ce0617d89507412d5393a462f8e9b003 = "<!doctype html>\n<body>\n  <p>Can you see this? → {{.Bar}}</p>\n</body>\n"
 | 
				
			||||||
 | 
					var _Assets3737a75b5254ed1f6d588b40a3449721f9ea86c2 = "<!doctype html>\n<body>\n  <p>Hello, {{.Foo}}</p>\n</body>\n"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// Assets returns go-assets FileSystem
 | 
				
			||||||
 | 
					var Assets = assets.NewFileSystem(map[string][]string{"/": {"html"}, "/html": {"bar.tmpl", "index.tmpl"}}, map[string]*assets.File{
 | 
				
			||||||
 | 
						"/": {
 | 
				
			||||||
 | 
							Path:     "/",
 | 
				
			||||||
 | 
							FileMode: 0x800001ed,
 | 
				
			||||||
 | 
							Mtime:    time.Unix(1524365738, 1524365738517125470),
 | 
				
			||||||
 | 
							Data:     nil,
 | 
				
			||||||
 | 
						}, "/html": {
 | 
				
			||||||
 | 
							Path:     "/html",
 | 
				
			||||||
 | 
							FileMode: 0x800001ed,
 | 
				
			||||||
 | 
							Mtime:    time.Unix(1524365491, 1524365491289799093),
 | 
				
			||||||
 | 
							Data:     nil,
 | 
				
			||||||
 | 
						}, "/html/bar.tmpl": {
 | 
				
			||||||
 | 
							Path:     "/html/bar.tmpl",
 | 
				
			||||||
 | 
							FileMode: 0x1a4,
 | 
				
			||||||
 | 
							Mtime:    time.Unix(1524365491, 1524365491289611557),
 | 
				
			||||||
 | 
							Data:     []byte(_Assetsbfa8d115ce0617d89507412d5393a462f8e9b003),
 | 
				
			||||||
 | 
						}, "/html/index.tmpl": {
 | 
				
			||||||
 | 
							Path:     "/html/index.tmpl",
 | 
				
			||||||
 | 
							FileMode: 0x1a4,
 | 
				
			||||||
 | 
							Mtime:    time.Unix(1524365491, 1524365491289995821),
 | 
				
			||||||
 | 
							Data:     []byte(_Assets3737a75b5254ed1f6d588b40a3449721f9ea86c2),
 | 
				
			||||||
 | 
						}}, "")
 | 
				
			||||||
							
								
								
									
										4
									
								
								examples/assets-in-binary/html/bar.tmpl
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										4
									
								
								examples/assets-in-binary/html/bar.tmpl
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,4 @@
 | 
				
			|||||||
 | 
					<!doctype html>
 | 
				
			||||||
 | 
					<body>
 | 
				
			||||||
 | 
					  <p>Can you see this? → {{.Bar}}</p>
 | 
				
			||||||
 | 
					</body>
 | 
				
			||||||
							
								
								
									
										4
									
								
								examples/assets-in-binary/html/index.tmpl
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										4
									
								
								examples/assets-in-binary/html/index.tmpl
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,4 @@
 | 
				
			|||||||
 | 
					<!doctype html>
 | 
				
			||||||
 | 
					<body>
 | 
				
			||||||
 | 
					  <p>Hello, {{.Foo}}</p>
 | 
				
			||||||
 | 
					</body>
 | 
				
			||||||
							
								
								
									
										48
									
								
								examples/assets-in-binary/main.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										48
									
								
								examples/assets-in-binary/main.go
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,48 @@
 | 
				
			|||||||
 | 
					package main
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import (
 | 
				
			||||||
 | 
						"html/template"
 | 
				
			||||||
 | 
						"io/ioutil"
 | 
				
			||||||
 | 
						"net/http"
 | 
				
			||||||
 | 
						"strings"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						"github.com/gin-gonic/gin"
 | 
				
			||||||
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func main() {
 | 
				
			||||||
 | 
						r := gin.New()
 | 
				
			||||||
 | 
						t, err := loadTemplate()
 | 
				
			||||||
 | 
						if err != nil {
 | 
				
			||||||
 | 
							panic(err)
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						r.SetHTMLTemplate(t)
 | 
				
			||||||
 | 
						r.GET("/", func(c *gin.Context) {
 | 
				
			||||||
 | 
							c.HTML(http.StatusOK, "/html/index.tmpl", gin.H{
 | 
				
			||||||
 | 
								"Foo": "World",
 | 
				
			||||||
 | 
							})
 | 
				
			||||||
 | 
						})
 | 
				
			||||||
 | 
						r.GET("/bar", func(c *gin.Context) {
 | 
				
			||||||
 | 
							c.HTML(http.StatusOK, "/html/bar.tmpl", gin.H{
 | 
				
			||||||
 | 
								"Bar": "World",
 | 
				
			||||||
 | 
							})
 | 
				
			||||||
 | 
						})
 | 
				
			||||||
 | 
						r.Run(":8080")
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func loadTemplate() (*template.Template, error) {
 | 
				
			||||||
 | 
						t := template.New("")
 | 
				
			||||||
 | 
						for name, file := range Assets.Files {
 | 
				
			||||||
 | 
							if file.IsDir() || !strings.HasSuffix(name, ".tmpl") {
 | 
				
			||||||
 | 
								continue
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
							h, err := ioutil.ReadAll(file)
 | 
				
			||||||
 | 
							if err != nil {
 | 
				
			||||||
 | 
								return nil, err
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
							t, err = t.New(name).Parse(string(h))
 | 
				
			||||||
 | 
							if err != nil {
 | 
				
			||||||
 | 
								return nil, err
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						return t, nil
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
							
								
								
									
										6
									
								
								vendor/vendor.json
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										6
									
								
								vendor/vendor.json
									
									
									
									
										vendored
									
									
								
							@ -33,6 +33,12 @@
 | 
				
			|||||||
			"revision": "5a0f697c9ed9d68fef0116532c6e05cfeae00e55",
 | 
								"revision": "5a0f697c9ed9d68fef0116532c6e05cfeae00e55",
 | 
				
			||||||
			"revisionTime": "2017-06-01T23:02:30Z"
 | 
								"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=",
 | 
								"checksumSHA1": "Ajh8TemnItg4nn+jKmVcsMRALBc=",
 | 
				
			||||||
			"path": "github.com/json-iterator/go",
 | 
								"path": "github.com/json-iterator/go",
 | 
				
			||||||
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user