some little fixes + drone ci
This commit is contained in:
parent
3b0f69dc46
commit
6b332f7254
|
@ -0,0 +1,19 @@
|
||||||
|
pipeline:
|
||||||
|
test:
|
||||||
|
image: golang:latest
|
||||||
|
commands:
|
||||||
|
- go get -d -t ./...
|
||||||
|
- go get -u github.com/mattn/goveralls
|
||||||
|
- go get -u golang.org/x/tools/cmd/cover
|
||||||
|
- ./.test-coverage
|
||||||
|
codestyle:
|
||||||
|
image: golang:latest
|
||||||
|
commands:
|
||||||
|
- go get github.com/client9/misspell/cmd/misspell
|
||||||
|
- misspell -error .
|
||||||
|
- if [ -n "$(gofmt -s -l .)" ]; then echo "Go code is not formatted, run 'gofmt -s -w .'" >&2; exit 1; fi
|
||||||
|
test-race:
|
||||||
|
image: golang:latest
|
||||||
|
commands:
|
||||||
|
- go get -d -t ./...
|
||||||
|
- go test -v -cover --race ./...
|
|
@ -1,2 +1,7 @@
|
||||||
# golang-lib [![Build Status](https://travis-ci.org/genofire/golang-lib.svg?branch=master)](https://travis-ci.org/genofire/golang-lib) [![CircleCI](https://circleci.com/gh/genofire/golang-lib/tree/master.svg?style=shield)](https://circleci.com/gh/genofire/golang-lib/tree/master) [![Coverage Status](https://coveralls.io/repos/github/genofire/golang-lib/badge.svg?branch=master)](https://coveralls.io/github/genofire/golang-lib?branch=master) [![Go Report Card](https://goreportcard.com/badge/dev.sum7.eu/genofire/golang-lib)](https://goreportcard.com/report/dev.sum7.eu/genofire/golang-lib) [![GoDoc](https://godoc.org/dev.sum7.eu/genofire/golang-lib?status.svg)](https://godoc.org/dev.sum7.eu/genofire/golang-lib)
|
# golang-lib
|
||||||
|
[![DroneCI](https://ci.sum7.eu/api/badges/genofire/golang-lib/status.svg?branch=master)](https://ci.sum7.eu/genofire/golang-lib)
|
||||||
|
[![Travis](https://travis-ci.org/genofire/golang-lib.svg?branch=master)](https://travis-ci.org/genofire/golang-lib) [![CircleCI](https://circleci.com/gh/genofire/golang-lib/tree/master.svg?style=shield)](https://circleci.com/gh/genofire/golang-lib/tree/master)
|
||||||
|
[![Coverage](https://coveralls.io/repos/github/genofire/golang-lib/badge.svg?branch=master)](https://coveralls.io/github/genofire/golang-lib?branch=master)
|
||||||
|
[![Go Report Card](https://goreportcard.com/badge/dev.sum7.eu/genofire/golang-lib)](https://goreportcard.com/report/dev.sum7.eu/genofire/golang-lib)
|
||||||
|
[![GoDoc](https://godoc.org/dev.sum7.eu/genofire/golang-lib?status.svg)](https://godoc.org/dev.sum7.eu/genofire/golang-lib)
|
||||||
some packages collected for easy and often used functions
|
some packages collected for easy and often used functions
|
||||||
|
|
13
file/main.go
13
file/main.go
|
@ -15,8 +15,7 @@ func ReadTOML(path string, data interface{}) error {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
err = toml.Unmarshal(file, data)
|
if err := toml.Unmarshal(file, data); err != nil {
|
||||||
if err != nil {
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -30,8 +29,7 @@ func ReadJSON(path string, data interface{}) error {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
err = json.NewDecoder(file).Decode(data)
|
if err := json.NewDecoder(file).Decode(data); err != nil {
|
||||||
if err != nil {
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -42,17 +40,16 @@ func ReadJSON(path string, data interface{}) error {
|
||||||
func SaveJSON(outputFile string, data interface{}) error {
|
func SaveJSON(outputFile string, data interface{}) error {
|
||||||
tmpFile := outputFile + ".tmp"
|
tmpFile := outputFile + ".tmp"
|
||||||
|
|
||||||
f, err := os.OpenFile(tmpFile, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644)
|
file, err := os.OpenFile(tmpFile, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
err = json.NewEncoder(f).Encode(data)
|
if err := json.NewEncoder(file).Encode(data); err != nil {
|
||||||
if err != nil {
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
f.Close()
|
file.Close()
|
||||||
if err := os.Rename(tmpFile, outputFile); err != nil {
|
if err := os.Rename(tmpFile, outputFile); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
|
@ -10,6 +10,6 @@ func NewSaveJSONWorker(repeat time.Duration, path string, data interface{}) *wor
|
||||||
saveWorker := worker.NewWorker(repeat, func() {
|
saveWorker := worker.NewWorker(repeat, func() {
|
||||||
SaveJSON(path, data)
|
SaveJSON(path, data)
|
||||||
})
|
})
|
||||||
go saveWorker.Start()
|
saveWorker.Start()
|
||||||
return saveWorker
|
return saveWorker
|
||||||
}
|
}
|
||||||
|
|
|
@ -11,7 +11,7 @@ import (
|
||||||
// Function to read data from a http request via json format (input)
|
// Function to read data from a http request via json format (input)
|
||||||
func Read(r *http.Request, to interface{}) (err error) {
|
func Read(r *http.Request, to interface{}) (err error) {
|
||||||
if !strings.Contains(r.Header.Get("Content-Type"), "application/json") {
|
if !strings.Contains(r.Header.Get("Content-Type"), "application/json") {
|
||||||
err = errors.New("no json request recieved")
|
err = errors.New("no json request received")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
err = json.NewDecoder(r.Body).Decode(to)
|
err = json.NewDecoder(r.Body).Decode(to)
|
||||||
|
|
|
@ -8,7 +8,6 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
// Function to test the logging
|
// Function to test the logging
|
||||||
// Input: pointer to teh testing object
|
|
||||||
func TestGetIP(t *testing.T) {
|
func TestGetIP(t *testing.T) {
|
||||||
assertion := assert.New(t)
|
assertion := assert.New(t)
|
||||||
|
|
||||||
|
|
|
@ -76,6 +76,8 @@ func (s *SessionManager) Remove(c *Client) (client bool, session bool) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *SessionManager) Send(id uuid.UUID, msg *Message) {
|
func (s *SessionManager) Send(id uuid.UUID, msg *Message) {
|
||||||
|
s.Lock()
|
||||||
|
defer s.Unlock()
|
||||||
clients := s.sessionToClient[id]
|
clients := s.sessionToClient[id]
|
||||||
for _, c := range clients {
|
for _, c := range clients {
|
||||||
c.Write(msg)
|
c.Write(msg)
|
||||||
|
|
|
@ -28,17 +28,19 @@ func NewWorker(every time.Duration, f func()) (w *Worker) {
|
||||||
// (please us it as a go routine with go w.Start())
|
// (please us it as a go routine with go w.Start())
|
||||||
func (w *Worker) Start() {
|
func (w *Worker) Start() {
|
||||||
w.wg.Add(1)
|
w.wg.Add(1)
|
||||||
ticker := time.NewTicker(w.every)
|
go func() {
|
||||||
for {
|
defer w.wg.Done()
|
||||||
select {
|
ticker := time.NewTicker(w.every)
|
||||||
case <-ticker.C:
|
for {
|
||||||
w.run()
|
select {
|
||||||
case <-w.quit:
|
case <-ticker.C:
|
||||||
ticker.Stop()
|
w.run()
|
||||||
w.wg.Done()
|
case <-w.quit:
|
||||||
return
|
ticker.Stop()
|
||||||
|
return
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}()
|
||||||
}
|
}
|
||||||
|
|
||||||
// Function to stop the Worker
|
// Function to stop the Worker
|
||||||
|
|
|
@ -15,9 +15,9 @@ func TestWorker(t *testing.T) {
|
||||||
runtime := 0
|
runtime := 0
|
||||||
|
|
||||||
w := NewWorker(time.Duration(5)*time.Millisecond, func() {
|
w := NewWorker(time.Duration(5)*time.Millisecond, func() {
|
||||||
runtime = runtime + 1
|
runtime++
|
||||||
})
|
})
|
||||||
go w.Start()
|
w.Start()
|
||||||
time.Sleep(time.Duration(18) * time.Millisecond)
|
time.Sleep(time.Duration(18) * time.Millisecond)
|
||||||
w.Close()
|
w.Close()
|
||||||
time.Sleep(time.Duration(18) * time.Millisecond)
|
time.Sleep(time.Duration(18) * time.Millisecond)
|
||||||
|
|
Loading…
Reference in New Issue