Reorganized code and include client in binary

This commit is contained in:
Simon Eisenmann 2017-05-22 14:04:03 +02:00
parent 54db38bac6
commit 4edb3a16b6
22 changed files with 105 additions and 102 deletions

1
.gitignore vendored
View File

@ -1,2 +1,3 @@
/vendor
/bin
/client/bindata.go

View File

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

22
client/client.go Normal file
View File

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

View File

Before

Width:  |  Height:  |  Size: 198 B

After

Width:  |  Height:  |  Size: 198 B

View File

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

View File

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