Update build system for javascript/go asset packaging

pull/10616/head
Chris Goller 2016-09-19 09:36:48 -07:00
parent a1a5631513
commit 977c9b6b78
4 changed files with 98 additions and 32 deletions

View File

@ -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

8
assets.go Normal file
View File

@ -0,0 +1,8 @@
package mrfusion
import "net/http"
// Assets returns a handler to serve the website.
type Assets interface {
Serve() http.Handler
}

34
dist/dist.go vendored
View File

@ -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

View File

@ -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)