genofire/hs_monolith
genofire
/
hs_monolith
Archived
1
0
Fork 0

[TASK] some basics

This commit is contained in:
Martin Geno 2017-03-25 16:09:17 +01:00
parent 597c2b5610
commit 4ae2c3a826
No known key found for this signature in database
GPG Key ID: F0D39A37E925E941
11 changed files with 190 additions and 14 deletions

View File

@ -2,7 +2,7 @@ language: go
go: go:
- tip - tip
install: 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 github.com/mattn/goveralls
- go get golang.org/x/tools/cmd/cover - go get golang.org/x/tools/cmd/cover
script: script:

View File

@ -3,18 +3,36 @@ package main
import ( import (
"flag" "flag"
"log" "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 ( var (
configFile string
config *models.Config
timestamps bool timestamps bool
) )
func main() { func main() {
flag.BoolVar(&timestamps, "timestamps", true, "print timestamps in output") flag.BoolVar(&timestamps, "timestamps", true, "print timestamps in output")
flag.StringVar(&configFile, "config", "config.conf", "path of configuration file (default:config.conf)")
flag.Parse() flag.Parse()
// load config
config = models.ReadConfigFile(configFile)
if !timestamps { if !timestamps {
log.SetFlags(0) 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)
} }

View File

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

1
config_example.conf Normal file
View File

@ -0,0 +1 @@
webserver_bind = "[::1]:8080"

10
http/main.go Normal file
View File

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

11
http/status.go Normal file
View File

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

19
http/status_test.go Normal file
View File

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

28
lib/http.go Normal file
View File

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

27
models/config.go Normal file
View File

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

16
models/config_test.go Normal file
View File

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

58
test/http.go Normal file
View File

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