-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
201 lines (169 loc) · 4.19 KB
/
main.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
package main
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"strings"
)
var s = ""
type XYZ struct {
X float64 `json:"x"`
Y float64 `json:"y"`
Z float64 `json:"z"`
}
type CDL struct {
Uid string `json:"uid"`
Name string `json:"name"`
Path string `json:"path"`
Slope XYZ `json:"slope"`
Offset XYZ `json:"offset"`
Power XYZ `json:"power"`
Saturation float64 `json:"saturation"`
}
type CDLPost struct {
Uid string `json:"uid"`
Name string `json:"name"`
Slope XYZ `json:"slope"`
Offset XYZ `json:"offset"`
Power XYZ `json:"power"`
Saturation float64 `json:"saturation"`
}
type OldCDL struct {
Uid string `json:"uid"`
Slope XYZ `json:"slope"`
Offset XYZ `json:"offset"`
Power XYZ `json:"power"`
Saturation float64 `json:"saturation"`
}
type CDLs struct {
Status map[string]string `json:"status"`
Result []CDL `json:"result"`
}
type uid struct {
Uid string `json:"uid"`
}
type uids struct {
Uids []uid `json:"uids"`
}
func getCDLs(w http.ResponseWriter, req *http.Request) {
resp, err := http.Get(fmt.Sprintf("http://%s/api/session/colour/cdls", s))
if err != nil {
log.Fatalln(err)
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Fatalln(err)
}
//Convert the body to type string
var dat CDLs
json.Unmarshal(body, &dat)
//x := make(map[string][]string)
//dat["result"]
uids := make([]uid, len(dat.Result))
for i := 0; i < len(dat.Result); i++ {
uids[i] = uid{Uid: dat.Result[i].Uid}
}
x := make(map[string][]uid)
x["uids"] = uids
t, err := json.Marshal(x)
if err != nil {
log.Fatalln(err)
}
log.Printf("%s", t)
//w.WriteHeader(200)
w.Header().Set("Content-Type", "application/json; charset=utf-8")
w.Write(t)
}
func getCDL(w http.ResponseWriter, req *http.Request) {
id := strings.TrimPrefix(req.URL.Path, "/api/v1/colour/cdl/get/")
resp, err := http.Get(fmt.Sprintf("http://%s/api/session/colour/cdls", s))
if err != nil {
log.Fatalln(err)
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Fatalln(err)
}
var dat CDLs
json.Unmarshal(body, &dat)
var rp []byte
x := make(map[string]CDL)
for i := 0; i < len(dat.Result); i++ {
if dat.Result[i].Uid == id {
dat.Result[i].Path = fmt.Sprintf("objects/cdl/%s%s", dat.Result[i].Name, ".apx")
x["cdl"] = dat.Result[i]
rp, err = json.Marshal(x)
if err != nil {
log.Fatalln(err)
}
}
}
log.Printf("%s", rp)
//Convert the body to type string
//w.WriteHeader(200)
w.Header().Set("Content-Type", "application/json; charset=utf-8")
w.Write(rp)
}
func setCDL(w http.ResponseWriter, req *http.Request) {
body, err := ioutil.ReadAll(req.Body)
if err != nil {
log.Fatalln(err)
}
var dat OldCDL
json.Unmarshal(body, &dat)
resp, err := http.Get(fmt.Sprintf("http://%s/api/session/colour/cdls", s))
if err != nil {
log.Fatalln(err)
}
bd2, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Fatalln(err)
}
var cdls CDLs
json.Unmarshal(bd2, &cdls)
var updatedCDL CDLPost
for i := 0; i < len(cdls.Result); i++ {
if cdls.Result[i].Uid == dat.Uid {
//updatedCDL.Name = cdls.Result[i].Name
updatedCDL.Uid = cdls.Result[i].Uid
updatedCDL.Name = cdls.Result[i].Name
break
}
}
updatedCDL.Offset = dat.Offset
updatedCDL.Power = dat.Power
updatedCDL.Saturation = dat.Saturation
updatedCDL.Slope = dat.Slope
x := make(map[string]CDLPost)
var rp []byte
x["cdl"] = updatedCDL
rp, err = json.Marshal(x)
if err != nil {
log.Fatalln(err)
}
log.Printf("%s", rp)
resp3, err := http.Post(fmt.Sprintf("http://%s/api/session/colour/cdl", s), "application/json", bytes.NewBuffer(rp))
if err != nil {
log.Fatalln(err)
}
bd3, err := ioutil.ReadAll(resp3.Body)
if err != nil {
log.Fatalln(err)
}
log.Printf("%s", bd3)
w.Header().Set("Content-Type", "application/json; charset=utf-8")
w.Write([]byte("{}"))
}
func main() {
s = os.Args[1]
log.Printf("Starting server on %s", s)
http.HandleFunc("/api/v1/colour/cdl/list", getCDLs)
http.HandleFunc("/api/v1/colour/cdl/get/", getCDL)
http.HandleFunc("/api/v1/colour/cdl/set", setCDL)
//http.HandleFunc("/", getCDL)
http.ListenAndServe(":8008", nil)
}