/
getAllEvents.go
40 lines (38 loc) · 1.23 KB
/
getAllEvents.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
package handler
import (
"fmt"
"github.com/dnielsen/campsite/pkg/config"
"io/ioutil"
"log"
"net/http"
)
// `/events` GET route. It communicates with the event service only.
func GetAllEvents(client *http.Client, c *config.Config) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
// Create the request that calls our event service to get the events.
req, err := http.NewRequest(http.MethodGet, fmt.Sprintf("http://%v:%v", c.Service.Event.Host, c.Service.Event.Port), nil)
if err != nil {
log.Printf("Failed to create new request: %v", err)
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
// Make the request.
res, err := client.Do(req)
if err != nil {
log.Printf("Failed to do request: %v", err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
// Read the response body (hopefully it's our events).
b, err := ioutil.ReadAll(res.Body)
if err != nil {
log.Printf("Failed to read body: %v", err)
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
// Respond with the received response (hopefully it's 200 Status OK).
w.Header().Set(CONTENT_TYPE, r.Header.Get(CONTENT_TYPE))
w.WriteHeader(res.StatusCode)
w.Write(b)
}
}