Skip to content

Commit

Permalink
template: Bundle in binary with bindata (#36)
Browse files Browse the repository at this point in the history
This uses go-bindata/go-bindata to bundle the template in the binary.

I also realized that the handler test was now broken because we were
checking the exact contents of the index page in the test. This change
fixes that too.
abhinav authored Jan 3, 2019
1 parent a3b25c3 commit 2bf0f68
Showing 9 changed files with 293 additions and 24 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -4,6 +4,13 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]
### Fixed
- Templates are now bundled with the binary rather than requiring a copy of the
sally source.

## 1.0.0 - 2019-01-03

- Initial tagged release.

[Unreleased]: https://github.com/uber-go/sally/compare/v1.0.0...HEAD
1 change: 0 additions & 1 deletion Dockerfile.scratch
Original file line number Diff line number Diff line change
@@ -3,5 +3,4 @@ FROM scratch
EXPOSE 8080
ADD sally.yaml /
ADD _tmp/sally /
ADD templates /templates
ENTRYPOINT ["/sally"]
7 changes: 7 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
GOLINT = go run github.com/golang/lint/golint
STATICCHECK = go run honnef.co/go/tools/cmd/staticcheck
GOBINDATA = go run github.com/go-bindata/go-bindata/go-bindata

.PHONY: all
all: test
@@ -8,6 +9,12 @@ all: test
build:
go build

.PHONY: generate
generate: bindata.go

bindata.go: templates/*
$(GOBINDATA) templates

.PHONY: install
install:
go install .
258 changes: 258 additions & 0 deletions bindata.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -2,6 +2,7 @@ module go.uber.org/sally

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/go-bindata/go-bindata v1.0.0
github.com/golang/lint v0.0.0-20181217174547-8f45f776aaf1
github.com/julienschmidt/httprouter v1.2.0
github.com/kisielk/gotool v1.0.0 // indirect
3 changes: 3 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/go-bindata/go-bindata v1.0.0 h1:upAQEGKG3hFv00/4cU/VhzF+NpCHz+Jk3rO946wWr+A=
github.com/go-bindata/go-bindata v1.0.0/go.mod h1:xK8Dsgwmeed+BBsSy2XTopBn/8uK2HWuGSnA11C3Joo=
github.com/golang/lint v0.0.0-20181217174547-8f45f776aaf1 h1:6DVPu65tee05kY0/rciBQ47ue+AnuY8KTayV6VHikIo=
github.com/golang/lint v0.0.0-20181217174547-8f45f776aaf1/go.mod h1:tluoj9z5200jBnyusfRPU2LqT6J+DAorxEvtC7LHB+E=
github.com/julienschmidt/httprouter v1.2.0 h1:TDTW5Yz1mjftljbcKqRcrYhd4XeOoI98t+9HbQbYf7g=
@@ -11,6 +13,7 @@ github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORN
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/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/testify v1.2.2 h1:bSDNvY7ZPG5RlJ8otE/7V6gMiyenm9RtJ7IUVIAoJ1w=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
17 changes: 6 additions & 11 deletions handler.go
Original file line number Diff line number Diff line change
@@ -8,17 +8,12 @@ import (
"github.com/julienschmidt/httprouter"
)

var indexTemplate, packageTemplate *template.Template

func init() {
tmpls := template.Must(template.ParseGlob("templates/*.html"))
if indexTemplate = tmpls.Lookup("index.html"); indexTemplate == nil {
panic("Missing index.html template")
}
if packageTemplate = tmpls.Lookup("package.html"); packageTemplate == nil {
panic("Missing package.html template")
}
}
var (
indexTemplate = template.Must(
template.New("index.html").Parse(string(MustAsset("templates/index.html"))))
packageTemplate = template.Must(
template.New("package.html").Parse(string(MustAsset("templates/package.html"))))
)

// CreateHandler creates a Sally http.Handler
func CreateHandler(config *Config) http.Handler {
22 changes: 10 additions & 12 deletions handler_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package main

import "testing"
import (
"testing"

"github.com/stretchr/testify/assert"
)

var config = `
@@ -15,17 +19,11 @@ packages:

func TestIndex(t *testing.T) {
rr := CallAndRecord(t, config, "/")
AssertResponse(t, rr, 200, `
<!DOCTYPE html>
<html>
<body>
<ul>
<li>thriftrw - github.com/thriftrw/thriftrw-go</li>
<li>yarpc - github.com/yarpc/yarpc-go</li>
</ul>
</body>
</html>
`)
assert.Equal(t, 200, rr.Code)

body := rr.Body.String()
assert.Contains(t, body, "github.com/thriftrw/thriftrw-go")
assert.Contains(t, body, "github.com/yarpc/yarpc-go")
}

func TestPackageShouldExist(t *testing.T) {
1 change: 1 addition & 0 deletions tools.go
Original file line number Diff line number Diff line change
@@ -3,6 +3,7 @@
package main

import (
_ "github.com/go-bindata/go-bindata/go-bindata"
_ "github.com/golang/lint/golint"
_ "honnef.co/go/tools/cmd/staticcheck"
)

0 comments on commit 2bf0f68

Please sign in to comment.