-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrest.go
More file actions
226 lines (205 loc) · 5.79 KB
/
Copy pathrest.go
File metadata and controls
226 lines (205 loc) · 5.79 KB
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
package main
import (
"bytes"
"encoding/json"
"fmt"
"io"
"log"
"net/http"
"strconv"
"time"
)
// This will be used to demonstrate a basic data type
type serverData struct {
Id int `json:"id"`
Name string `json:"name"`
Description string `json:"description"`
}
// This will represent what is normally a database, but could be JSON,
// CSV, XML, etc.
var dataStore map[int]*serverData
var url = "localhost"
var port = "8888"
func rest() {
//Build dummy data
dataStore = buildData()
//Create router using new mux capabilities which would normally
//be created with a package like gin
mux := http.NewServeMux()
mux.Handle("GET /{id}", http.HandlerFunc(handleGet))
mux.Handle("POST /", http.HandlerFunc(handlePost))
mux.Handle("PUT /", http.HandlerFunc(handlePut))
mux.Handle("DELETE /{id}", http.HandlerFunc(handleDelete))
fmt.Println("Router created")
//Kick off the server and continue
go listenAndServe(mux)
//Wait a sec to allow the server to init
time.Sleep(1 * time.Second)
//Demonstrate usage of endpoints
callEndpoints()
}
func callEndpoints() {
callGet(1)
callGet(5)
callPost()
callPut()
callDelete(4)
callDelete(6)
}
func callGet(id int) {
fmt.Println("Calling GET endpoint")
//This is the simplest way to call a GET in Go
resp, err := http.Get(fmt.Sprintf("http://%s:%s/%v", url, port, id))
if err != nil {
fmt.Println(fmt.Sprintf("Error calling GET: %v", err))
}
defer resp.Body.Close()
body, err := io.ReadAll(resp.Body)
if err != nil {
fmt.Println(fmt.Sprintf("Error calling GET: %v", err))
}
if resp.StatusCode != http.StatusOK {
fmt.Printf("Status : %s\n", resp.Status)
} else {
fmt.Printf("Response received: %s\n", string(body))
}
}
func callPost() {
fmt.Println("Getting data for id=11. This should get a 404:")
callGet(11)
fmt.Println("Calling POST endpoint")
data := serverData{Id: 11, Name: "data-11", Description: "This is data 11"}
d, err := json.Marshal(data)
if err != nil {
fmt.Println(fmt.Sprintf("Error marshalling json: %v", err))
}
//This is the simplest way to POST in Go
resp, err := http.Post(fmt.Sprintf("http://%s:%s/", url, port), "application/json", bytes.NewBuffer(d))
if err != nil {
fmt.Println(fmt.Sprintf("Error calling POST: %v", err))
}
defer resp.Body.Close()
fmt.Printf("Response received: %s\n", resp.Status)
fmt.Println("Getting data for id=11. This should succeed now")
callGet(11)
}
func callPut() {
fmt.Println("Object to update:")
callGet(7)
fmt.Println("Calling PUT endpoint")
data := serverData{Id: 7, Name: "data-777", Description: "Updated data 777"}
d, err := json.Marshal(data)
if err != nil {
fmt.Println(fmt.Sprintf("Error marshalling json: %v", err))
}
//Currently, there is no shorthand for a PUT in Go,
//so we have to create an http client
client := &http.Client{}
url := fmt.Sprintf("http://%s:%s/%v", url, port, 7)
req, err := http.NewRequest(http.MethodPut, url, bytes.NewBuffer(d))
if err != nil {
fmt.Printf("Error creating PUT request: %v\n", err)
}
req.Header.Set("Content-Type", "application/json")
resp, err := client.Do(req)
if err != nil {
fmt.Printf("Error calling PUT: %v\n", err)
}
fmt.Printf("Response received: %s\n", resp.Status)
fmt.Println("Getting data for id=7. This should be updated now")
callGet(7)
}
func callDelete(id int) {
callGet(id)
fmt.Println("Calling Delete endpoint")
//Same for DELETEs, need a client
client := &http.Client{}
url := fmt.Sprintf("http://%s:%s/%v", url, port, id)
req, err := http.NewRequest(http.MethodDelete, url, nil)
if err != nil {
fmt.Printf("Error creating DELETE request: %v\n", err)
}
resp, err := client.Do(req)
if err != nil {
fmt.Printf("Error calling DELETE: %v\n", err)
}
fmt.Printf("Response received: %s\n", resp.Status)
fmt.Printf("Getting data for id=%v This should get a 404\n", id)
callGet(id)
}
func listenAndServe(mux *http.ServeMux) {
fmt.Println("Starting server")
log.Fatal(http.ListenAndServe(fmt.Sprintf(":%s", port), mux))
}
func handleGet(w http.ResponseWriter, r *http.Request) {
//id is defined in the route stored in mux
id := r.PathValue("id")
if x, err := strconv.Atoi(id); err != nil {
w.WriteHeader(http.StatusBadRequest)
} else {
if d, ok := dataStore[x]; ok {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
_ = json.NewEncoder(w).Encode(d)
} else {
w.WriteHeader(http.StatusNotFound)
}
}
}
func handlePost(w http.ResponseWriter, r *http.Request) {
var data serverData
if err := json.NewDecoder(r.Body).Decode(&data); err != nil {
//poorly formed object
w.WriteHeader(http.StatusBadRequest)
return
}
if _, ok := dataStore[data.Id]; ok {
//id already exists, so we can't (or shouldn't) create it
w.WriteHeader(http.StatusBadRequest)
return
}
dataStore[data.Id] = &data
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusCreated)
}
func handlePut(w http.ResponseWriter, r *http.Request) {
var data serverData
if err := json.NewDecoder(r.Body).Decode(&data); err != nil {
w.WriteHeader(http.StatusBadRequest)
return
}
if _, ok := dataStore[data.Id]; !ok {
w.WriteHeader(http.StatusNotFound)
return
}
//Update the "record"
dataStore[data.Id] = &data
w.WriteHeader(http.StatusOK)
}
func handleDelete(w http.ResponseWriter, r *http.Request) {
id := r.PathValue("id")
x, err := strconv.Atoi(id)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
return
}
if _, ok := dataStore[x]; !ok {
w.WriteHeader(http.StatusNotFound)
return
}
//Delete the record
delete(dataStore, x)
w.WriteHeader(http.StatusOK)
}
func buildData() map[int]*serverData {
//Create some data to use
data := make(map[int]*serverData)
for i := 0; i < 11; i++ {
data[i] = &serverData{
Id: i,
Name: fmt.Sprintf("data-%v", i),
Description: fmt.Sprintf("This is data #%v", i),
}
}
return data
}