[TASK] some basics
This commit is contained in:
		
							parent
							
								
									597c2b5610
								
							
						
					
					
						commit
						4ae2c3a826
					
				| 
						 | 
				
			
			@ -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:
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -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)
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -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")
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			@ -0,0 +1 @@
 | 
			
		|||
webserver_bind = "[::1]:8080"
 | 
			
		||||
| 
						 | 
				
			
			@ -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)
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			@ -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")
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			@ -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)
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			@ -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)
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			@ -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
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			@ -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)
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			@ -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
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue