From 4edb3a16b65facc9b50faa9421aa8373bbeeabfc Mon Sep 17 00:00:00 2001 From: Simon Eisenmann Date: Mon, 22 May 2017 14:04:03 +0200 Subject: [PATCH] Reorganized code and include client in binary --- .gitignore | 1 + Makefile | 90 ++++++++---------- client/client.go | 22 +++++ client/{ => static}/css/base.css | 0 client/{ => static}/css/detail.css | 0 client/{ => static}/css/graph.css | 0 client/{ => static}/css/legend.css | 0 client/{ => static}/favicon.ico | Bin .../index.html} | 0 .../{ => static}/scripts/console-shim-min.js | 0 client/{ => static}/scripts/d3.v2.min.js | 0 client/{ => static}/scripts/jquery-ui.min.js | 0 .../{ => static}/scripts/jquery.ba-bbq.min.js | 0 client/{ => static}/scripts/jquery.min.js | 0 client/{ => static}/scripts/modernizr.js | 0 .../{ => static}/scripts/realtimetraffic.js | 0 client/{ => static}/scripts/rickshaw.min.js | 0 .../conn.go | 32 +------ .../data.go | 0 .../grabber.go | 0 .../hub.go | 0 .../main.go | 62 +++++++----- 22 files changed, 105 insertions(+), 102 deletions(-) create mode 100644 client/client.go rename client/{ => static}/css/base.css (100%) rename client/{ => static}/css/detail.css (100%) rename client/{ => static}/css/graph.css (100%) rename client/{ => static}/css/legend.css (100%) rename client/{ => static}/favicon.ico (100%) rename client/{realtimetraffic.html => static/index.html} (100%) rename client/{ => static}/scripts/console-shim-min.js (100%) rename client/{ => static}/scripts/d3.v2.min.js (100%) rename client/{ => static}/scripts/jquery-ui.min.js (100%) rename client/{ => static}/scripts/jquery.ba-bbq.min.js (100%) rename client/{ => static}/scripts/jquery.min.js (100%) rename client/{ => static}/scripts/modernizr.js (100%) rename client/{ => static}/scripts/realtimetraffic.js (100%) rename client/{ => static}/scripts/rickshaw.min.js (100%) rename {src/realtimetraffic => realtimetrafficd}/conn.go (84%) rename {src/realtimetraffic => realtimetrafficd}/data.go (100%) rename {src/realtimetraffic => realtimetrafficd}/grabber.go (100%) rename {src/realtimetraffic => realtimetrafficd}/hub.go (100%) rename {src/realtimetraffic => realtimetrafficd}/main.go (54%) diff --git a/.gitignore b/.gitignore index ccd0805..3b509bc 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ /vendor /bin +/client/bindata.go diff --git a/Makefile b/Makefile index 5f5a95f..23c0344 100644 --- a/Makefile +++ b/Makefile @@ -1,60 +1,52 @@ +PWD := $(shell dirname $(realpath $(lastword $(MAKEFILE_LIST)))) -PKG := realtimetraffic -EXENAME := realtimetraffic - +GOPKG = github.com/longsleep/realtimetraffic GOPATH = "$(CURDIR)/vendor:$(CURDIR)" -SYSTEM_GOPATH := /usr/share/gocode/src/ -OUTPUT := $(CURDIR)/bin +SYSTEM_GOPATH = /usr/share/gocode/src/ -DESTDIR ?= / -BIN := $(DESTDIR)/usr/sbin -CONF := $(DESTDIR)/$(CONFIG_PATH) - -BUILD_ARCH := $(shell go env GOARCH) -DIST := $(CURDIR)/dist_$(BUILD_ARCH) +DIST := $(PWD)/dist DIST_SRC := $(DIST)/src -DIST_BIN := $(DIST)/bin -build: get binary +FOLDERS = $(shell find -mindepth 1 -maxdepth 1 -type d -not -path "*.git" -not -path "*debian" -not -path "*vendor" -not -path "*doc" -not -path "*bin") -gopath: - @echo GOPATH=$(GOPATH) - -get: - GOPATH=$(GOPATH) go get $(PKG) - -binary: - GOPATH=$(GOPATH) go build -o $(OUTPUT)/$(EXENAME) -ldflags '$(LDFLAGS)' $(PKG) - -binaryrace: - GOPATH=$(GOPATH) go build -race -o $(OUTPUT)/$(EXENAME) -ldflags '$(LDFLAGS)' $(PKG) - -fmt: - GOPATH=$(GOPATH) go fmt $(PKG)/... - -test: TESTDEPS = $(shell GOPATH=$(GOPATH) go list -f '{{.ImportPath}}{{"\n"}}{{join .Deps "\n"}}' $(PKG) |grep $(PKG)) -test: get - GOPATH=$(GOPATH) go test -i $(TESTDEPS) - GOPATH=$(GOPATH) go test -v $(TESTDEPS) - -clean: - GOPATH=$(GOPATH) go clean -i $(PKG) - rm -rf $(CURDIR)/pkg - -distclean: clean - rm -rf $(DIST) - -pristine: distclean - rm -rf vendor/* +all: build $(DIST_SRC): - mkdir -p $@ - -$(DIST_BIN): - mkdir -p $@ + mkdir -p $@ dist_gopath: $(DIST_SRC) - find $(SYSTEM_GOPATH) -mindepth 1 -maxdepth 1 -type d \ - -exec ln -sf {} $(DIST_SRC) \; + if [ -d "$(SYSTEM_GOPATH)" ]; then find $(SYSTEM_GOPATH) -mindepth 1 -maxdepth 1 -type d \ + -exec ln -sf {} $(DIST_SRC) \; ; fi + if [ ! -d "$(SYSTEM_GOPATH)" ]; then find $(CURDIR)/vendor/src -mindepth 1 -maxdepth 1 -type d \ + -exec ln -sf {} $(DIST_SRC) \; ; fi -.PHONY: clean distclean pristine get build gopath binary +goget: +# if [ -z "$(DEB_BUILDING)" ]; then GOPATH=$(GOPATH) go get github.com/rogpeppe/godeps; fi +# if [ -z "$(DEB_BUILDING)" ]; then GOPATH=$(GOPATH) $(CURDIR)/vendor/bin/godeps -u dependencies.tsv; fi + mkdir -p $(shell dirname "$(CURDIR)/vendor/src/$(GOPKG)") + rm -f $(CURDIR)/vendor/src/$(GOPKG) + ln -sf $(PWD) $(CURDIR)/vendor/src/$(GOPKG) + +generate: + if [ -z "$(DEB_BUILDING)" ]; then GOPATH=$(GOPATH) go get github.com/jteeuwen/go-bindata/...; fi + if [ -z "$(DEB_BUILDING)" ]; then GOPATH=$(GOPATH) $(CURDIR)/vendor/bin/go-bindata -prefix "client/static/" -pkg client -o client/bindata.go client/static/...; fi + +generate-dev: + GOPATH=$(GOPATH) $(CURDIR)/vendor/bin/go-bindata -dev -prefix "client/static/" -pkg client -o client/bindata.go client/static/...; fi + +binary: + GOPATH=$(GOPATH) go build -o bin/realtimetrafficd realtimetrafficd/*.go + +build: goget generate binary + +format: + find $(FOLDERS) \( -name "*.go" ! -name "bindata.go" \) -print0 | xargs -0 -n 1 go fmt + +dependencies.tsv: + set -e ;\ + TMP=$$(mktemp -d) ;\ + cp -r $(CURDIR)/vendor $$TMP ;\ + GOPATH=$$TMP/vendor:$(CURDIR) $(CURDIR)/vendor/bin/godeps $(GOPKG)/wlan > $(CURDIR)/dependencies.tsv ;\ + rm -rf $$TMP ;\ + +.PHONY: all dist_gopath goget generate generate-dev binary dependencies.tsv diff --git a/client/client.go b/client/client.go new file mode 100644 index 0000000..d547cf0 --- /dev/null +++ b/client/client.go @@ -0,0 +1,22 @@ +package client + +import ( + "net/http" + + "github.com/elazarl/go-bindata-assetfs" +) + +//go:generate go-bindata -prefix "static/" -pkg client -o bindata.go static/... + +func handler(w http.ResponseWriter, r *http.Request) { + http.FileServer( + &assetfs.AssetFS{ + Asset: Asset, + AssetDir: AssetDir, + AssetInfo: AssetInfo, + Prefix: "/", + }, + ).ServeHTTP(w, r) +} + +var HandlerFunc http.HandlerFunc = handler diff --git a/client/css/base.css b/client/static/css/base.css similarity index 100% rename from client/css/base.css rename to client/static/css/base.css diff --git a/client/css/detail.css b/client/static/css/detail.css similarity index 100% rename from client/css/detail.css rename to client/static/css/detail.css diff --git a/client/css/graph.css b/client/static/css/graph.css similarity index 100% rename from client/css/graph.css rename to client/static/css/graph.css diff --git a/client/css/legend.css b/client/static/css/legend.css similarity index 100% rename from client/css/legend.css rename to client/static/css/legend.css diff --git a/client/favicon.ico b/client/static/favicon.ico similarity index 100% rename from client/favicon.ico rename to client/static/favicon.ico diff --git a/client/realtimetraffic.html b/client/static/index.html similarity index 100% rename from client/realtimetraffic.html rename to client/static/index.html diff --git a/client/scripts/console-shim-min.js b/client/static/scripts/console-shim-min.js similarity index 100% rename from client/scripts/console-shim-min.js rename to client/static/scripts/console-shim-min.js diff --git a/client/scripts/d3.v2.min.js b/client/static/scripts/d3.v2.min.js similarity index 100% rename from client/scripts/d3.v2.min.js rename to client/static/scripts/d3.v2.min.js diff --git a/client/scripts/jquery-ui.min.js b/client/static/scripts/jquery-ui.min.js similarity index 100% rename from client/scripts/jquery-ui.min.js rename to client/static/scripts/jquery-ui.min.js diff --git a/client/scripts/jquery.ba-bbq.min.js b/client/static/scripts/jquery.ba-bbq.min.js similarity index 100% rename from client/scripts/jquery.ba-bbq.min.js rename to client/static/scripts/jquery.ba-bbq.min.js diff --git a/client/scripts/jquery.min.js b/client/static/scripts/jquery.min.js similarity index 100% rename from client/scripts/jquery.min.js rename to client/static/scripts/jquery.min.js diff --git a/client/scripts/modernizr.js b/client/static/scripts/modernizr.js similarity index 100% rename from client/scripts/modernizr.js rename to client/static/scripts/modernizr.js diff --git a/client/scripts/realtimetraffic.js b/client/static/scripts/realtimetraffic.js similarity index 100% rename from client/scripts/realtimetraffic.js rename to client/static/scripts/realtimetraffic.js diff --git a/client/scripts/rickshaw.min.js b/client/static/scripts/rickshaw.min.js similarity index 100% rename from client/scripts/rickshaw.min.js rename to client/static/scripts/rickshaw.min.js diff --git a/src/realtimetraffic/conn.go b/realtimetrafficd/conn.go similarity index 84% rename from src/realtimetraffic/conn.go rename to realtimetrafficd/conn.go index b945366..38e4875 100644 --- a/src/realtimetraffic/conn.go +++ b/realtimetrafficd/conn.go @@ -41,10 +41,9 @@ package main import ( - "github.com/gorilla/websocket" - "log" - "net/http" "time" + + "github.com/gorilla/websocket" ) const ( @@ -107,30 +106,3 @@ func (c *connection) writePump() { } } } - -func serveWs(w http.ResponseWriter, r *http.Request) { - if r.Method != "GET" { - http.Error(w, "Method not allowed", 405) - return - } - - // Read request details. - r.ParseForm() - iface := r.FormValue("if") - if iface == "" { - iface = "eth0" - } - - ws, err := websocket.Upgrade(w, r, nil, 1024, 1024) - if _, ok := err.(websocket.HandshakeError); ok { - http.Error(w, "Not a websocket handshake", 400) - return - } else if err != nil { - log.Println(err) - return - } - c := &connection{send: make(chan []byte, 256), ws: ws, iface: iface} - h.register <- c - go c.writePump() - c.readPump() -} diff --git a/src/realtimetraffic/data.go b/realtimetrafficd/data.go similarity index 100% rename from src/realtimetraffic/data.go rename to realtimetrafficd/data.go diff --git a/src/realtimetraffic/grabber.go b/realtimetrafficd/grabber.go similarity index 100% rename from src/realtimetraffic/grabber.go rename to realtimetrafficd/grabber.go diff --git a/src/realtimetraffic/hub.go b/realtimetrafficd/hub.go similarity index 100% rename from src/realtimetraffic/hub.go rename to realtimetrafficd/hub.go diff --git a/src/realtimetraffic/main.go b/realtimetrafficd/main.go similarity index 54% rename from src/realtimetraffic/main.go rename to realtimetrafficd/main.go index 6afafef..3822560 100644 --- a/src/realtimetraffic/main.go +++ b/realtimetrafficd/main.go @@ -20,52 +20,68 @@ import ( "flag" "log" "net/http" - "os" - "path" - "text/template" + + "github.com/longsleep/realtimetraffic/client" + + "github.com/gorilla/websocket" ) -var templates *template.Template +var staticPath *string +var listenAddr *string func serveClient(w http.ResponseWriter, r *http.Request) { - if r.URL.Path != "/" { - http.Error(w, "Not found", 404) - return - } if r.Method != "GET" { http.Error(w, "Method nod allowed", 405) return } - w.Header().Set("Content-Type", "text/html; charset=utf-8") - err := templates.ExecuteTemplate(w, "realtimetraffic.html", nil) - if err != nil { - http.Error(w, err.Error(), http.StatusInternalServerError) + + http.StripPrefix("/", client.HandlerFunc).ServeHTTP(w, r) +} + +func serveWs(w http.ResponseWriter, r *http.Request) { + if r.Method != "GET" { + http.Error(w, "Method not allowed", 405) + return } + + // Read request details. + r.ParseForm() + iface := r.FormValue("if") + if iface == "" { + iface = "eth0" + } + + ws, err := websocket.Upgrade(w, r, nil, 1024, 1024) + if _, ok := err.(websocket.HandshakeError); ok { + http.Error(w, "Not a websocket handshake", 400) + return + } else if err != nil { + log.Println(err) + return + } + c := &connection{send: make(chan []byte, 256), ws: ws, iface: iface} + h.register <- c + go c.writePump() + c.readPump() } func main() { - var err error - cwd, _ := os.Getwd() - client := flag.String("client", path.Join(cwd, "client"), "Full path to client directory.") - addr := flag.String("listen", "127.0.0.1:8088", "Listen address.") - - templates, err = template.ParseGlob(path.Join(*client, "*.html")) - if err != nil { - log.Fatal("Failed to load templates: ", err) - } + staticPath = flag.String("client", "", "Full path to client directory.") + listenAddr = flag.String("listen", "127.0.0.1:8088", "Listen address.") flag.Parse() go h.run() http.HandleFunc("/", serveClient) http.HandleFunc("/realtimetraffic", serveWs) - http.Handle("/css/", http.FileServer(http.Dir(*client))) + /*http.Handle("/css/", http.FileServer(http.Dir(*client))) http.Handle("/scripts/", http.FileServer(http.Dir(*client))) http.Handle("/img/", http.FileServer(http.Dir(*client))) http.Handle("/favicon.ico", http.FileServer(http.Dir(*client))) + */ - err = http.ListenAndServe(*addr, nil) + err = http.ListenAndServe(*listenAddr, nil) if err != nil { log.Fatal("ListenAndServe: ", err) }