-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
212 lines (186 loc) · 3.81 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
202
203
204
205
206
207
208
209
210
211
212
package main
import (
"encoding/json"
"errors"
"io/ioutil"
"net/http"
"os"
"time"
"code.google.com/p/go-uuid/uuid"
"github.com/golang/glog"
)
const (
Breakfast int = iota
Lunch
Dinner
)
type ClientEvent interface {
Valid() error
}
type Event interface {
Handle()
}
type MSI map[string]interface{}
type ClientMeal struct {
Client uuid.UUID `json:"id"`
Location string `json:"location"`
Date time.Time `json:"date"`
Meal int `json:"meal"`
Description string `json:"description"`
}
func ParseMSI(b []byte) (cm ClientMeal, err error) {
var msi MSI
if err := json.Unmarshal(b, &msi); err != nil {
return cm, err
}
cm.Client = uuid.Parse(msi["id"].(string))
cm.Location = msi["location"].(string)
if cm.Date, err = time.Parse(time.RFC3339Nano, msi["date"].(string)); err != nil {
return cm, err
}
switch msi["meal"].(string) {
case "Breakfast":
cm.Meal = Breakfast
case "Lunch":
cm.Meal = Lunch
case "Dinner":
cm.Meal = Dinner
default:
return cm, errors.New("invalid meal")
}
cm.Description = msi["description"].(string)
return cm, nil
}
func ParseClientMeal(b []byte) (cm ClientMeal, err error) {
err = json.Unmarshal(b, &cm)
return cm, err
}
func (cm ClientMeal) Valid() (err error) {
if cm.Client != nil && len(cm.Client) == 16 &&
cm.Location != "" &&
cm.Meal >= Breakfast && cm.Meal <= Dinner &&
cm.Description != "" {
return nil
}
return errors.New("Client Meal Validation Error")
}
func NewClientID(b []byte) (c ClientID) {
if len(b) != 16 {
return c
}
for i := range b {
c[i] = b[i]
}
return c
}
type Meal struct {
TimeStamp time.Time
Client ClientID
Location string
Date time.Time
Meal int
Description string
}
func NewMeal(cm ClientMeal) Meal {
return Meal{
TimeStamp: time.Now(),
Client: NewClientID(cm.Client),
Location: cm.Location,
Date: cm.Date,
Meal: cm.Meal,
Description: cm.Description,
}
}
type Badge struct {
Name string `json:"name"`
}
func RecomputeBadges(c *Client) {
}
func (m Meal) Handle() {
client, ok := clients[m.Client]
if !ok {
return
}
client.meals = append(client.meals, m)
RecomputeBadges(&client)
}
var events = make(chan Event)
func EventHandler() {
for {
e := <-events
e.Handle()
}
}
func WriteEvent(e Event) error {
f, err := os.OpenFile("event.log", os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0666)
if err != nil {
return err
}
defer f.Close()
enc := json.NewEncoder(f)
return enc.Encode(e)
}
type Client struct {
ID ClientID
meals []Meal
badges []Badge
}
type ClientID [16]byte
var clients map[ClientID]Client
func meals(w http.ResponseWriter, r *http.Request) {
body, err := ioutil.ReadAll(r.Body)
if err != nil {
glog.Error(err)
return
}
var cm ClientMeal
if cm, err = ParseClientMeal(body); err != nil {
glog.Error(err)
w.WriteHeader(http.StatusBadRequest)
return
}
if err = cm.Valid(); err != nil {
glog.Error(err)
w.WriteHeader(http.StatusBadRequest)
return
}
m := NewMeal(cm)
WriteEvent(m)
events <- m
}
type GetBadges struct {
ID ClientID
reply chan []Badge
}
func (g GetBadges) Handle() {
var b []Badge
if c, ok := clients[g.ID]; ok {
b = c.badges
}
g.reply <- b
}
func badges(w http.ResponseWriter, r *http.Request) {
uuid := uuid.Parse(r.URL.Path)
reply := make(chan []Badge)
events <- GetBadges{
ID: NewClientID(uuid),
reply: reply,
}
badges := <-reply
b, err := json.Marshal(badges)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
if _, err := w.Write(b); err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
}
func main() {
go EventHandler()
// read and replay events
http.HandleFunc("/meals", meals)
http.Handle("/badges/", http.StripPrefix("/badges/", http.HandlerFunc(badges)))
glog.Fatal(http.ListenAndServe(":8080", nil))
}