-
Notifications
You must be signed in to change notification settings - Fork 40
/
followgopher.go
44 lines (34 loc) · 1.02 KB
/
followgopher.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 endpoints
import (
"encoding/json"
"io/ioutil"
"log"
"net/http"
"github.com/EngineerKamesh/gofullstack/volume4/section2/gopherface/common/authenticate"
"github.com/EngineerKamesh/gofullstack/volume4/section2/gopherface/common"
)
func FollowGopherEndpoint(env *common.Env) http.HandlerFunc {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
gfSession, err := authenticate.SessionStore.Get(r, "gopherface-session")
if err != nil {
log.Print(err)
return
}
uuid := gfSession.Values["uuid"].(string)
var gopherUUID string
reqBody, err := ioutil.ReadAll(r.Body)
if err != nil {
log.Print("Encountered error when attempting to read the request body: ", err)
}
err = json.Unmarshal(reqBody, &gopherUUID)
if err != nil {
log.Print("Encountered error when attempting to unmarshal JSON: ", err)
}
err = env.DB.FollowGopher(uuid, gopherUUID)
if err != nil {
log.Print(err)
}
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(err)
})
}