From 4f37100b71e347b184fbdc899dd8d2a9eb554888 Mon Sep 17 00:00:00 2001 From: Christine Dodrill Date: Sat, 29 Apr 2017 12:53:08 -0700 Subject: [PATCH] homogenize tooling --- cmd/route-httpagent/.gitignore | 1 + cmd/route/.gitignore | 1 + cmd/route/gopreload.go | 9 ++++++++ cmd/route/gops.go | 13 ++++++++++++ cmd/route/main.go | 39 +++++++++++++++++++++++++++++----- cmd/routed/.gitignore | 1 + 6 files changed, 59 insertions(+), 5 deletions(-) create mode 100644 cmd/route-httpagent/.gitignore create mode 100644 cmd/route/.gitignore create mode 100644 cmd/route/gopreload.go create mode 100644 cmd/route/gops.go create mode 100644 cmd/routed/.gitignore diff --git a/cmd/route-httpagent/.gitignore b/cmd/route-httpagent/.gitignore new file mode 100644 index 0000000..0380c39 --- /dev/null +++ b/cmd/route-httpagent/.gitignore @@ -0,0 +1 @@ +route-httpagent \ No newline at end of file diff --git a/cmd/route/.gitignore b/cmd/route/.gitignore new file mode 100644 index 0000000..6f36346 --- /dev/null +++ b/cmd/route/.gitignore @@ -0,0 +1 @@ +route diff --git a/cmd/route/gopreload.go b/cmd/route/gopreload.go new file mode 100644 index 0000000..6829ae5 --- /dev/null +++ b/cmd/route/gopreload.go @@ -0,0 +1,9 @@ +// gopreload.go +package main + +/* + This file is separate to make it very easy to both add into an application, but + also very easy to remove. +*/ + +import _ "github.com/Xe/gopreload" diff --git a/cmd/route/gops.go b/cmd/route/gops.go new file mode 100644 index 0000000..184b656 --- /dev/null +++ b/cmd/route/gops.go @@ -0,0 +1,13 @@ +package main + +import ( + "log" + + "github.com/google/gops/agent" +) + +func init() { + if err := agent.Listen(nil); err != nil { + log.Fatal(err) + } +} diff --git a/cmd/route/main.go b/cmd/route/main.go index 1e08927..242b218 100644 --- a/cmd/route/main.go +++ b/cmd/route/main.go @@ -5,10 +5,14 @@ import ( "crypto/tls" "encoding/json" "fmt" + "log" + "net/http" "os" "path/filepath" "runtime" + "go.uber.org/atomic" + "git.xeserv.us/xena/route/database" "git.xeserv.us/xena/route/lib/routecrypto" proto "git.xeserv.us/xena/route/proto" @@ -16,6 +20,7 @@ import ( "github.com/Xe/uuid" jwtcreds "github.com/Xe/x/tools/svc/credentials/jwt" "github.com/dickeyxxx/netrc" + "github.com/kr/pretty" "github.com/olekukonko/tablewriter" "google.golang.org/grpc" "google.golang.org/grpc/credentials" @@ -61,6 +66,9 @@ var ( routesRm = routes.Command("rm", "remove a route") routesRmID = routesRm.Flag("id", "route ID to remove").Required().String() + testServer = app.Command("test-server", "spawn a simple HTTP test server on a TCP address") + testServerAddr = testServer.Flag("addr", "TCP address").Default(":9090").String() + token = app.Command("token", "token management") tokenCreate = token.Command("create", "create a new token") @@ -81,13 +89,29 @@ var ( tokenRm = token.Command("rm", "remove a token") tokenRmHard = tokenRm.Flag("hard", "hard-delete the token instead of deactivating it").Default("false").Bool() tokenRmID = tokenRm.Flag("id", "token id").Required().String() - - // testServer = app.Command("test-server", "spawn a simple HTTP test server on a TCP address") - // testServerAddr = testServer.Flag("addr", "TCP address").Default(":9090").String() - - // connect = app.Command("connect", "connect to the backend server and relay client HTTP traffic") ) +var ( + hits *atomic.Int64 +) + +func init() { + hits = atomic.NewInt64(0) +} + +func handle(w http.ResponseWriter, r *http.Request) { + fmt.Fprintln(w, "Route is go!") + fmt.Fprintf(w, "%s\n", pretty.Sprintf("%s", r.Header)) + hn, _ := os.Hostname() + fmt.Fprintf(w, "Served by %s running %s\n", hn, runtime.GOOS) + fmt.Fprintf(w, "Hit count: %d", hits.Inc()) + + ip := r.Header.Get("X-Remote-Ip") + if ip != "" { + log.Printf("Hit from %s: %s", ip, r.RequestURI) + } +} + func main() { cmdline := kingpin.MustParse(app.Parse(os.Args[1:])) @@ -98,6 +122,11 @@ func main() { _ = n switch cmdline { + case "test-server": + http.HandleFunc("/", handle) + + ln.Fatal(ln.F{"err": http.ListenAndServe(*testServerAddr, nil), "action": "test_server"}) + case "generate-key": key, err := routecrypto.GenerateKey() if err != nil { diff --git a/cmd/routed/.gitignore b/cmd/routed/.gitignore new file mode 100644 index 0000000..aecaf60 --- /dev/null +++ b/cmd/routed/.gitignore @@ -0,0 +1 @@ +routed \ No newline at end of file