-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapi.go
139 lines (112 loc) · 2.83 KB
/
api.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
package main
import (
"encoding/json"
"fmt"
"github.com/gorilla/mux"
"math/rand"
"net/http"
"strconv"
"strings"
)
// Return the data in JSON format. This is the default return method.
func returnJson(obj interface{}, w http.ResponseWriter, h *http.Request) {
// Don't cache json returns. This is to work around ie's weird caching behavior
w.Header().Set("Cache-Control", "no-cache")
// Set the content type to json
w.Header().Set("Content-Type", "application/json")
j, err := json.Marshal(obj)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
fmt.Fprint(w, string(j))
}
// Return all memories
func allMemoryHandler(w http.ResponseWriter, h *http.Request) {
returnJson(brain, w, h)
}
// Return a random memory. This should only consider active memories
func randomMemoryHandler(w http.ResponseWriter, h *http.Request) {
var active_memories []Memory
var i int
for k, v := range brain.Memories {
if v.Active == true {
active_memories = append(active_memories, brain.Memories[k])
i++
}
}
random_id := rand.Intn(i)
m := active_memories[random_id]
returnJson(m, w, h)
}
// Return a specific memory
func getMemoryHandler(w http.ResponseWriter, h *http.Request) {
vars := mux.Vars(h)
id_string := vars["id"]
id, err := strconv.Atoi(id_string)
if err != nil {
fmt.Fprint(w, "Could not parse Id")
}
for _, v := range brain.Memories {
if v.Id == id && v.Active == true {
returnJson(v, w, h)
return
}
}
var blank Memory
returnJson(blank, w, h)
}
// Add a memory
func addMemoryHandler(w http.ResponseWriter, h *http.Request) {
var m Memory
b := json.NewDecoder(h.Body)
b.Decode(&m)
// Default a new memory to active
m.Active = true
memory := brain.Add(m)
returnJson(memory, w, h)
}
func searchMemoryHandler(w http.ResponseWriter, h *http.Request) {
vars := mux.Vars(h)
term := vars["term"]
var results []Memory
for _, v := range brain.Memories {
if strings.Contains(v.Text, term) {
results = append(results, v)
}
}
returnJson(results, w, h)
}
// Change a memory
func changeMemoryHandler(w http.ResponseWriter, h *http.Request) {
var m Memory
b := json.NewDecoder(h.Body)
b.Decode(&m)
// The ID should come from the URL route, not the memory object that was posted
vars := mux.Vars(h)
mem_id_str := vars["id"]
mem_id, err := strconv.Atoi(mem_id_str)
if err != nil {
panic("ID was not valid")
}
m.Id = mem_id
memory := brain.Update(m)
returnJson(memory, w, h)
}
// Remove a memory
func removeMemoryHandler(w http.ResponseWriter, h *http.Request) {
vars := mux.Vars(h)
id_str := vars["id"]
id, err := strconv.Atoi(id_str)
if err != nil {
panic("ID was not an integer")
}
// Find the item to be deleted
var memory Memory
for k, v := range brain.Memories {
if v.Id == id {
v.Active = false
memory = brain.Memories[k]
}
}
returnJson(memory, w, h)
}