-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcamsizes.go
118 lines (100 loc) · 2.1 KB
/
camsizes.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
package main
import (
"encoding/json"
"io/ioutil"
"fmt"
"os"
"html/template"
"net/http"
)
type Brand struct {
Name string
Products []Product
}
type Product struct {
Name string
Sizes []Size
}
type Size struct {
Name, Color string
Start, End float32
}
func (s Size) GetWidth() int {
return int(s.End) - int(s.Start)
}
type Brands struct {
Brands []Brand
}
func (b Brands) StepSize() float32 {
return 10
}
func (b Brands) MaxSize() float32 {
var max float32
for _, bb := range b.Brands {
for _, p := range bb.Products {
for _, s := range p.Sizes {
if s.End > max {max = s.End}
}
}
}
return max
}
var VerticalPos float32
func main() {
http.HandleFunc("/", handle)
http.ListenAndServe(":8080", nil)
}
func handle(w http.ResponseWriter, r *http.Request){
file, e := ioutil.ReadFile("./static/cams.json")
if e!= nil {
fmt.Printf("Error reading file: %v\n", e)
return
}
var jsontype Brands
e = json.Unmarshal(file, &jsontype)
if e != nil {
fmt.Printf("Error unmarschal file: %v\n", e)
return
}
funcMap := template.FuncMap{
"add": func(a, b float32) float32 { return a+b },
"verticalAdd": func(a float32) float32 {
VerticalPos = VerticalPos + a
return VerticalPos
},
"verticalReset": func() float32 {
VerticalPos = float32(0)
return VerticalPos
},
"verticalResetAdd": func(a float32) float32 {
VerticalPos = float32(0)+a
return VerticalPos
},
"sizeSteps": sizeSteps,
"pasteJS": func() template.JS {
js, e := ioutil.ReadFile("./static/cams.js")
if e!= nil {
fmt.Printf("Error reading JS file: %v\n", e)
os.Exit(1)
}
return template.JS(js)
},
}
t, e := template.New("cams.tmpl").Funcs(funcMap).ParseFiles("./template/cams.tmpl")
if e != nil {
fmt.Printf("Error parsing template file: %v\n", e)
return
}
e = t.ExecuteTemplate(w, "root", jsontype)
if e != nil {
fmt.Printf("Error executing template file: %v\n", e)
return
}
}
func sizeSteps(limit float32, stepsSize float32) []float32 {
var steps []float32
for i := float32(0.0); i<=limit+stepsSize; i = i+stepsSize {
steps = append(steps, i)
}
return steps
}