genofire/hs_monolith
genofire
/
hs_monolith
Archived
1
0
Fork 0

[QS] Comment and document \stock and \http

This commit is contained in:
mlabusch 2017-05-03 06:52:14 +02:00
parent eb6e3c53d9
commit 0b47b3f9c5
12 changed files with 58 additions and 24 deletions

View File

@ -1,4 +1,4 @@
// The cmd binary of the microservice to run // Package that containts the cmd binary of the microservice to run it
package main package main
import ( import (
@ -19,11 +19,13 @@ import (
"github.com/genofire/hs_master-kss-monolith/runtime" "github.com/genofire/hs_master-kss-monolith/runtime"
) )
// Configuration File
var ( var (
configFile string configFile string
config *models.Config config *models.Config
) )
// Function to run this go program
func main() { func main() {
flag.StringVar(&configFile, "config", "config.conf", "path of configuration file (default:config.conf)") flag.StringVar(&configFile, "config", "config.conf", "path of configuration file (default:config.conf)")
flag.Parse() flag.Parse()

View File

@ -1,4 +1,5 @@
<!-- Funktionen: --> <!-- SVG to show the current stock with an traffic light food labeling system -->
<!-- Used functions -->
<!-- process_radius ANZAHL MAXANZAHL RADIUS --> <!-- process_radius ANZAHL MAXANZAHL RADIUS -->
<!-- procent ANZAHL MAXANZAHL --> <!-- procent ANZAHL MAXANZAHL -->
<!-- ex. {procent .Count 10}%--> <!-- ex. {procent .Count 10}%-->

Before

Width:  |  Height:  |  Size: 1.2 KiB

After

Width:  |  Height:  |  Size: 1.3 KiB

View File

@ -0,0 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="GO_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$" />
<orderEntry type="jdk" jdkName="Go 1.8" jdkType="Go SDK" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>

View File

@ -1,4 +1,4 @@
// all api routes of this microservice // Package that contains all api routes of this microservice
package http package http
import ( import (
@ -9,7 +9,7 @@ import (
"github.com/genofire/hs_master-kss-monolith/runtime" "github.com/genofire/hs_master-kss-monolith/runtime"
) )
// bind all API routes to webserver // Function to bind all api routes to the webserver
func BindAPI(router *goji.Mux) { func BindAPI(router *goji.Mux) {
router.HandleFunc(pat.Get("/api/status"), status) router.HandleFunc(pat.Get("/api/status"), status)
router.HandleFunc(pat.Get("/api/good/:productid"), listGoods) router.HandleFunc(pat.Get("/api/good/:productid"), listGoods)

View File

@ -1,3 +1,4 @@
// Package that contains all api routes of this microservice
package http package http
import ( import (
@ -13,12 +14,13 @@ import (
"github.com/genofire/hs_master-kss-monolith/runtime" "github.com/genofire/hs_master-kss-monolith/runtime"
) )
// Function to add goods to the stock
func addGood(w http.ResponseWriter, r *http.Request) { func addGood(w http.ResponseWriter, r *http.Request) {
log := logger.HTTP(r) log := logger.HTTP(r)
id, err := strconv.ParseInt(pat.Param(r, "productid"), 10, 64) id, err := strconv.ParseInt(pat.Param(r, "productid"), 10, 64)
if err != nil { if err != nil {
log.Warn("wrong productid format") log.Warn("false productid format")
http.Error(w, "wrong productid", http.StatusNotAcceptable) http.Error(w, "the product id is false", http.StatusNotAcceptable)
return return
} }
log = log.WithField("productid", id) log = log.WithField("productid", id)
@ -29,8 +31,8 @@ func addGood(w http.ResponseWriter, r *http.Request) {
return return
} }
if !ok { if !ok {
log.Warn("wrong product not found") log.Warn("false product, product not found")
http.Error(w, "wrong product not found", http.StatusNotFound) http.Error(w, "the product was not found", http.StatusNotFound)
return return
} }
@ -41,8 +43,8 @@ func addGood(w http.ResponseWriter, r *http.Request) {
db := database.Write.Create(&obj) db := database.Write.Create(&obj)
if db.Error != nil { if db.Error != nil {
log.Error("database could not write", db.Error) log.Error("database not able to write", db.Error)
http.Error(w, "was not possible to write", http.StatusInternalServerError) http.Error(w, "the product could not be written into the database", http.StatusInternalServerError)
} }
lib.Write(w, &obj) lib.Write(w, &obj)

View File

@ -1,3 +1,4 @@
// Package that contains all api routes of this microservice
package http package http
import ( import (
@ -14,12 +15,13 @@ import (
"github.com/genofire/hs_master-kss-monolith/runtime" "github.com/genofire/hs_master-kss-monolith/runtime"
) )
// Function to list all goods
func listGoods(w http.ResponseWriter, r *http.Request) { func listGoods(w http.ResponseWriter, r *http.Request) {
log := logger.HTTP(r) log := logger.HTTP(r)
id, err := strconv.ParseInt(pat.Param(r, "productid"), 10, 64) id, err := strconv.ParseInt(pat.Param(r, "productid"), 10, 64)
if err != nil { if err != nil {
log.Warn("wrong productid format") log.Warn("wrong productid format")
http.Error(w, "wrong productid", http.StatusNotAcceptable) http.Error(w, "the productid is false", http.StatusNotAcceptable)
return return
} }
log = log.WithField("productid", id) log = log.WithField("productid", id)
@ -27,31 +29,32 @@ func listGoods(w http.ResponseWriter, r *http.Request) {
result := database.Read.Where("product_id = ?", id).Find(&list) result := database.Read.Where("product_id = ?", id).Find(&list)
if result.RowsAffected == 0 { if result.RowsAffected == 0 {
log.Warn("no goods found") log.Warn("no goods found")
http.Error(w, "no goods found", http.StatusNotFound) http.Error(w, "no goods found for this product", http.StatusNotFound)
return return
} }
lib.Write(w, list) lib.Write(w, list)
log.Info("done") log.Info("done")
} }
// Function that counts als available goods for one product
func getGoodAvailablityCount(w http.ResponseWriter, r *http.Request) (int, *logrus.Entry) { func getGoodAvailablityCount(w http.ResponseWriter, r *http.Request) (int, *logrus.Entry) {
log := logger.HTTP(r) log := logger.HTTP(r)
id, err := strconv.ParseInt(pat.Param(r, "productid"), 10, 64) id, err := strconv.ParseInt(pat.Param(r, "productid"), 10, 64)
if err != nil { if err != nil {
log.Warn("wrong productid format") log.Warn("wrong productid format")
http.Error(w, "wrong productid", http.StatusNotAcceptable) http.Error(w, "the product id has a false format", http.StatusNotAcceptable)
return -1, log return -1, log
} }
log = log.WithField("productid", id) log = log.WithField("productid", id)
ok, err := runtime.ProductExists(id) ok, err := runtime.ProductExists(id)
if err != nil { if err != nil {
log.Warn("product could not verified on other microservice") log.Warn("product could not verified on the microservice")
http.Error(w, "product could not verified on other microservice", http.StatusGatewayTimeout) http.Error(w, "the product could not be verified", http.StatusGatewayTimeout)
return -1, log return -1, log
} }
if !ok { if !ok {
log.Warn("product did not exists anymore") log.Warn("product does not exists anymore")
http.Error(w, "product did not exists anymore", http.StatusNotFound) http.Error(w, "the product does not exists anymore", http.StatusNotFound)
return -1, log return -1, log
} }
var count float64 var count float64
@ -59,6 +62,7 @@ func getGoodAvailablityCount(w http.ResponseWriter, r *http.Request) (int, *logr
return int(count), log return int(count), log
} }
// Function that returns the availability of a good
func getGoodAvailablity(w http.ResponseWriter, r *http.Request) { func getGoodAvailablity(w http.ResponseWriter, r *http.Request) {
count, log := getGoodAvailablityCount(w, r) count, log := getGoodAvailablityCount(w, r)
if count < 0 { if count < 0 {

View File

@ -1,3 +1,4 @@
// Package that contains all api routes of this microservice
package http package http
import ( import (
@ -13,6 +14,7 @@ import (
"github.com/genofire/hs_master-kss-monolith/test" "github.com/genofire/hs_master-kss-monolith/test"
) )
// Function to test listGoods()
func TestListGood(t *testing.T) { func TestListGood(t *testing.T) {
assertion, router := test.Init(t) assertion, router := test.Init(t)
@ -37,6 +39,7 @@ func TestListGood(t *testing.T) {
test.Close() test.Close()
} }
// Function to getGoodAvailability() and getGoodAvailabilityCount()
func TestGetGoodAvailable(t *testing.T) { func TestGetGoodAvailable(t *testing.T) {
now := time.Now() now := time.Now()
assertion, router := test.Init(t) assertion, router := test.Init(t)

View File

@ -1,3 +1,4 @@
// Package that contains all api routes of this microservice
package http package http
import ( import (
@ -8,21 +9,25 @@ import (
"text/template" "text/template"
) )
// path to the svg image templaes to show availablity of a given good // Path to the svg image template, that shows the availablity of a given good
// with a traffic light food labeling system
var GoodAvailablityTemplate string var GoodAvailablityTemplate string
func tempProcent(value, max int) int { // Function to calculate a percent value from a given value and an maximum value
func tempPercent(value, max int) int {
return value * 100 / max return value * 100 / max
} }
// Function to calculate a partial radius, depending on a percentage value
func tempProcessRadius(value, max, radius int) float64 { func tempProcessRadius(value, max, radius int) float64 {
return (1 - float64(value)/float64(max)) * float64(radius) * 2 * 3.14 return (1 - float64(value)/float64(max)) * float64(radius) * 2 * 3.14
} }
// Function to get the SVG, that shows the traffic light food labeling system for a given good
func getGoodAvailablitySVG(w http.ResponseWriter, count int) { func getGoodAvailablitySVG(w http.ResponseWriter, count int) {
t := template.New("some") t := template.New("some")
t = t.Funcs(template.FuncMap{"procent": tempProcent, t = t.Funcs(template.FuncMap{"procent": tempPercent,
"process_radius": tempProcessRadius, "process_radius": tempProcessRadius,
}) })
buf := bytes.NewBuffer(nil) buf := bytes.NewBuffer(nil)

View File

@ -1,3 +1,4 @@
// Package that contains all api routes of this microservice
package http package http
import ( import (
@ -6,9 +7,10 @@ import (
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
) )
// Function to test tempPercent() and tempProcessRadius()
func TestTempFuncs(t *testing.T) { func TestTempFuncs(t *testing.T) {
assert := assert.New(t) assert := assert.New(t)
resultInt := tempProcent(3, 9) resultInt := tempPercent(3, 9)
assert.Equal(33, resultInt) assert.Equal(33, resultInt)
// TODO is there a other way to calc this? // TODO is there a other way to calc this?

View File

@ -1,3 +1,4 @@
// Package that contains all api routes of this microservice
package http package http
import ( import (
@ -11,6 +12,7 @@ import (
"github.com/genofire/hs_master-kss-monolith/test" "github.com/genofire/hs_master-kss-monolith/test"
) )
// Function to test addGood()
func TestAddGood(t *testing.T) { func TestAddGood(t *testing.T) {
assertion, router := test.Init(t) assertion, router := test.Init(t)

View File

@ -1,3 +1,4 @@
// Package that contains all api routes of this microservice
package http package http
import ( import (
@ -9,6 +10,7 @@ import (
"github.com/genofire/hs_master-kss-monolith/models" "github.com/genofire/hs_master-kss-monolith/models"
) )
// Function to get the status of the microservice, the database and the goods
func status(w http.ResponseWriter, r *http.Request) { func status(w http.ResponseWriter, r *http.Request) {
log := logger.HTTP(r) log := logger.HTTP(r)
var goods []*models.Good var goods []*models.Good

View File

@ -1,3 +1,4 @@
// Package that contains all api routes of this microservice
package http package http
import ( import (
@ -9,6 +10,7 @@ import (
"github.com/genofire/hs_master-kss-monolith/test" "github.com/genofire/hs_master-kss-monolith/test"
) )
// Function to test status()
func TestStatus(t *testing.T) { func TestStatus(t *testing.T) {
assertion, router := test.Init(t) assertion, router := test.Init(t)