[TASK] extract lib package

This commit is contained in:
Martin Geno 2017-05-29 22:55:38 +02:00
parent 376afc7494
commit c82a07d3fe
No known key found for this signature in database
GPG Key ID: F0D39A37E925E941
17 changed files with 14 additions and 256 deletions

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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