/
web.go
214 lines (182 loc) · 4.59 KB
/
web.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
202
203
204
205
206
207
208
209
210
211
212
213
214
package main
import (
"encoding/json"
"io/ioutil"
"log"
"math/rand"
"net/http"
"os"
"reflect"
"strconv"
"github.com/go-redis/redis"
"github.com/labstack/echo"
"github.com/labstack/echo/middleware"
)
var (
conf dwrConfig
rClient *redis.Client
)
func _init() {
// Load json config file
if fd, err := os.Open("config.json"); err == nil {
_configJSON, _ = ioutil.ReadAll(fd)
fd.Close()
}
if err := json.Unmarshal(_configJSON, &conf); err != nil {
panic(err)
}
// env takes precedence
redisNode := os.Getenv("REDIS_HOST")
if len(redisNode) > 0 {
conf.Redis.Host = redisNode
}
dbName := os.Getenv("REDIS_DB")
if len(dbName) > 0 {
if i, err := strconv.Atoi(dbName); err == nil {
conf.Redis.DB = i
}
}
rPass := os.Getenv("REDIS_PASS")
if len(rPass) == 0 {
conf.Redis.Password = rPass
}
// log.Printf(conf.Redis.Address)
rClient = redis.NewClient(&redis.Options{
Addr: conf.Redis.Host + ":" + conf.Redis.Port,
Password: conf.Redis.Password, // no password set
DB: conf.Redis.DB, // use default DB
})
log.Println("DWR initialized..")
log.Println("With config ", conf)
}
func runWeb() {
e := echo.New()
// Middleware
e.Use(middleware.Logger())
e.Use(middleware.Recover())
// Routes
e.POST("/:key", addKeyWeights)
e.GET("/:key", getValue)
e.DELETE("/:key", deleteKey)
// Start server
e.Logger.Fatal(e.Start(":80"))
}
// Handlers
func hello(c echo.Context) error {
return c.String(http.StatusOK, "Hello, World!")
}
type weightConstruct struct {
Key string `json:"key"`
Weights map[string]int `json:"weights"`
}
func addKeyWeights(c echo.Context) error {
retString := "Created"
// fmt.Println(c.Request().URL)
key := c.Param("key")
if len(key) == 0 {
c.JSON(http.StatusBadRequest, nil)
}
wm := make(map[string]int)
if err := c.Bind(&wm); err != nil {
return c.JSON(http.StatusNotAcceptable, nil)
}
w := make(weights)
w[key] = new(weightsBundle)
w[key].UW = wm
// Before we computeWeights; lets retrieve existing entry for the same
// key if it exists and try to compare. If same, we can ignore
if val, err := rClient.Get(
conf.Redis.Prefix + key).Result(); err == nil {
rW := make(weights)
if err := json.Unmarshal([]byte(val), &rW); err != nil {
if rW[key].UW != nil {
if reflect.DeepEqual(w[key].UW, rW[key].UW) {
return c.JSON(http.StatusCreated, "Same")
} // else we compute
retString = "Updated"
} else if err == redis.Nil {
// nothing, we compute
retString = "Updated"
} else if err != nil {
panic(err)
}
}
}
// Compute weights
w[key].DW = make(kwA, 0) // Create new DW for population
w[key].ComputeWeights()
// Write to store
if jSr, err := json.Marshal(w); err == nil {
if err := rClient.Set(conf.Redis.Prefix+key, jSr, 0).Err(); err != nil {
return c.JSON(http.StatusInternalServerError, err)
}
} else {
return c.JSON(http.StatusInternalServerError, err)
}
return c.JSON(http.StatusCreated, retString)
}
func getValue(c echo.Context) error { // for lack of a better name
key := c.Param("key")
if len(key) == 0 {
c.JSON(http.StatusBadRequest, nil)
}
var sum int
var retKey string
// retrieve the weights from store
if val, err := rClient.Get(
conf.Redis.Prefix + key).Result(); err == nil {
w := weights{}
if err := json.Unmarshal([]byte(val), &w); err == nil {
wb := w[key]
r := rand.Intn(wb.TW) + 1
for idx, cw := range wb.DW {
sum = sum + cw.Weight
if r <= sum {
wb.DW[idx].Weight--
retKey = wb.DW[idx].Key
wb.TW--
if wb.TW == 0 {
//free(wb.DW)
wb.DW = make(kwA, 0)
wb.ComputeWeights()
}
break
}
}
// We have to push the result back to store
if jSr, err := json.Marshal(w); err == nil {
if err := rClient.Set(
conf.Redis.Prefix+key,
jSr, 0).Err(); err != nil {
return c.JSON(http.StatusInternalServerError, nil)
}
} else {
return c.JSON(http.StatusInternalServerError, nil)
}
} else {
return c.JSON(http.StatusInternalServerError, nil)
}
} else if err == redis.Nil { // we did not find it 404
return c.JSON(http.StatusNotFound, nil)
} else {
panic(err)
//return c.JSON(http.StatusInternalServerError, err)
}
return c.JSON(http.StatusOK, map[string]string{"key": retKey})
}
func deleteKey(c echo.Context) error {
key := c.Param("key")
if len(key) == 0 {
c.JSON(http.StatusBadRequest, nil)
}
// Remove key from store
_, err := rClient.Del(conf.Redis.Prefix + key).Result()
if err != nil {
return c.JSON(http.StatusNotFound, nil)
}
return c.JSON(http.StatusOK, map[string]string{"message": "Deleted"})
}
func main() {
_init()
runWeb()
}