From 5b9b0d85718338348e396aae11b832bd19d6f7e3 Mon Sep 17 00:00:00 2001 From: Rootul Patel Date: Mon, 20 Nov 2023 16:06:48 -0500 Subject: [PATCH] feat: include HTML template in binary --- Makefile | 2 ++ assets.go | 28 ++++++++++++++++++++++++++++ go.mod | 3 +++ go.sum | 7 +++++++ {templates => html}/index.tmpl | 0 main.go | 32 +++++++++++++++++++++++++++++--- 6 files changed, 69 insertions(+), 3 deletions(-) create mode 100644 assets.go rename {templates => html}/index.tmpl (100%) diff --git a/Makefile b/Makefile index cde6362..eb89b49 100644 --- a/Makefile +++ b/Makefile @@ -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 diff --git a/assets.go b/assets.go new file mode 100644 index 0000000..079d028 --- /dev/null +++ b/assets.go @@ -0,0 +1,28 @@ +package main + +import ( + "time" + + "github.com/jessevdk/go-assets" +) + +var _Assets3737a75b5254ed1f6d588b40a3449721f9ea86c2 = "\n\tThese routes return values in TIA:\n \n\n" + +// Assets returns go-assets FileSystem +var Assets = assets.NewFileSystem(map[string][]string{"/": []string{"html"}, "/html": []string{"index.tmpl"}}, map[string]*assets.File{ + "/": &assets.File{ + Path: "/", + FileMode: 0x800001ed, + Mtime: time.Unix(1700513691, 1700513691812742423), + Data: nil, + }, "/html": &assets.File{ + Path: "/html", + FileMode: 0x800001ed, + Mtime: time.Unix(1700511741, 1700511741825205591), + Data: nil, + }, "/html/index.tmpl": &assets.File{ + Path: "/html/index.tmpl", + FileMode: 0x1a4, + Mtime: time.Unix(1700513808, 1700513808546131264), + Data: []byte(_Assets3737a75b5254ed1f6d588b40a3449721f9ea86c2), + }}, "") diff --git a/go.mod b/go.mod index 9d5db7a..c55b035 100644 --- a/go.mod +++ b/go.mod @@ -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 diff --git a/go.sum b/go.sum index 0d64bc3..c072879 100644 --- a/go.sum +++ b/go.sum @@ -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= @@ -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= diff --git a/templates/index.tmpl b/html/index.tmpl similarity index 100% rename from templates/index.tmpl rename to html/index.tmpl diff --git a/main.go b/main.go index 3b7871c..cca911c 100644 --- a/main.go +++ b/main.go @@ -1,7 +1,10 @@ package main import ( + "html/template" + "io" "net/http" + "strings" "time" "github.com/celestiaorg/supply/internal" @@ -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 +}