/
getSessionById.go
44 lines (42 loc) · 1.3 KB
/
getSessionById.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
package handler
import (
"fmt"
"github.com/dnielsen/campsite/pkg/config"
"github.com/gorilla/mux"
"io/ioutil"
"log"
"net/http"
)
// `/sessions/{id}` GET route. It communicates with the session service only.
func GetSessionById(client *http.Client, c *config.Config) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
// Get the id parameter.
vars := mux.Vars(r)
id := vars[ID]
// Create the request that calls our session service to get it.
req, err := http.NewRequest(http.MethodGet, fmt.Sprintf("http://%v:%v/%v", c.Service.Session.Host, c.Service.Session.Port, id), 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 session).
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.
w.Header().Set(CONTENT_TYPE, r.Header.Get(CONTENT_TYPE))
w.WriteHeader(res.StatusCode)
w.Write(b)
}
}