Skip to content

Commit 0bc4671

Browse files
committed
Use build tags to manage go-bindata
Signed-off-by: Carlos Martín <[email protected]>
1 parent 9a885b0 commit 0bc4671

File tree

5 files changed

+46
-55
lines changed

5 files changed

+46
-55
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
# builds
88
/build
9+
server/assets/asset.go
910

1011
# testing, logs, caches...
1112
/coverage

Makefile

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,24 @@ GODEP := dep
1818
GOLINT := golint
1919
GOVET := go vet
2020
BINDATA := go-bindata
21-
DIFF := diff
2221
YARN := yarn --cwd $(FRONTEND_PATH)
2322
REMOVE := rm -rf
2423
MOVE := mv
2524
MKDIR := mkdir -p
2625
COMPOSE := docker-compose
2726

27+
# To be used as -tags
28+
GO_BINDATA_TAG := bindata
29+
30+
# Override Makefile.main defaults for arguments to be used in `go` commands.
31+
GO_BUILD_ARGS := -ldflags "$(LD_FLAGS)" -tags "$(GO_BINDATA_TAG)"
32+
33+
# Environment and arguments to use in `go run` calls.
34+
GO_RUN_ENV := GITBASEPG_ENV=dev
35+
GO_RUN_ARGS += -tags "$(GO_BINDATA_TAG)"
36+
37+
GORUN = $(GOCMD) run $(GO_RUN_ARGS)
38+
2839
# Default rule
2940
all:
3041

@@ -55,7 +66,6 @@ lint: back-lint front-lint
5566

5667
validate-commit: | \
5768
back-dependencies \
58-
back-ensure-assets-proxy \
5969
front-fix-lint-errors \
6070
no-changes-in-commit
6171

@@ -67,9 +77,9 @@ clean: front-clean
6777
build-path:
6878
$(MKDIR) $(BUILD_PATH)
6979

70-
## Compiles the assets, and serve the tool through its API
80+
# Compiles the assets, and serves the tool through its API
7181

72-
serve: | front-dependencies front-build back-start
82+
serve: | front-dependencies front-build back-build back-start
7383

7484
compose-serve-latest:
7585
$(COMPOSE) pull && \
@@ -87,10 +97,6 @@ require-repos-folder:
8797
$(MKDIR) $(GITBASEPG_REPOS_FOLDER)
8898

8999
# Backend
90-
91-
assets := ./server/assets/asset.go
92-
assets_back := $(assets).bak
93-
94100
back-dependencies:
95101
$(GODEP) ensure
96102

@@ -108,10 +114,7 @@ $(GO_LINTABLE_PACKAGES):
108114
$(GOVET) $@
109115

110116
back-start:
111-
GITBASEPG_ENV=dev go run cmd/gitbase-playground/main.go
112-
113-
back-ensure-assets-proxy:
114-
$(DIFF) $(assets) $(assets_back) || exit 1
117+
$(GO_RUN_ENV) $(GORUN) cmd/gitbase-playground/main.go
115118

116119
# Frontend
117120

server/assets/asset.go

Lines changed: 0 additions & 40 deletions
This file was deleted.

server/handler/static_files.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// +build bindata
2+
13
package handler
24

35
import (
@@ -59,15 +61,15 @@ func (s *Static) ServeHTTP(w http.ResponseWriter, r *http.Request) {
5961
return
6062
}
6163

62-
s.ServeIndexHTML(nil)(w, r)
64+
s.serveIndexHTML(nil)(w, r)
6365
return
6466
}
6567

6668
s.serveAsset(w, r, filepath, b)
6769
}
6870

69-
// ServeIndexHTML serves index.html file
70-
func (s *Static) ServeIndexHTML(initialState interface{}) http.HandlerFunc {
71+
// serveIndexHTML serves index.html file
72+
func (s *Static) serveIndexHTML(initialState interface{}) http.HandlerFunc {
7173
return func(w http.ResponseWriter, r *http.Request) {
7274
filepath := path.Join(s.dir, indexFileName)
7375
b, err := assets.Asset(filepath)

server/handler/static_files_dev.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// +build !bindata
2+
3+
package handler
4+
5+
import (
6+
"net/http"
7+
)
8+
9+
// Static without the 'bindata' tag contains a placeholder handler
10+
// that returns 'not implemented'
11+
type Static struct{}
12+
13+
// NewStatic creates new Static
14+
func NewStatic(dir, serverURL string, selectLimit int, footerHTML string) *Static {
15+
return &Static{}
16+
}
17+
18+
// ServeHTTP serves any static file from static directory or fallbacks on index.hml
19+
func (s *Static) ServeHTTP(w http.ResponseWriter, r *http.Request) {
20+
http.Error(w,
21+
"Frontend assets are only available when using 'make build' or 'make serve'",
22+
http.StatusNotImplemented)
23+
24+
return
25+
}

0 commit comments

Comments
 (0)