forked from gin-gonic/gin
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add example to build single binary with templates (gin-gonic#1328)
- Loading branch information
Showing
8 changed files
with
175 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,7 +9,7 @@ go: | |
- master | ||
|
||
git: | ||
depth: 3 | ||
depth: 10 | ||
|
||
install: | ||
- make install | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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), | ||
}}, "") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
<!doctype html> | ||
<body> | ||
<p>Can you see this? → {{.Bar}}</p> | ||
</body> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
<!doctype html> | ||
<body> | ||
<p>Hello, {{.Foo}}</p> | ||
</body> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters