-
Notifications
You must be signed in to change notification settings - Fork 0
/
getAllSessions.go
40 lines (38 loc) · 1.24 KB
/
getAllSessions.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"
)
// `/sessions` GET route. It communicates with the session service only.
func GetAllSessions(client *http.Client, c *config.Config) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
// Create the request that calls our session service to get the sessions.
req, err := http.NewRequest(http.MethodGet, fmt.Sprintf("http://%v:%v", c.Service.Session.Host, c.Service.Session.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 sessions).
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)
}
}