-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstore.go
106 lines (93 loc) · 2.08 KB
/
store.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
package main
import (
"encoding/json"
"fmt"
"math/rand"
"os"
"sync"
"time"
)
var Store *EventStore
func init() {
Store = &EventStore{
Events: make(map[string]Event),
Responses: make(map[string][]Response),
mtx: &sync.Mutex{},
}
go SaveData()
f, err := os.Open("events.json")
if err != nil {
return
}
json.NewDecoder(f).Decode(&Store.Events)
fmt.Println(Store.Events)
r, err := os.Open("responses.json")
if err != nil {
return
}
json.NewDecoder(r).Decode(&Store.Responses)
}
func SaveData() {
for tick := range time.Tick(time.Minute) {
fmt.Println("Save Data:", tick)
f, err := os.Create("events.json")
if err != nil {
return
}
json.NewEncoder(f).Encode(Store.Events)
r, err := os.Create("responses.json")
if err != nil {
return
}
json.NewEncoder(r).Encode(Store.Responses)
}
}
type EventStore struct {
Events map[string]Event
Responses map[string][]Response
mtx *sync.Mutex
}
func (s *EventStore) NewEvent(e Event) string {
s.mtx.Lock()
defer s.mtx.Unlock()
k := fmt.Sprint(rand.Int63())
s.Events[k] = e
fmt.Println(s.Events)
return k
}
func (s *EventStore) NewResponse(k string, r Response) {
s.mtx.Lock()
s.Responses[k] = append(s.Responses[k], r)
s.mtx.Unlock()
}
func (s *EventStore) GetEvent(k string) (Event, bool) {
s.mtx.Lock()
defer s.mtx.Unlock()
e, b := s.Events[k]
return e, b
}
func (s *EventStore) GetResponses(k string) []Response {
s.mtx.Lock()
defer s.mtx.Unlock()
return s.Responses[k]
}
type Event struct {
Name string `json:"name"`
EventType string `json:"event_type"`
Date string `json:"date"`
StartTime string `json:"startTime"`
EndTime string `json:"endTime"`
Instructions string `json:"instructions"`
Questions []Question `json:"questions"`
}
type Question struct {
Type string `json:"type"`
Title string `json:"title"`
Required bool `json:"required"`
Key string `json:"key"`
Choices []string `json:"choices"`
}
type Response struct {
Created time.Time `json:"create"`
Answers []string `json:"answers"`
}