-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
259 lines (216 loc) · 5.67 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
package main
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"os/signal"
"time"
"github.com/google/uuid"
"github.com/gorilla/mux"
"golang.org/x/sync/errgroup"
"golang.org/x/sys/unix"
"google.golang.org/protobuf/proto"
"github.com/VauntDev/glhf"
"github.com/VauntDev/glhf/example/pb"
)
type TodoService struct {
todos map[string]*pb.Todo
}
func (ts *TodoService) Add(t *pb.Todo) error {
ts.todos[t.Id] = t
return nil
}
func (ts *TodoService) Get(id string) (*pb.Todo, error) {
t, ok := ts.todos[id]
if !ok {
return nil, fmt.Errorf("no todo")
}
return t, nil
}
type Handlers struct {
service *TodoService
}
func (h *Handlers) StandardLookupTodo(w http.ResponseWriter, r *http.Request) {
p := mux.Vars(r)
id, ok := p["id"]
if !ok {
w.WriteHeader(http.StatusInternalServerError)
return
}
todo, err := h.service.Get(id)
if err != nil {
w.WriteHeader(http.StatusNotFound)
return
}
switch r.Header.Get("accept") {
case "application/json":
b, err := json.Marshal(todo)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
w.WriteHeader(http.StatusOK)
w.Write(b)
case "application/proto":
b, err := proto.Marshal(todo)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
w.WriteHeader(http.StatusOK)
w.Write(b)
}
}
func (h *Handlers) StandardCreateTodo(w http.ResponseWriter, r *http.Request) {
todo := &pb.Todo{}
switch r.Header.Get("content-type") {
case "application/json":
decode := json.NewDecoder(r.Body)
if err := decode.Decode(todo); err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
case "application/proto":
b, err := ioutil.ReadAll(r.Body)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
if err := proto.Unmarshal(b, todo); err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
default:
w.WriteHeader(http.StatusBadRequest)
return
}
if err := h.service.Add(todo); err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
w.WriteHeader(http.StatusOK)
}
func (h *Handlers) GLHFLookupTodo(r *glhf.Request[glhf.EmptyBody], w *glhf.Response[pb.Todo]) {
p := mux.Vars(r.HTTPRequest())
id, ok := p["id"]
if !ok {
w.SetStatus(http.StatusInternalServerError)
return
}
todo, err := h.service.Get(id)
if err != nil {
w.SetStatus(http.StatusNotFound)
return
}
w.SetBody(todo)
w.SetStatus(http.StatusOK)
}
func (h *Handlers) GLHFCreateTodo(r *glhf.Request[pb.Todo], w *glhf.Response[glhf.EmptyBody]) {
if r.Body() == nil {
w.SetStatus(http.StatusBadRequest)
return
}
if err := h.service.Add(r.Body()); err != nil {
w.SetStatus(http.StatusInternalServerError)
return
}
w.SetStatus(http.StatusOK)
}
func main() {
TodoService := &TodoService{
todos: make(map[string]*pb.Todo),
}
h := &Handlers{service: TodoService}
mux := mux.NewRouter()
mux.HandleFunc("/standard/todo", h.StandardCreateTodo)
mux.HandleFunc("/standard/todo/{id}", h.StandardLookupTodo)
mux.HandleFunc("/glhf/todo", glhf.Post(h.GLHFCreateTodo))
mux.HandleFunc("/glhf/todo/{id}", glhf.Get(h.GLHFLookupTodo))
server := http.Server{
Addr: ":8080",
Handler: mux,
}
g, ctx := errgroup.WithContext(context.Background())
g.Go(func() error {
log.Println("starting server")
if err := server.ListenAndServe(); err != nil {
return nil
}
return nil
})
g.Go(func() error {
sigs := make(chan os.Signal, 1)
// We'll accept graceful shutdowns when quit via SIGINT (Ctrl+C)
// SIGKILL, SIGQUIT or SIGTERM (Ctrl+/) will not be caught.
signal.Notify(sigs, os.Interrupt, unix.SIGTERM)
// block until we receive our signal or context is done
select {
case <-ctx.Done():
log.Println("ctx done, shutting down server")
case <-sigs:
log.Println("caught sig, shutting down server")
}
// Create a deadline to wait for cleanup
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
// Doesn't block if no connections, but will otherwise wait
// until the timeout deadline.
if err := server.Shutdown(ctx); err != nil {
return fmt.Errorf("error in server shutdown: %w", err)
}
return nil
})
// ----- Client Code ----- //
// wait for server to start
time.Sleep(time.Second * 1)
client := http.DefaultClient
id := uuid.NewString()
todo := &pb.Todo{
Id: id,
Item: &pb.Item{
Name: "Marshaling Example",
Message: "This todo is used to demo the marshalling functionality of glhf",
},
}
b, err := proto.Marshal(todo)
if err != nil {
log.Fatal("failed to marshal proto")
}
postRequest, err := http.NewRequest("POST", "http://localhost:8080/glhf/todo", bytes.NewBuffer(b))
if err != nil {
log.Fatal("failed to create post request")
}
postRequest.Header.Add("Content-Type", "application/proto") // send protobuff
log.Println("sending post request to create todo")
postResp, err := client.Do(postRequest)
if err != nil {
log.Fatal("failed to do post request", err)
}
if postResp.StatusCode != http.StatusOK {
log.Fatal("post request failed with", postResp.StatusCode)
}
getRequest, err := http.NewRequest("GET", "http://localhost:8080/glhf/todo/"+id, nil)
if err != nil {
log.Fatal("failed to create get request")
}
getRequest.Header.Add("Accept", "application/json") // get json
log.Println("sending get request to lookup todo")
getResp, err := client.Do(getRequest)
if err != nil {
log.Fatal("failed to do get request", err)
}
defer getResp.Body.Close()
if getResp.StatusCode != http.StatusOK {
log.Fatal("get failed with", getResp.StatusCode)
}
body, err := ioutil.ReadAll(getResp.Body)
if err != nil {
log.Fatal("failed to read response body", err)
}
log.Println(string(body))
}