github.com/EngineerKamesh/gofullstack@v0.0.0-20180609171605-d41341d7d4ee/volume4/section2/gopherface/endpoints/friendslist.go (about)

     1  package endpoints
     2  
     3  import (
     4  	"encoding/json"
     5  	"log"
     6  	"net/http"
     7  
     8  	"github.com/EngineerKamesh/gofullstack/volume4/section2/gopherface/common/authenticate"
     9  
    10  	"github.com/EngineerKamesh/gofullstack/volume4/section2/gopherface/common"
    11  )
    12  
    13  func FriendsListEndpoint(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  		gophers, err := env.DB.FriendsList(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(gophers)
    31  
    32  	})
    33  }