Skip to content

Commit

Permalink
Use Go's native embedding functionality to simplify build
Browse files Browse the repository at this point in the history
  • Loading branch information
alexbakker committed Mar 16, 2022
1 parent 2128219 commit eb141f7
Show file tree
Hide file tree
Showing 130 changed files with 78 additions and 15,271 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ jobs:
- name: Setup
uses: actions/setup-go@v2
with:
go-version: '^1.15'
go-version: '^1.16'
- name: Checkout
uses: actions/checkout@v2
- name: Build
Expand Down
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
build/
cmd/toxstatus/assets.go
nodes.json
6 changes: 1 addition & 5 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
all: prep assets
all: prep
go build -o build/bin/toxstatus github.com/Tox/ToxStatus/cmd/toxstatus

assets:
go run vendor/github.com/alexbakker/go-embed/*.go -pkg=main -input=cmd/toxstatus/assets -output=cmd/toxstatus/assets.go

prep:
mkdir -p build/bin

clean:
rm -rf build
rm -f cmd/toxstatus/assets.go
6 changes: 1 addition & 5 deletions cmd/toxstatus/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,6 @@ func main() {
lastRefresh = state.LastRefresh
nodes = state.Nodes

if err := loadCountries(); err != nil {
log.Fatalf("error loading countries.json: %s", err.Error())
}

inst, err := NewInstance(":33450")
if err != nil {
log.Fatalf("fatal: %s", err.Error())
Expand Down Expand Up @@ -422,7 +418,7 @@ func connectTCP(node *toxNode, port int) (*net.TCPConn, error) {
dialer := net.Dialer{}
dialer.Deadline = time.Now().Add(2 * time.Second)
fmtIp := ""
if (ip.To4() == nil) {
if ip.To4() == nil {
// Wrap IPv6 in brackets
fmtIp = fmt.Sprintf("[%s]:%d", ip, port)
} else {
Expand Down
56 changes: 41 additions & 15 deletions cmd/toxstatus/template.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
package main

import (
"embed"
"fmt"
"html/template"
"io/fs"
"net/http"
"path/filepath"
"strings"
)

var (
assetMap = GetAssets()
//go:embed assets/*
assetsFs embed.FS

templateMap = loadTemplates()
funcMap = template.FuncMap{
"lower": strings.ToLower,
Expand All @@ -22,27 +26,49 @@ var (

func loadTemplates() (templates map[string]*template.Template) {
templates = map[string]*template.Template{}
baseLayout := "templates/base.html"
pages := matchAssetByPrefix("templates/pages/")
baseBytes := mustGetAssetBytes("assets/templates/base.html")

err := fs.WalkDir(assetsFs, "assets/templates/pages", func(path string, d fs.DirEntry, err error) error {
if err != nil {
panic(err)
}
if d.IsDir() {
return nil
}

for _, page := range pages {
//parse the child layout
tmplName := filepath.Base(page)
tmpl := template.Must(template.New(tmplName).Funcs(funcMap).Parse(string(assetMap[page])))
// parse the child layout
tmplName := filepath.Base(path)
bytes := mustGetAssetBytes(path)
tmpl := template.Must(template.New(tmplName).Funcs(funcMap).Parse(string(bytes)))

//and finally also parse the base layout
templates[tmplName] = template.Must(tmpl.Parse(string(assetMap[baseLayout])))
// and finally also parse the base layout
templates[tmplName] = template.Must(tmpl.Parse(string(baseBytes)))

return nil
})
if err != nil {
panic(fmt.Errorf("unable to load page templates: %w", err))
}

return
}

func matchAssetByPrefix(prefix string) (matches []string) {
for key := range assetMap {
if strings.HasPrefix(key, prefix) {
matches = append(matches, key)
}
func mustGetAssetBytes(name string) []byte {
bytes, err := getAssetBytes(name)
if err != nil {
panic(err)
}
return

return bytes
}

func getAssetBytes(name string) ([]byte, error) {
bytes, err := assetsFs.ReadFile(name)
if err != nil {
return nil, fmt.Errorf("unable to read asset: %w", err)
}

return bytes, nil
}

func renderTemplate(w http.ResponseWriter, name string, data interface{}) error {
Expand Down
16 changes: 7 additions & 9 deletions cmd/toxstatus/web.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"io/ioutil"
"net"
"net/http"
"path"
"strconv"
"strings"
"time"
Expand Down Expand Up @@ -50,14 +51,11 @@ var (
countries map[string]string
)

func loadCountries() error {
const name = "countries.json"
bytes, ok := assetMap[name]
if !ok {
return fmt.Errorf("asset %s not found", name)
func init() {
bytes := mustGetAssetBytes("assets/countries.json")
if err := json.Unmarshal(bytes, &countries); err != nil {
panic(fmt.Errorf("unable to parse countries: %w", err))
}

return json.Unmarshal(bytes, &countries)
}

func handleHTTPRequest(w http.ResponseWriter, r *http.Request) {
Expand All @@ -80,8 +78,8 @@ func handleHTTPRequest(w http.ResponseWriter, r *http.Request) {
case "/test", "/about":
filename = urlPath + ".html"
default:
data, ok := assetMap[urlPath]
if !ok {
data, err := getAssetBytes(path.Join("assets", urlPath))
if err != nil {
http.Error(w, http.StatusText(404), 404)
} else {
w.Header().Set("Content-Type", mimeTypeByExtension(urlPath))
Expand Down
10 changes: 5 additions & 5 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
module github.com/Tox/ToxStatus

go 1.15
go 1.16

require (
github.com/alexbakker/go-embed v0.0.0-20160504184358-845f3d77900c
github.com/alexbakker/tox4go v0.0.0-20170617131442-f2fe5c787e3e
github.com/didip/tollbooth/v6 v6.1.0
golang.org/x/crypto v0.0.0-20201221181555-eec23a3978ad // indirect
golang.org/x/time v0.0.0-20201208040808-7e3f01d25324 // indirect
github.com/didip/tollbooth/v6 v6.1.2
golang.org/x/crypto v0.0.0-20220315160706-3147a52a75dd // indirect
golang.org/x/sys v0.0.0-20220315194320-039c03cc5b86 // indirect
golang.org/x/time v0.0.0-20220224211638-0e9765cccd65 // indirect
)
31 changes: 15 additions & 16 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,12 @@ github.com/alexbakker/tox4go v0.0.0-20170617131442-f2fe5c787e3e h1:xYpIsctV2hzFR
github.com/alexbakker/tox4go v0.0.0-20170617131442-f2fe5c787e3e/go.mod h1:2X6sEQF4Ispf3Gm4J9pjt1leacEqBvTl3tZETlOxSlg=
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/didip/tollbooth/v6 v6.1.0 h1:ZS2fNa9JhFdRSJCj3+V12VfuUifYrGB4Z0jSwXmKMeE=
github.com/didip/tollbooth/v6 v6.1.0/go.mod h1:xjcse6CTHCLuOkzsWrEgdy9WPJFv+p/x6v+MyfP+O9s=
github.com/didip/tollbooth/v6 v6.1.2 h1:Kdqxmqw9YTv0uKajBUiWQg+GURL/k4vy9gmLCL01PjQ=
github.com/didip/tollbooth/v6 v6.1.2/go.mod h1:xjcse6CTHCLuOkzsWrEgdy9WPJFv+p/x6v+MyfP+O9s=
github.com/go-pkgz/expirable-cache v0.0.3 h1:rTh6qNPp78z0bQE6HDhXBHUwqnV9i09Vm6dksJLXQDc=
github.com/go-pkgz/expirable-cache v0.0.3/go.mod h1:+IauqN00R2FqNRLCLA+X5YljQJrwB179PfiAoMPlTlQ=
github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI=
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
Expand All @@ -18,20 +16,21 @@ github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZN
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.5.1 h1:nOGnQDM7FYENwehXlg/kFVnos3rEvtKTjRvOWSzb6H4=
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20201221181555-eec23a3978ad h1:DN0cp81fZ3njFcrLCytUHRSUkqBjfTo4Tx9RJTWs0EY=
golang.org/x/crypto v0.0.0-20201221181555-eec23a3978ad/go.mod h1:jdWPYTVW3xRLrWPugEBEK3UY2ZEsg3UU495nc5E+M+I=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20191026070338-33540a1f6037 h1:YyJpGZS1sBuBCzLAR1VEpK193GlqGZbnPFnPV/5Rsb4=
golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/term v0.0.0-20201117132131-f5c789dd3221/go.mod h1:Nr5EML6q2oocZ2LXRh80K7BxOlk5/8JxuGnuhpl+muw=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/crypto v0.0.0-20220315160706-3147a52a75dd h1:XcWmESyNjXJMLahc3mqVQJcgSTDxFxhETVlfk9uGc38=
golang.org/x/crypto v0.0.0-20220315160706-3147a52a75dd/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4=
golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220315194320-039c03cc5b86 h1:A9i04dxx7Cribqbs8jf3FQLogkL/CV2YN7hj9KWJCkc=
golang.org/x/sys v0.0.0-20220315194320-039c03cc5b86/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/time v0.0.0-20200416051211-89c76fbcd5d1/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
golang.org/x/time v0.0.0-20201208040808-7e3f01d25324 h1:Hir2P/De0WpUhtrKGGjvSb2YxUgyZ7EFOSLIcSSpiwE=
golang.org/x/time v0.0.0-20201208040808-7e3f01d25324/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
golang.org/x/time v0.0.0-20220224211638-0e9765cccd65 h1:M73Iuj3xbbb9Uk1DYhzydthsj6oOd6l9bpuFcNoUvTs=
golang.org/x/time v0.0.0-20220224211638-0e9765cccd65/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 h1:qIbj1fsPNlZgppZ+VLlY7N33q108Sa+fhmuc+sWQYwY=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
7 changes: 7 additions & 0 deletions shell.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
with import <nixpkgs> {};
mkShell {
buildInputs = [
go
gnumake
];
}
24 changes: 0 additions & 24 deletions vendor/github.com/alexbakker/go-embed/.gitignore

This file was deleted.

Loading

0 comments on commit eb141f7

Please sign in to comment.