diff --git a/Makefile b/Makefile index a326ec5..e1d5508 100644 --- a/Makefile +++ b/Makefile @@ -6,6 +6,8 @@ GOARCH ?= $(shell go env GOARCH) GOPKG = github.com/longsleep/realtimetraffic GOPATH = "$(CURDIR)/vendor:$(CURDIR)" SYSTEM_GOPATH = /usr/share/gocode/src/ +VERSION = $(shell git describe --tags --dirty 2>/dev/null || git rev-parse --short HEAD 2>/dev/null || echo git) +BUILDSTAMP = $(shell date -u '+%Y-%m-%dT%T%z') DIST := $(PWD)/dist DIST_SRC := $(DIST)/src @@ -40,7 +42,13 @@ generate-dev: GOPATH=$(GOPATH) $(CURDIR)/vendor/bin/go-bindata -dev -prefix "client/static/" -pkg client -o client/bindata.go client/static/...; fi binary-%: generate - GOPATH=$(GOPATH) GOOS=linux GOARCH=$(GOARCH) GOARM=$(GOARM) CGO_ENABLED=0 go build -ldflags="-s -w" -o bin/realtimetrafficd-$(GOARCH) realtimetrafficd/*.go + GOPATH=$(GOPATH) GOOS=linux GOARCH=$(GOARCH) GOARM=$(GOARM) CGO_ENABLED=0 \ + go build \ + -ldflags="-s -w \ + -X main.Version=$(VERSION) \ + -X main.BuildStamp=$(BUILDSTAMP) \ + " \ + -o bin/realtimetrafficd-$(GOARCH) realtimetrafficd/*.go binary: binary-$(GOARCH) mv bin/realtimetrafficd-$(GOARCH) bin/realtimetrafficd diff --git a/realtimetrafficd/main.go b/realtimetrafficd/main.go index ed7ff6d..a01602a 100644 --- a/realtimetrafficd/main.go +++ b/realtimetrafficd/main.go @@ -19,16 +19,16 @@ package main import ( "flag" + "fmt" "log" "net/http" + "runtime" "github.com/longsleep/realtimetraffic/client" "github.com/gorilla/websocket" ) -var listenAddr *string - func serveClient(w http.ResponseWriter, r *http.Request) { if r.Method != "GET" && r.Method != "HEAD" { http.Error(w, "Method nod allowed", 405) @@ -68,15 +68,26 @@ func serveWs(w http.ResponseWriter, r *http.Request) { func main() { var err error - listenAddr = flag.String("listen", "127.0.0.1:8088", "Listen address.") - + listenAddr := flag.String("listen", "127.0.0.1:8088", "Listen address.") + versionFlag := flag.Bool("version", false, "Version") flag.Parse() + + if *versionFlag { + fmt.Println("Version :", Version) + fmt.Println("Build time :", BuildStamp) + fmt.Println("Go :", runtime.Version()) + return + } + go h.run() http.HandleFunc("/", serveClient) http.HandleFunc("/realtimetraffic", serveWs) + log.Printf("starting realtimetrafficd %s on %sn", Version, *listenAddr) err = http.ListenAndServe(*listenAddr, nil) if err != nil { - log.Fatal("ListenAndServe: ", err) + log.Fatal(err.Error()) } + + defer log.Println("exiting") } diff --git a/realtimetrafficd/version.go b/realtimetrafficd/version.go new file mode 100644 index 0000000..c6b41dc --- /dev/null +++ b/realtimetrafficd/version.go @@ -0,0 +1,4 @@ +package main + +var Version = "0.0.0" +var BuildStamp = "not set"