Skip to content

Commit

Permalink
feat: include HTML template in binary
Browse files Browse the repository at this point in the history
  • Loading branch information
rootulp committed Nov 20, 2023
1 parent 3dbd9d0 commit 5b9b0d8
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 3 deletions.
2 changes: 2 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ lint:

## build: Build the supply-server binary.
build:
@echo "--> Generating assets.go based on the html directory"
@go-assets-builder html -o assets.go
@echo "--> Building the binary and saving in bin/supply-server"
@go build -o bin/supply-server
.PHONY: build
Expand Down
28 changes: 28 additions & 0 deletions assets.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package main

import (
"time"

"github.com/jessevdk/go-assets"
)

var _Assets3737a75b5254ed1f6d588b40a3449721f9ea86c2 = "<html>\n\tThese routes return values in TIA:\n <ul>\n \t\t<li><a href={{ .RouteCirculatingSupply }}>{{ .RouteCirculatingSupply }}</a></li>\n\t\t<li><a href={{ .RouteTotalSupply }}>{{ .RouteTotalSupply }}</a></li>\n\t</ul>\n</html>\n"

// Assets returns go-assets FileSystem
var Assets = assets.NewFileSystem(map[string][]string{"/": []string{"html"}, "/html": []string{"index.tmpl"}}, map[string]*assets.File{

Check failure on line 12 in assets.go

View workflow job for this annotation

GitHub Actions / golangci-lint

File is not `gofumpt`-ed (gofumpt)
"/": &assets.File{
Path: "/",
FileMode: 0x800001ed,
Mtime: time.Unix(1700513691, 1700513691812742423),
Data: nil,
}, "/html": &assets.File{

Check failure on line 18 in assets.go

View workflow job for this annotation

GitHub Actions / golangci-lint

File is not `gofumpt`-ed (gofumpt)
Path: "/html",
FileMode: 0x800001ed,
Mtime: time.Unix(1700511741, 1700511741825205591),
Data: nil,
}, "/html/index.tmpl": &assets.File{

Check failure on line 23 in assets.go

View workflow job for this annotation

GitHub Actions / golangci-lint

File is not `gofumpt`-ed (gofumpt)
Path: "/html/index.tmpl",
FileMode: 0x1a4,
Mtime: time.Unix(1700513808, 1700513808546131264),
Data: []byte(_Assets3737a75b5254ed1f6d588b40a3449721f9ea86c2),
}}, "")
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ require (
github.com/go-playground/universal-translator v0.18.1 // indirect
github.com/go-playground/validator/v10 v10.14.0 // indirect
github.com/goccy/go-json v0.10.2 // indirect
github.com/jessevdk/go-assets v0.0.0-20160921144138-4f4301a06e15 // indirect
github.com/jessevdk/go-assets-builder v0.0.0-20130903091706-b8483521738f // indirect
github.com/jessevdk/go-flags v1.5.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/klauspost/cpuid/v2 v2.2.4 // indirect
github.com/leodido/go-urn v1.2.4 // indirect
Expand Down
7 changes: 7 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@ github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaS
github.com/google/go-cmp v0.5.5 h1:Khx7svrCpmxxtHBq5j2mp/xVjsi8hQMfNLvJFAlrGgU=
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
github.com/jessevdk/go-assets v0.0.0-20160921144138-4f4301a06e15 h1:cW/amwGEJK5MSKntPXRjX4dxs/nGxGT8gXKIsKFmHGc=
github.com/jessevdk/go-assets v0.0.0-20160921144138-4f4301a06e15/go.mod h1:Fdm/oWRW+CH8PRbLntksCNtmcCBximKPkVQYvmMl80k=
github.com/jessevdk/go-assets-builder v0.0.0-20130903091706-b8483521738f h1:K2zqtTU3T3ZX/vVeFtJ1OoxEm+gsLhu3zQ34tKgOAyk=
github.com/jessevdk/go-assets-builder v0.0.0-20130903091706-b8483521738f/go.mod h1:GjkD6wGIxVEccQ4pa27Ebe00zAi1EEpAcL6rL0ADvwU=
github.com/jessevdk/go-flags v1.5.0 h1:1jKYvbxEjfUl0fmqTCOfonvskHHXMjBySTLW4y9LFvc=
github.com/jessevdk/go-flags v1.5.0/go.mod h1:Fw0T6WPc1dYxT4mKEZRfG5kJhaTDP9pj1c2EWnYs/m4=
github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM=
github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo=
github.com/klauspost/cpuid/v2 v2.0.9/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa0213Md/qVLRg=
Expand Down Expand Up @@ -68,6 +74,7 @@ golang.org/x/crypto v0.14.0 h1:wBqGXzWJW6m1XrIKlAH0Hs1JJ7+9KBwnIO8v66Q9cHc=
golang.org/x/crypto v0.14.0/go.mod h1:MVFd36DqK4CsrnJYDkBA3VC4m2GkXAM0PvzMCn4JQf4=
golang.org/x/net v0.17.0 h1:pVaXccu2ozPjCXewfr1S7xza/zcXTity9cCdXQYSjIM=
golang.org/x/net v0.17.0/go.mod h1:NxSsAGuq816PNPmqtQdLE42eU2Fs7NoRIZrHJAlaCOE=
golang.org/x/sys v0.0.0-20210320140829-1e4c9ba3b0c4/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20220704084225-05e143d24a9e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.13.0 h1:Af8nKPmuFypiUBjVoU9V20FiaFXOcuZI21p0ycVYYGE=
Expand Down
File renamed without changes.
32 changes: 29 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package main

import (
"html/template"
"io"
"net/http"
"strings"
"time"

"github.com/celestiaorg/supply/internal"
Expand Down Expand Up @@ -30,17 +33,40 @@ func getTotalSupply(c *gin.Context) {

func main() {
router := gin.Default()
router.LoadHTMLGlob("templates/*")
t, err := loadTemplate()
if err != nil {
panic(err)
}
router.SetHTMLTemplate(t)
router.GET("/", func(c *gin.Context) {
c.HTML(http.StatusOK, "index.tmpl", gin.H{
c.HTML(http.StatusOK, "/html/index.tmpl", gin.H{
"RouteCirculatingSupply": RouteCirculatingSupply,
"RouteTotalSupply": RouteTotalSupply,
})
})
router.GET(RouteCirculatingSupply, getCirculatingSupply)
router.GET(RouteTotalSupply, getTotalSupply)
err := router.Run("0.0.0.0:8080")
err = router.Run("0.0.0.0:8080")
if err != nil {
panic(err)
}
}

// 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 := io.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
}

0 comments on commit 5b9b0d8

Please sign in to comment.