From 977c9b6b78fc8bdd741e6939949801e00a4c6541 Mon Sep 17 00:00:00 2001 From: Chris Goller Date: Mon, 19 Sep 2016 09:36:48 -0700 Subject: [PATCH] Update build system for javascript/go asset packaging --- Makefile | 46 +++++++++++++++++++++------------- assets.go | 8 ++++++ dist/dist.go | 34 +++++++++++++++++++++++++ restapi/configure_mr_fusion.go | 42 ++++++++++++++++++++----------- 4 files changed, 98 insertions(+), 32 deletions(-) create mode 100644 assets.go diff --git a/Makefile b/Makefile index e2b717986b..50e35685c1 100644 --- a/Makefile +++ b/Makefile @@ -5,12 +5,14 @@ BUILD_TIME ?= $$(date +%FT%T%z) SOURCES := $(shell find . -name '*.go') -LDFLAGS=-ldflags "-s -X dist.rootDir=. -X main.Version=${VERSION} -X main.Commit=${COMMIT} -X main.BuildTime=${BUILD_TIME} -X main.Branch=${BRANCH}" +LDFLAGS=-ldflags "-s -X main.Version=${VERSION} -X main.Commit=${COMMIT} -X main.BuildTime=${BUILD_TIME} -X main.Branch=${BRANCH}" BINARY=mrfusion -default: prepare ${BINARY} +default: dep build -prepare: dev assets +build: assets ${BINARY} + +dev: dev-assets ${BINARY} ${BINARY}: $(SOURCES) go build -o ${BINARY} ${LDFLAGS} ./cmd/mr-fusion-server/main.go @@ -18,33 +20,37 @@ ${BINARY}: $(SOURCES) docker-${BINARY}: $(SOURCES) CGO_ENABLED=0 GOOS=linux go build -installsuffix cgo -o ${BINARY} ${LDFLAGS} \ ./cmd/mr-fusion-server/main.go -docker: docker-${BINARY} + +docker: dep assets docker-${BINARY} docker build -t mrfusion . +assets: js bindata + +devassets: dev-js dev-bindata + bindata: + go-bindata -o dist/dist_gen.go -ignore 'map|go' -pkg dist ui/build/... + +dev-bindata: go-bindata -debug -dev -o dist/dist_gen.go -ignore 'map|go' -pkg dist ui/build/... -assets: jsbuild bindata - - -jsbuild: +js: cd ui && npm run build -dev: jsdev godev +dev-js: + cd ui && npm run build:dev -godev: +dep: jsdep godep + +godep: go get github.com/sparrc/gdm gdm restore go get -u github.com/jteeuwen/go-bindata/... -jsdev: +jsdep: cd ui && npm install -clean: - if [ -f ${BINARY} ] ; then rm ${BINARY} ; fi - cd ui && npm run clean - -test: gotest jstest +test: jstest gotest gotest: go test -race ./... @@ -52,7 +58,11 @@ gotest: jstest: cd ui && npm test -run: +run: ${BINARY} ./mrfusion --port 8888 -.PHONY: clean test jstest run +clean: + if [ -f ${BINARY} ] ; then rm ${BINARY} ; fi + cd ui && npm run clean + +.PHONY: clean test jstest gotest run diff --git a/assets.go b/assets.go new file mode 100644 index 0000000000..38fa016b84 --- /dev/null +++ b/assets.go @@ -0,0 +1,8 @@ +package mrfusion + +import "net/http" + +// Assets returns a handler to serve the website. +type Assets interface { + Serve() http.Handler +} diff --git a/dist/dist.go b/dist/dist.go index e8907e81d0..11e4f89e19 100644 --- a/dist/dist.go +++ b/dist/dist.go @@ -1,3 +1,37 @@ package dist +import ( + "net/http" + + "github.com/elazarl/go-bindata-assetfs" +) + +// DebugAssets serves assets via a specified directory +type DebugAssets struct { + Dir string // Dir is a directory location of asset files +} + +// Serve is an http.FileServer for the Dir +func (d *DebugAssets) Serve() http.Handler { + return http.FileServer(http.Dir(d.Dir)) +} + +// BindataAssets serves assets from go-bindata +type BindataAssets struct { + Prefix string // Prefix is prepended to the http file request +} + +// Serve serves go-bindata using a go-bindata-assetfs façade +func (b *BindataAssets) Serve() http.Handler { + var dir http.FileSystem = &assetfs.AssetFS{ + Asset: Asset, + AssetDir: AssetDir, + AssetInfo: AssetInfo, + Prefix: b.Prefix} + return http.FileServer(dir) +} + +// You found me! I was hiding down here! + +// rootDir is used for go-bindata dev mode to specify a relative path. var rootDir string diff --git a/restapi/configure_mr_fusion.go b/restapi/configure_mr_fusion.go index e8790880de..72897dae4c 100644 --- a/restapi/configure_mr_fusion.go +++ b/restapi/configure_mr_fusion.go @@ -2,7 +2,6 @@ package restapi import ( "crypto/tls" - "fmt" "log" "net/http" "strings" @@ -10,8 +9,10 @@ import ( errors "github.com/go-openapi/errors" runtime "github.com/go-openapi/runtime" middleware "github.com/go-openapi/runtime/middleware" + "github.com/go-openapi/swag" "golang.org/x/net/context" + "github.com/influxdata/mrfusion" "github.com/influxdata/mrfusion/dist" "github.com/influxdata/mrfusion/mock" "github.com/influxdata/mrfusion/restapi/operations" @@ -21,8 +22,30 @@ import ( //go:generate swagger generate server --target .. --name --spec ../swagger.yaml --with-context +var devFlags = struct { + Develop bool `short:"d" long:"develop" description:"Run server in develop mode."` +}{} + func configureFlags(api *operations.MrFusionAPI) { - // api.CommandLineOptionsGroups = []swag.CommandLineOptionsGroup{ ... } + api.CommandLineOptionsGroups = []swag.CommandLineOptionsGroup{ + swag.CommandLineOptionsGroup{ + ShortDescription: "Develop Mode server", + LongDescription: "Server will use the ui/build directory directly.", + Options: &devFlags, + }, + } +} + +func assets() mrfusion.Assets { + if devFlags.Develop { + log.Printf("Running in develop mode.") + return &dist.DebugAssets{ + Dir: "ui", + } + } + return &dist.BindataAssets{ + Prefix: "ui", + } } func configureAPI(api *operations.MrFusionAPI) http.Handler { @@ -152,20 +175,11 @@ func setupGlobalMiddleware(handler http.Handler) http.Handler { if strings.Contains(r.URL.Path, "/chronograf/v1") { handler.ServeHTTP(w, r) return - } else if r.URL.Path == "/build/" { - octets, _ := dist.Asset("ui/build/index.html") - fmt.Fprintf(w, "%s", string(octets)) + } else if r.URL.Path == "/build" { + http.Redirect(w, r, "", http.StatusFound) return } else if strings.Index(r.URL.Path, "/build/") == 0 { - octets, err := dist.Asset("ui" + r.URL.Path) - if err != nil { - http.NotFound(w, r) - } - if strings.Contains(r.URL.Path, ".css") { - w.Header().Set("Content-Type", "text/css") - } - - fmt.Fprintf(w, "%s", string(octets)) + assets().Serve() return } http.Redirect(w, r, "/build/index.html", http.StatusFound)