From c82a07d3fe7862a44c460c5e225d575dacd34adf Mon Sep 17 00:00:00 2001 From: Martin Geno Date: Mon, 29 May 2017 22:55:38 +0200 Subject: [PATCH] [TASK] extract lib package --- cmd/freifunkmanager/main.go | 8 +++--- config/main.go | 3 +- lib/http/io.go | 32 --------------------- lib/http/io_test.go | 56 ------------------------------------- lib/http/main.go | 11 -------- lib/http/main_test.go | 19 ------------- lib/log/log.go | 31 -------------------- lib/log/log_test.go | 24 ---------------- lib/worker/worker.go | 41 --------------------------- lib/worker/worker_test.go | 24 ---------------- runtime/node.go | 3 +- runtime/nodes.go | 2 +- ssh/execute.go | 3 +- ssh/manager.go | 3 +- ssh/run.go | 3 +- websocket/client.go | 3 +- websocket/server.go | 4 +-- 17 files changed, 14 insertions(+), 256 deletions(-) delete mode 100644 lib/http/io.go delete mode 100644 lib/http/io_test.go delete mode 100644 lib/http/main.go delete mode 100644 lib/http/main_test.go delete mode 100644 lib/log/log.go delete mode 100644 lib/log/log_test.go delete mode 100644 lib/worker/worker.go delete mode 100644 lib/worker/worker_test.go diff --git a/cmd/freifunkmanager/main.go b/cmd/freifunkmanager/main.go index a32a6c6..39ab17b 100644 --- a/cmd/freifunkmanager/main.go +++ b/cmd/freifunkmanager/main.go @@ -8,17 +8,17 @@ import ( "syscall" "time" + yanic "github.com/FreifunkBremen/yanic/database/socket/client" runtimeYanic "github.com/FreifunkBremen/yanic/runtime" "github.com/NYTimes/gziphandler" + httpLib "github.com/genofire/golang-lib/http" + "github.com/genofire/golang-lib/log" + "github.com/genofire/golang-lib/worker" configPackage "github.com/FreifunkBremen/freifunkmanager/config" - httpLib "github.com/FreifunkBremen/freifunkmanager/lib/http" - "github.com/FreifunkBremen/freifunkmanager/lib/log" - "github.com/FreifunkBremen/freifunkmanager/lib/worker" "github.com/FreifunkBremen/freifunkmanager/runtime" "github.com/FreifunkBremen/freifunkmanager/ssh" "github.com/FreifunkBremen/freifunkmanager/websocket" - yanic "github.com/FreifunkBremen/yanic/database/socket/client" ) var ( diff --git a/config/main.go b/config/main.go index 3036320..153607f 100644 --- a/config/main.go +++ b/config/main.go @@ -4,8 +4,7 @@ import ( "io/ioutil" "github.com/BurntSushi/toml" - - "github.com/FreifunkBremen/freifunkmanager/lib/log" + "github.com/genofire/golang-lib/log" ) //config file of this daemon (for more the config_example.conf in git repository) diff --git a/lib/http/io.go b/lib/http/io.go deleted file mode 100644 index b9d5514..0000000 --- a/lib/http/io.go +++ /dev/null @@ -1,32 +0,0 @@ -// Package http provides the -// logic of the webserver -package http - -import ( - "encoding/json" - "errors" - "net/http" -) - -// Function to read data from a request via json format -// Input: pointer to http request r, interface to -func Read(r *http.Request, to interface{}) (err error) { - if r.Header.Get("Content-Type") != "application/json" { - err = errors.New("no json data recived") - return - } - err = json.NewDecoder(r.Body).Decode(to) - return -} - -// Function to write data as json to a http output -// Input: http response writer w, interface data -func Write(w http.ResponseWriter, data interface{}) { - js, err := json.Marshal(data) - if err != nil { - http.Error(w, "failed to encode response: "+err.Error(), http.StatusInternalServerError) - return - } - w.Header().Set("Content-Type", "application/json") - w.Write(js) -} diff --git a/lib/http/io_test.go b/lib/http/io_test.go deleted file mode 100644 index 3fd276e..0000000 --- a/lib/http/io_test.go +++ /dev/null @@ -1,56 +0,0 @@ -// Package http provides the -// logic of the webserver -package http - -import ( - "bytes" - "net/http" - "net/http/httptest" - "strings" - "testing" - - "github.com/stretchr/testify/assert" -) - -// Function to test the writing into a http response -// Input: pointer to testing object -func TestWrite(t *testing.T) { - assert := assert.New(t) - - w := httptest.NewRecorder() - from := map[string]string{"a": "b"} - Write(w, from) - result := w.Result() - - assert.Equal([]string{"application/json"}, result.Header["Content-Type"], "no header information") - buf := new(bytes.Buffer) - buf.ReadFrom(result.Body) - to := buf.String() - assert.Equal("{\"a\":\"b\"}", to, "wrong content") - - w = httptest.NewRecorder() - value := make(chan int) - Write(w, value) - result = w.Result() - - assert.Equal(http.StatusInternalServerError, result.StatusCode, "wrong statuscode") - -} - -// Function to test the reading from a http response -// Input: pointer to testing object -func TestRead(t *testing.T) { - assert := assert.New(t) - - to := make(map[string]string) - r, _ := http.NewRequest("GET", "/a", strings.NewReader("{\"a\":\"b\"}")) - - r.Header["Content-Type"] = []string{"application/json"} - err := Read(r, &to) - assert.NoError(err, "no error") - assert.Equal(map[string]string{"a": "b"}, to, "wrong content") - - r.Header["Content-Type"] = []string{""} - err = Read(r, &to) - assert.Error(err, "no error") -} diff --git a/lib/http/main.go b/lib/http/main.go deleted file mode 100644 index 58d5503..0000000 --- a/lib/http/main.go +++ /dev/null @@ -1,11 +0,0 @@ -package http - -import "net/http" - -func GetRemoteIP(r *http.Request) string { - ip := r.Header.Get("X-Forwarded-For") - if len(ip) <= 1 { - ip = r.RemoteAddr - } - return ip -} diff --git a/lib/http/main_test.go b/lib/http/main_test.go deleted file mode 100644 index 91034b7..0000000 --- a/lib/http/main_test.go +++ /dev/null @@ -1,19 +0,0 @@ -package http - -import ( - "net/http" - "testing" - - "github.com/stretchr/testify/assert" -) - -// Function to test the logging -// Input: pointer to teh testing object -func TestGetIP(t *testing.T) { - assertion := assert.New(t) - - req, _ := http.NewRequest("GET", "https://google.com/lola/duda?q=wasd", nil) - ip := GetRemoteIP(req) - - assertion.Equal("", ip, "no remote ip address") -} diff --git a/lib/log/log.go b/lib/log/log.go deleted file mode 100644 index e75651e..0000000 --- a/lib/log/log.go +++ /dev/null @@ -1,31 +0,0 @@ -// Package log provides the -// functionality to start und initialize to logger -package log - -import ( - "log" - "net/http" - - logger "github.com/Sirupsen/logrus" - - httpLib "github.com/FreifunkBremen/freifunkmanager/lib/http" -) - -// current logger with configuration -var Log *logger.Logger - -// Function to initiate a new logger -func init() { - Log = logger.New() - log.SetOutput(Log.Writer()) // Enable fallback if core logger -} - -// Function to add the information of a http request to the log -// Input: pointer to the http request r -func HTTP(r *http.Request) *logger.Entry { - return Log.WithFields(logger.Fields{ - "remote": httpLib.GetRemoteIP(r), - "method": r.Method, - "url": r.URL.RequestURI(), - }) -} diff --git a/lib/log/log_test.go b/lib/log/log_test.go deleted file mode 100644 index fc7cdc9..0000000 --- a/lib/log/log_test.go +++ /dev/null @@ -1,24 +0,0 @@ -// Package log provides the -// functionality to start und initialize to logger -package log - -import ( - "net/http" - "testing" - - "github.com/stretchr/testify/assert" -) - -// Function to test the logging -// Input: pointer to teh testing object -func TestLog(t *testing.T) { - assertion := assert.New(t) - - req, _ := http.NewRequest("GET", "https://google.com/lola/duda?q=wasd", nil) - log := HTTP(req) - _, ok := log.Data["remote"] - - assertion.NotNil(ok, "remote address not set in logger") - assertion.Equal("GET", log.Data["method"], "method not set in logger") - assertion.Equal("/lola/duda?q=wasd", log.Data["url"], "path not set in logger") -} diff --git a/lib/worker/worker.go b/lib/worker/worker.go deleted file mode 100644 index 286f90d..0000000 --- a/lib/worker/worker.go +++ /dev/null @@ -1,41 +0,0 @@ -// A little lib for cronjobs to run it in background -package worker - -import "time" - -// a struct which handle the job -type Worker struct { - every time.Duration - run func() - quit chan struct{} -} - -// create a new Worker with timestamp run every and his function -func NewWorker(every time.Duration, f func()) (w *Worker) { - w = &Worker{ - every: every, - run: f, - quit: make(chan struct{}), - } - return -} - -// start the worker -// please us it as a goroutine: go w.Start() -func (w *Worker) Start() { - ticker := time.NewTicker(w.every) - for { - select { - case <-ticker.C: - w.run() - case <-w.quit: - ticker.Stop() - return - } - } -} - -// stop the worker -func (w *Worker) Close() { - close(w.quit) -} diff --git a/lib/worker/worker_test.go b/lib/worker/worker_test.go deleted file mode 100644 index c72f47d..0000000 --- a/lib/worker/worker_test.go +++ /dev/null @@ -1,24 +0,0 @@ -package worker - -import ( - "testing" - "time" - - "github.com/stretchr/testify/assert" -) - -func TestWorker(t *testing.T) { - assert := assert.New(t) - - runtime := 0 - - w := NewWorker(time.Duration(5)*time.Millisecond, func() { - runtime = runtime + 1 - }) - go w.Start() - time.Sleep(time.Duration(18) * time.Millisecond) - w.Close() - - assert.Equal(3, runtime) - time.Sleep(time.Duration(8) * time.Millisecond) -} diff --git a/runtime/node.go b/runtime/node.go index d779a75..d6bc624 100644 --- a/runtime/node.go +++ b/runtime/node.go @@ -5,10 +5,11 @@ import ( "fmt" "net" - "github.com/FreifunkBremen/freifunkmanager/ssh" "github.com/FreifunkBremen/yanic/data" "github.com/FreifunkBremen/yanic/jsontime" yanicRuntime "github.com/FreifunkBremen/yanic/runtime" + + "github.com/FreifunkBremen/freifunkmanager/ssh" ) const ( diff --git a/runtime/nodes.go b/runtime/nodes.go index 9e2014b..1bd13a2 100644 --- a/runtime/nodes.go +++ b/runtime/nodes.go @@ -7,8 +7,8 @@ import ( "github.com/FreifunkBremen/yanic/jsontime" yanic "github.com/FreifunkBremen/yanic/runtime" + "github.com/genofire/golang-lib/log" - "github.com/FreifunkBremen/freifunkmanager/lib/log" "github.com/FreifunkBremen/freifunkmanager/ssh" ) diff --git a/ssh/execute.go b/ssh/execute.go index 310137c..041d7cd 100644 --- a/ssh/execute.go +++ b/ssh/execute.go @@ -3,9 +3,8 @@ package ssh import ( "net" + "github.com/genofire/golang-lib/log" "golang.org/x/crypto/ssh" - - "github.com/FreifunkBremen/freifunkmanager/lib/log" ) func (m *Manager) ExecuteEverywhere(cmd string) { diff --git a/ssh/manager.go b/ssh/manager.go index 162cdcb..6a46e19 100644 --- a/ssh/manager.go +++ b/ssh/manager.go @@ -6,9 +6,8 @@ import ( "sync" "time" + "github.com/genofire/golang-lib/log" "golang.org/x/crypto/ssh" - - "github.com/FreifunkBremen/freifunkmanager/lib/log" ) // the SSH Connection Manager for multiple connections diff --git a/ssh/run.go b/ssh/run.go index b5f430c..eee7229 100644 --- a/ssh/run.go +++ b/ssh/run.go @@ -6,9 +6,8 @@ import ( "io" "net" + "github.com/genofire/golang-lib/log" "golang.org/x/crypto/ssh" - - "github.com/FreifunkBremen/freifunkmanager/lib/log" ) type SSHResultHandler func([]byte, error) diff --git a/websocket/client.go b/websocket/client.go index 004c024..c9e3272 100644 --- a/websocket/client.go +++ b/websocket/client.go @@ -3,9 +3,8 @@ package websocket import ( "io" + "github.com/genofire/golang-lib/log" "golang.org/x/net/websocket" - - "github.com/FreifunkBremen/freifunkmanager/lib/log" ) const channelBufSize = 100 diff --git a/websocket/server.go b/websocket/server.go index adac732..95347be 100644 --- a/websocket/server.go +++ b/websocket/server.go @@ -5,10 +5,10 @@ import ( "sync" runtimeYanic "github.com/FreifunkBremen/yanic/runtime" + httpLib "github.com/genofire/golang-lib/http" + "github.com/genofire/golang-lib/log" "golang.org/x/net/websocket" - httpLib "github.com/FreifunkBremen/freifunkmanager/lib/http" - "github.com/FreifunkBremen/freifunkmanager/lib/log" "github.com/FreifunkBremen/freifunkmanager/runtime" )