From 80290a8a17a9d6c4485aeb2ad38d4638420ee311 Mon Sep 17 00:00:00 2001 From: Martin Geno Date: Wed, 25 Oct 2017 18:58:54 +0200 Subject: [PATCH] add file (json+toml) --- file/main.go | 60 ++++++++++++++++++++++++++++++++++++++++++++++++ file/worker.go | 15 ++++++++++++ log/main.go | 29 ----------------------- log/main_test.go | 22 ------------------ 4 files changed, 75 insertions(+), 51 deletions(-) create mode 100644 file/main.go create mode 100644 file/worker.go delete mode 100644 log/main.go delete mode 100644 log/main_test.go diff --git a/file/main.go b/file/main.go new file mode 100644 index 0000000..a6c9c11 --- /dev/null +++ b/file/main.go @@ -0,0 +1,60 @@ +package file + +import ( + "encoding/json" + "io/ioutil" + "os" + + "github.com/BurntSushi/toml" +) + +// ReadConfigFile reads a config model from path of a yml file +func ReadTOML(path string, data interface{}) error { + file, err := ioutil.ReadFile(path) + if err != nil { + return err + } + + err = toml.Unmarshal(file, data) + if err != nil { + return err + } + + return nil +} + +// ReadJSON reads a config model from path of a yml file +func ReadJSON(path string, data interface{}) error { + file, err := os.Open(path) + if err != nil { + return err + } + + err = json.NewDecoder(file).Decode(data) + if err != nil { + return err + } + + return nil +} + +// SaveJSON to path +func SaveJSON(outputFile string, data interface{}) error { + tmpFile := outputFile + ".tmp" + + f, err := os.OpenFile(tmpFile, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644) + if err != nil { + return err + } + + err = json.NewEncoder(f).Encode(data) + if err != nil { + return err + } + + f.Close() + if err := os.Rename(tmpFile, outputFile); err != nil { + return err + } + return nil +} diff --git a/file/worker.go b/file/worker.go new file mode 100644 index 0000000..42ed797 --- /dev/null +++ b/file/worker.go @@ -0,0 +1,15 @@ +package file + +import ( + "time" + + "github.com/genofire/golang-lib/worker" +) + +func NewSaveJSONWorker(repeat time.Duration, path string, data interface{}) *worker.Worker { + saveWorker := worker.NewWorker(repeat, func() { + SaveJSON(path, data) + }) + go saveWorker.Start() + return saveWorker +} diff --git a/log/main.go b/log/main.go deleted file mode 100644 index ef8d616..0000000 --- a/log/main.go +++ /dev/null @@ -1,29 +0,0 @@ -// Package that provides the functionality to start und initialize the logger -package log - -import ( - "log" - "net/http" - - logger "github.com/Sirupsen/logrus" - httpLib "github.com/genofire/golang-lib/http" -) - -// Current logger with it's configuration -var Log *logger.Logger - -// Function to initiate a new logger -func init() { - Log = logger.New() - // Enable fallback, if core logger - log.SetOutput(Log.Writer()) -} - -// Function to add the information of a http request to the log -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/log/main_test.go b/log/main_test.go deleted file mode 100644 index a7fb705..0000000 --- a/log/main_test.go +++ /dev/null @@ -1,22 +0,0 @@ -// Package that provides the functionality to start und initialize the logger -package log - -import ( - "net/http" - "testing" - - "github.com/stretchr/testify/assert" -) - -// Function to test the logging -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") -}