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

     1  package endpoints
     2  
     3  import (
     4  	"encoding/json"
     5  	"io/ioutil"
     6  	"log"
     7  	"net/http"
     8  
     9  	"github.com/EngineerKamesh/gofullstack/volume4/section3/gopherface/common"
    10  )
    11  
    12  func GetGopherProfileEndpoint(env *common.Env) http.HandlerFunc {
    13  	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
    14  
    15  		var gopherUUID string
    16  		reqBody, err := ioutil.ReadAll(r.Body)
    17  		if err != nil {
    18  			log.Print("Encountered error when attempting to read the request body: ", err)
    19  		}
    20  		err = json.Unmarshal(reqBody, &gopherUUID)
    21  		if err != nil {
    22  			log.Print("Encountered error when attempting to unmarshal JSON: ", err)
    23  		}
    24  
    25  		u, err := env.DB.GetGopherProfile(gopherUUID)
    26  
    27  		if err != nil {
    28  			log.Print(err)
    29  		}
    30  
    31  		w.Header().Set("Content-Type", "application/json")
    32  		json.NewEncoder(w).Encode(u)
    33  
    34  	})
    35  }