From 4ae2c3a826bdd8b9e413834c200b0e27c9ee9427 Mon Sep 17 00:00:00 2001 From: Martin Geno Date: Sat, 25 Mar 2017 16:09:17 +0100 Subject: [PATCH] [TASK] some basics --- .travis.yml | 2 +- cmd/rezension/main.go | 20 ++++++++++++- cmd/rezension/main_test.go | 12 -------- config_example.conf | 1 + http/main.go | 10 +++++++ http/status.go | 11 ++++++++ http/status_test.go | 19 +++++++++++++ lib/http.go | 28 ++++++++++++++++++ models/config.go | 27 ++++++++++++++++++ models/config_test.go | 16 +++++++++++ test/http.go | 58 ++++++++++++++++++++++++++++++++++++++ 11 files changed, 190 insertions(+), 14 deletions(-) delete mode 100644 cmd/rezension/main_test.go create mode 100644 config_example.conf create mode 100644 http/main.go create mode 100644 http/status.go create mode 100644 http/status_test.go create mode 100644 lib/http.go create mode 100644 models/config.go create mode 100644 models/config_test.go create mode 100644 test/http.go diff --git a/.travis.yml b/.travis.yml index 047aacf..513d89f 100644 --- a/.travis.yml +++ b/.travis.yml @@ -2,7 +2,7 @@ language: go go: - tip install: - - go get -t github.com/genofire/hs_master-kss-monolith... + - go get -t github.com/genofire/hs_master-kss-monolith/... - go get github.com/mattn/goveralls - go get golang.org/x/tools/cmd/cover script: diff --git a/cmd/rezension/main.go b/cmd/rezension/main.go index 0e6e7a5..b66a909 100644 --- a/cmd/rezension/main.go +++ b/cmd/rezension/main.go @@ -3,18 +3,36 @@ package main import ( "flag" "log" + "net/http" + + goji "goji.io" + + http_api "github.com/genofire/hs_master-kss-monolith/http" + "github.com/genofire/hs_master-kss-monolith/models" ) var ( + configFile string + config *models.Config timestamps bool ) func main() { flag.BoolVar(×tamps, "timestamps", true, "print timestamps in output") + flag.StringVar(&configFile, "config", "config.conf", "path of configuration file (default:config.conf)") flag.Parse() + // load config + config = models.ReadConfigFile(configFile) + if !timestamps { log.SetFlags(0) } - log.Println("Startup rezenssion monolith") + + log.Println("Starting rezension monolith") + + // Startwebsrver + router := goji.NewMux() + http_api.BindAPI(router) + http.ListenAndServe(config.WebserverBind, router) } diff --git a/cmd/rezension/main_test.go b/cmd/rezension/main_test.go deleted file mode 100644 index 8554a10..0000000 --- a/cmd/rezension/main_test.go +++ /dev/null @@ -1,12 +0,0 @@ -package main - -import ( - "testing" - - "github.com/stretchr/testify/assert" -) - -func TestNothing(t *testing.T) { - assert := assert.New(t) - assert.True(true, "test testing") -} diff --git a/config_example.conf b/config_example.conf new file mode 100644 index 0000000..fa91879 --- /dev/null +++ b/config_example.conf @@ -0,0 +1 @@ +webserver_bind = "[::1]:8080" diff --git a/http/main.go b/http/main.go new file mode 100644 index 0000000..d7d0eb0 --- /dev/null +++ b/http/main.go @@ -0,0 +1,10 @@ +package http + +import ( + goji "goji.io" + "goji.io/pat" +) + +func BindAPI(router *goji.Mux) { + router.HandleFunc(pat.Get("/api/status"), statusHandler) +} diff --git a/http/status.go b/http/status.go new file mode 100644 index 0000000..2d0345e --- /dev/null +++ b/http/status.go @@ -0,0 +1,11 @@ +package http + +import ( + "net/http" + + "github.com/genofire/hs_master-kss-monolith/lib" +) + +func statusHandler(w http.ResponseWriter, r *http.Request) { + lib.Write(w, "hello world") +} diff --git a/http/status_test.go b/http/status_test.go new file mode 100644 index 0000000..bfa6430 --- /dev/null +++ b/http/status_test.go @@ -0,0 +1,19 @@ +package http + +import ( + "net/http" + "testing" + + "github.com/genofire/hs_master-kss-monolith/test" +) + +func TestStatus(t *testing.T) { + assertion, router := test.Init(t) + BindAPI(router) + session := test.NewSession(router) + + result, w := session.JSONRequest("GET", "/api/status", nil) + assertion.Equal(http.StatusOK, w.StatusCode) + assertion.Equal("hello world", result) + +} diff --git a/lib/http.go b/lib/http.go new file mode 100644 index 0000000..19bc43c --- /dev/null +++ b/lib/http.go @@ -0,0 +1,28 @@ +package lib + +import ( + "encoding/json" + "errors" + "net/http" +) + +// Read is reading data from request with json format +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 +} + +// Write is writing data as json to http output +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/models/config.go b/models/config.go new file mode 100644 index 0000000..2ecbd6d --- /dev/null +++ b/models/config.go @@ -0,0 +1,27 @@ +package models + +import ( + "io/ioutil" + + "github.com/influxdata/toml" +) + +//Config the config File of this daemon +type Config struct { + WebserverBind string +} + +// ReadConfigFile reads a config model from path of a yml file +func ReadConfigFile(path string) *Config { + config := &Config{} + file, err := ioutil.ReadFile(path) + if err != nil { + panic(err) + } + + if err := toml.Unmarshal(file, config); err != nil { + panic(err) + } + + return config +} diff --git a/models/config_test.go b/models/config_test.go new file mode 100644 index 0000000..4dd8b06 --- /dev/null +++ b/models/config_test.go @@ -0,0 +1,16 @@ +package models + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestReadConfig(t *testing.T) { + assert := assert.New(t) + + config := ReadConfigFile("../config_example.conf") + assert.NotNil(config) + + assert.Equal("[::1]:8080", config.WebserverBind) +} diff --git a/test/http.go b/test/http.go new file mode 100644 index 0000000..a79237f --- /dev/null +++ b/test/http.go @@ -0,0 +1,58 @@ +package test + +// Request a easy manager to test REST-API +import ( + "bytes" + "encoding/json" + "net/http" + "net/http/httptest" + "testing" + + "github.com/stretchr/testify/assert" + + goji "goji.io" +) + +//Init to initialisieren a API +func Init(t *testing.T) (assertion *assert.Assertions, router *goji.Mux) { + assertion = assert.New(t) + + router = goji.NewMux() + return +} + +type Request struct { + req *http.Request + cookies []*http.Cookie + router *goji.Mux +} + +// NewSession to get a new easy manager +func NewSession(router *goji.Mux) *Request { + return &Request{router: router} +} + +// JSONRequest send request to router +func (r *Request) JSONRequest(method string, url string, body interface{}) (jsonResult interface{}, res *http.Response) { + jsonObj, _ := json.Marshal(body) + req, _ := http.NewRequest(method, url, bytes.NewReader(jsonObj)) + req.Header.Set("Content-Type", "application/json") + for _, c := range r.cookies { + req.AddCookie(c) + } + + w := httptest.NewRecorder() + r.router.ServeHTTP(w, req) + res = w.Result() + cookies := res.Cookies() + if len(cookies) > 0 { + r.cookies = cookies + } + json.NewDecoder(w.Body).Decode(&jsonResult) + return +} + +// Clean to clean the current session +func (r *Request) Clean() { + r.cookies = nil +}