github.com/EngineerKamesh/gofullstack@v0.0.0-20180609171605-d41341d7d4ee/volume3/section5/gopherface/endpoints/fetchposts.go (about) 1 package endpoints 2 3 import ( 4 "encoding/json" 5 "log" 6 "net/http" 7 8 "github.com/EngineerKamesh/gofullstack/volume3/section5/gopherface/common/authenticate" 9 10 "github.com/EngineerKamesh/gofullstack/volume3/section5/gopherface/common" 11 ) 12 13 func FetchPostsEndpoint(env *common.Env) http.HandlerFunc { 14 return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 15 16 gfSession, err := authenticate.SessionStore.Get(r, "gopherface-session") 17 if err != nil { 18 log.Print(err) 19 return 20 } 21 uuid := gfSession.Values["uuid"].(string) 22 23 posts, err := env.DB.FetchPosts(uuid) 24 25 if err != nil { 26 log.Print(err) 27 } 28 29 w.Header().Set("Content-Type", "application/json") 30 json.NewEncoder(w).Encode(posts) 31 32 }) 33 }