2017-05-03 06:52:14 +02:00
|
|
|
// Package that contains all api routes of this microservice
|
2017-04-04 23:21:05 +02:00
|
|
|
package http
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"io"
|
|
|
|
"net/http"
|
|
|
|
"os"
|
|
|
|
"text/template"
|
|
|
|
)
|
|
|
|
|
2017-05-15 10:22:24 +02:00
|
|
|
// Path to the svg image template, that shows the availablity or freshness of a given good
|
2017-05-03 06:52:14 +02:00
|
|
|
// with a traffic light food labeling system
|
2017-05-17 21:33:23 +02:00
|
|
|
var GoodAvailabilityTemplate string
|
2017-05-12 10:54:05 +02:00
|
|
|
var GoodFreshnessTemplate string
|
2017-04-04 23:21:05 +02:00
|
|
|
|
2017-05-03 06:52:14 +02:00
|
|
|
// Function to calculate a percent value from a given value and an maximum value
|
|
|
|
func tempPercent(value, max int) int {
|
2017-04-04 23:21:05 +02:00
|
|
|
return value * 100 / max
|
|
|
|
}
|
|
|
|
|
2017-05-03 06:52:14 +02:00
|
|
|
// Function to calculate a partial radius, depending on a percentage value
|
2017-04-04 23:21:05 +02:00
|
|
|
func tempProcessRadius(value, max, radius int) float64 {
|
|
|
|
return (1 - float64(value)/float64(max)) * float64(radius) * 2 * 3.14
|
|
|
|
}
|
|
|
|
|
2017-05-19 15:37:05 +02:00
|
|
|
// Function to get the SVG, that shows the availability with a traffic light food labeling system for a given good
|
2017-04-04 23:21:05 +02:00
|
|
|
func getGoodAvailablitySVG(w http.ResponseWriter, count int) {
|
|
|
|
|
|
|
|
t := template.New("some")
|
2017-05-03 06:52:14 +02:00
|
|
|
t = t.Funcs(template.FuncMap{"procent": tempPercent,
|
2017-04-04 23:21:05 +02:00
|
|
|
"process_radius": tempProcessRadius,
|
|
|
|
})
|
|
|
|
buf := bytes.NewBuffer(nil)
|
2017-05-17 21:33:23 +02:00
|
|
|
f, _ := os.Open(GoodAvailabilityTemplate) // Error handling elided for brevity.
|
|
|
|
io.Copy(buf, f) // Error handling elided for brevity.
|
2017-04-04 23:21:05 +02:00
|
|
|
f.Close()
|
|
|
|
|
|
|
|
s := string(buf.Bytes())
|
|
|
|
t.Parse(s)
|
|
|
|
|
|
|
|
w.Header().Set("Content-Type", "image/svg+xml")
|
|
|
|
t.Execute(w, map[string]interface{}{"Count": count})
|
|
|
|
}
|
2017-05-12 10:54:05 +02:00
|
|
|
|
2017-05-15 10:22:24 +02:00
|
|
|
// Function to get the SVG, that shows the freshness with a traffic light food labeling system for a given good
|
2017-05-12 10:54:05 +02:00
|
|
|
func getGoodFreshnessSVG(w http.ResponseWriter, fresh bool) {
|
|
|
|
|
|
|
|
t := template.New("some")
|
|
|
|
buf := bytes.NewBuffer(nil)
|
|
|
|
f, _ := os.Open(GoodFreshnessTemplate) // Error handling elided for brevity.
|
2017-05-17 21:33:23 +02:00
|
|
|
io.Copy(buf, f) // Error handling elided for brevity.
|
2017-05-12 10:54:05 +02:00
|
|
|
f.Close()
|
|
|
|
|
|
|
|
s := string(buf.Bytes())
|
|
|
|
t.Parse(s)
|
|
|
|
|
|
|
|
w.Header().Set("Content-Type", "image/svg+xml")
|
|
|
|
t.Execute(w, map[string]interface{}{"Fresh": fresh})
|
2017-05-17 21:33:23 +02:00
|
|
|
}
|