-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
309 lines (269 loc) · 9.8 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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
package main
import (
"bufio"
"encoding/json"
"fmt"
"log"
"math/rand"
"net/http"
"os"
"strconv"
"github.com/gorilla/mux"
"gorm.io/driver/postgres"
"gorm.io/gorm"
)
type Client struct {
Id string `json:"Id"`
Username string `json:"Username"`
Email string `json:"Email"`
Password string `json:"Password"`
}
type Bottle struct {
Id string `json:"Id"`
Title string `json:"Title"`
Content string `json:"Content"`
}
type Bottle_Tag struct {
Id string `json:"Id"`
BottleId string `json:"BottleId"`
Tag string `json:"tag"`
}
type Incident struct {
Id string `json:"Id"`
ClientId string `json:"ClientId"`
Content string `json:"Content"`
Location string `json:"Location"`
Time string `json:"Time"`
}
type Incident_Type struct {
Id string `json:"Id"`
IncidentId string `json:"IncidentId"`
Type string `json:"Type"`
}
type Message struct {
Id string `json:"Id"`
ClientId string `json:"ClientId"`
Time string `json:"Time"`
Content string `json:"Content"`
ToClientId string `json:"ToClientId"`
}
// let's declare a global Articles array
// that we can then populate in our main function
// to simulate a database
var bottlesIdCounter int
var incidentsIdCounter int
var messagesIdCounter int
var db *gorm.DB
func homePage(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Welcome to the HomePage!")
fmt.Println("Endpoint Hit: homePage")
}
func returnAllBottles(w http.ResponseWriter, r *http.Request) {
fmt.Println("Endpoint Hit: returnAllArticles")
w.Header().Set("Content-Type", "application/json")
var bottles []Bottle
db.Exec("USE ocean;")
db.Raw("SELECT * FROM bottle;").Scan(&bottles);
json.NewEncoder(w).Encode(bottles)
}
func returnBottleById(w http.ResponseWriter, r *http.Request) {
fmt.Println("Endpoint Hit: returnBottleById")
w.Header().Set("Content-Type", "application/json")
params := mux.Vars(r)
var bottle Bottle
db.Exec("USE ocean;")
db.Raw("SELECT * FROM bottle WHERE id=?;", params["id"]).Scan(&bottle);
json.NewEncoder(w).Encode(bottle)
}
func returnRandomBottle(w http.ResponseWriter, r *http.Request) {
fmt.Println("Endpoint Hit: returnRandomBottle")
w.Header().Set("Content-Type", "application/json")
params := mux.Vars(r)
matchingBottles := []Bottle{}
val, ok := params["tag"]
db.Exec("USE ocean;")
if ok {
var suitableBottles []Bottle
db.Raw("SELECT * FROM bottle WHERE Id IN (SELECT bottleid FROM bottle_tag WHERE tag=?);", val).Scan(&suitableBottles)
matchingBottles = append(matchingBottles, suitableBottles...)
} else {
var bottles []Bottle
db.Raw("SELECT * FROM bottle;").Scan(&bottles)
json.NewEncoder(w).Encode(bottles[rand.Intn(len(bottles)+1)])
return
}
min := 0
max := len(matchingBottles)
if max == min {
w.WriteHeader(http.StatusNotFound)
w.Write([]byte("404 - bro there's nothing there"))
return
}
json.NewEncoder(w).Encode(matchingBottles[rand.Intn(max-min+1)])
}
func stringExists(a string, list []string) bool {
for _, b := range list {
if b == a {
return true
}
}
return false
}
func deleteBottleById(w http.ResponseWriter, r *http.Request) {
fmt.Println("Endpoint Hit: deleteBottleById")
w.Header().Set("Content-Type", "application/json")
params := mux.Vars(r)
db.Exec("USE ocean;")
db.Exec("DELETE FROM bottle WHERE id=?;", params["id"])
}
func createBottle(w http.ResponseWriter, r *http.Request) {
fmt.Println("Endpoint Hit: createBottle")
w.Header().Set("Content-Type", "application/json")
var bottle Bottle
json.NewDecoder(r.Body).Decode(&bottle)
bottle.Id = strconv.Itoa(bottlesIdCounter)
bottlesIdCounter++
db.Exec("USE ocean;")
db.Exec("INSERT INTO bottle (Id,Title,Content) VALUES (?,?,?);", bottle.Id, bottle.Title, bottle.Content)
json.NewEncoder(w).Encode(bottle)
}
func loginAuth(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
w.Header().Set("Content-Type", "application/json")
good := `{"Id": 1}`
json.NewEncoder(w).Encode(good);
}
func createIncidentReport(w http.ResponseWriter, r *http.Request) {
fmt.Println("Endpoint Hit: createIncidentReport")
w.Header().Set("Content-Type", "application/json")
var incident Incident
json.NewDecoder(r.Body).Decode(&incident)
incident.Id = strconv.Itoa(incidentsIdCounter)
incidentsIdCounter++
db.Exec("USE ocean;")
db.Exec("INSERT INTO incident (Id,ClientId,Location,Content,Time) VALUES (?,?,?,?,?);", incident.Id, incident.ClientId, incident.Location, incident.Content, incident.Time)
json.NewEncoder(w).Encode(incident)
}
func returnAllIncidents(w http.ResponseWriter, r *http.Request) {
fmt.Println("Endpoint Hit: returnAllIncidents")
w.Header().Set("Content-Type", "application/json")
var incidents []Incident
db.Exec("USE ocean;")
db.Raw("SELECT * FROM incident;").Scan(&incidents)
json.NewEncoder(w).Encode(incidents)
}
func returnIncidentById(w http.ResponseWriter, r *http.Request) {
fmt.Println("Endpoint Hit: returnIncidentById")
w.Header().Set("Content-Type", "application/json")
params := mux.Vars(r)
var incident Incident
db.Exec("USE ocean;")
db.Raw("SELECT * FROM incident WHERE id=?;", params["id"]).Scan(&incident);
json.NewEncoder(w).Encode(incident)
}
func createMessage(w http.ResponseWriter, r *http.Request) {
fmt.Println("Endpoint Hit: createMessage")
w.Header().Set("Content-Type", "application/json")
var message Message
json.NewDecoder(r.Body).Decode(&message)
message.Id = strconv.Itoa(messagesIdCounter)
messagesIdCounter++
db.Exec("USE ocean;")
db.Exec("INSERT INTO message (Id,ClientId,Time,Content,ToClientId) VALUES (?,?,?,?,?);", message.Id, message.ClientId, message.Time, message.Content, message.ToClientId)
json.NewEncoder(w).Encode(message)
}
func returnMessagesByClientId(w http.ResponseWriter, r *http.Request) {
fmt.Println("Endpoint Hit: returnMessageByClientId")
w.Header().Set("Content-Type", "application/json")
params := mux.Vars(r)
var messages []Message
db.Exec("USE ocean;")
db.Raw("SELECT * FROM message WHERE clientid=?;", params["clientid"]).Scan(&messages);
json.NewEncoder(w).Encode(messages)
}
func returnMessagesByToClientId(w http.ResponseWriter, r *http.Request) {
fmt.Println("Endpoint Hit: returnMessageByToClientId")
w.Header().Set("Content-Type", "application/json")
params := mux.Vars(r)
var messages []Message
db.Exec("USE ocean;")
db.Raw("SELECT * FROM message WHERE toclientid=?;", params["toclientid"]).Scan(&messages);
json.NewEncoder(w).Encode(messages)
}
func returnMessageById(w http.ResponseWriter, r *http.Request) {
fmt.Println("Endpoint Hit: returnMessageById")
w.Header().Set("Content-Type", "application/json")
params := mux.Vars(r)
var message Message
db.Exec("USE ocean;")
db.Raw("SELECT * FROM message WHERE id=?;", params["id"]).Scan(&message)
json.NewEncoder(w).Encode(message)
}
func getTags(w http.ResponseWriter, r *http.Request) {
fmt.Println("Endpoint Hit: getTags")
w.Header().Set("Content-Type", "application/json")
params := mux.Vars(r)
var tags []Bottle_Tag
db.Exec("USE ocean;")
db.Raw("SELECT * FROM bottle_tag WHERE bottleid=?;", params["bottleid"]).Scan(&tags)
json.NewEncoder(w).Encode(tags)
}
func assignTag(w http.ResponseWriter, r *http.Request) {
fmt.Println("Endpoint Hit: assignTags")
w.Header().Set("Content-Type", "application/json")
var tag Bottle_Tag
json.NewDecoder(r.Body).Decode(&tag)
tag.Id = strconv.Itoa(messagesIdCounter)
messagesIdCounter++
db.Exec("USE ocean;")
db.Raw("INSERT INTO bottle_tag (Id,Bottleid,Tag) VALUES (?,?,?);", tag.Id, tag.BottleId, tag.Tag)
json.NewEncoder(w).Encode(tag)
}
func handleRequests() {
// creates a new instance of a mux router
myRouter := mux.NewRouter().StrictSlash(true)
// replace http.HandleFunc with myRouter.HandleFunc
myRouter.HandleFunc("/", homePage).Methods("GET")
myRouter.HandleFunc("/bottles/all", returnAllBottles).Methods("GET")
myRouter.HandleFunc("/bottles/{id}", returnBottleById).Methods("GET")
myRouter.HandleFunc("/bottles/getRandom/", returnRandomBottle).Methods("GET")
myRouter.HandleFunc("/bottles/getRandom/{tag}", returnRandomBottle).Methods("GET")
myRouter.HandleFunc("/bottles/getTag/{bottleid}", getTags).Methods("GET")
myRouter.HandleFunc("/bottles/assignTag/", assignTag).Methods("POST")
myRouter.HandleFunc("/login", loginAuth).Methods("POST")
myRouter.HandleFunc("/bottles/{id}", deleteBottleById).Methods("DELETE")
myRouter.HandleFunc("/bottles", createBottle).Methods("POST")
myRouter.HandleFunc("/incidents", createIncidentReport).Methods("POST")
myRouter.HandleFunc("/incidents/all", returnAllIncidents).Methods("GET")
myRouter.HandleFunc("/incidents/{id}", returnIncidentById).Methods("GET")
myRouter.HandleFunc("/messages", createMessage).Methods("POST")
myRouter.HandleFunc("/messages/{id}", returnMessageById).Methods("GET")
myRouter.HandleFunc("/messages/from/{clientid}", returnMessagesByClientId).Methods("GET")
myRouter.HandleFunc("/messages/to/{toclientid}", returnMessagesByToClientId).Methods("GET")
// finally, instead of passing in nil, we want
// to pass in our newly created router as the second
// argument
log.Fatal(http.ListenAndServe(":10000", myRouter))
}
func main() {
fmt.Println("Rest API v2.0 - Mux Routers")
fmt.Println("starting to connect to cockroachdb cluster...")
// Connect to the "ocean" database as the "kaiser" user.
// Read in connection string
scanner := bufio.NewScanner(os.Stdin)
log.Println("Enter a connection string: ")
scanner.Scan()
connstring := os.ExpandEnv(scanner.Text())
// Connect to the "ocean" database
var err error
db, err = gorm.Open(postgres.Open(connstring), &gorm.Config{})
if err != nil {
log.Fatal("error configuring the database: ", err)
}
log.Println("Hey! You successfully connected to your CockroachDB cluster.")
fmt.Println("started localhost @ 127.0.0.1:10000")
bottlesIdCounter = 1
incidentsIdCounter = 1
messagesIdCounter = 1
handleRequests()
}