コード例 #1
0
ファイル: util.go プロジェクト: jmptrader/go-webapp-skeleton
// renderTemplate is a wrapper around template.ExecuteTemplate.  It writes into
// a bytes.Buffer before writing to the http.ResponseWriter to catch any errors
// resulting from populating the template.
func renderTemplate(ctx context.Context, w http.ResponseWriter, name string, data map[string]interface{}) error {
	// Ensure the template exists in the map.
	tmpl, ok := templatesMap[name]
	if !ok {
		log.FromContext(ctx).WithField("name", name).Error("template does not exist")
		return fmt.Errorf("The template %s does not exist", name)
	}

	// Create a buffer to temporarily write to and check if any errors were encounted.
	buf := bufpool.Get()
	defer bufpool.Put(buf)

	err := tmpl.ExecuteTemplate(buf, "base", data)
	if err != nil {
		log.FromContext(ctx).WithField("err", err).Error("could not render template")
		return err
	}

	// Set the header and write the buffer to the http.ResponseWriter
	w.Header().Set("Content-Type", "text/html; charset=utf-8")
	buf.WriteTo(w)
	return nil
}
コード例 #2
0
// ListPeople accepts a request to retrieve a list of people.
//
//     GET /api/people
//
func ListPeople(c web.C, w http.ResponseWriter, r *http.Request) {
	var (
		ctx    = context.FromC(c)
		limit  = handler.ToLimit(r)
		offset = handler.ToOffset(r)
	)

	people, err := datastore.ListPeople(ctx, limit, offset)
	if err != nil {
		log.FromContext(ctx).WithField("err", err).Error("Error listing people")
		w.WriteHeader(http.StatusNotFound)
		return
	}

	json.NewEncoder(w).Encode(people)
}
コード例 #3
0
// ListPeople shows a list of all people
//
//     GET /people
//
func ListPeople(c web.C, w http.ResponseWriter, r *http.Request) {
	var (
		ctx    = context.FromC(c)
		limit  = handler.ToLimit(r)
		offset = handler.ToOffset(r)
	)

	people, err := datastore.ListPeople(ctx, limit, offset)
	if err != nil {
		log.FromContext(ctx).WithField("err", err).Error("Error listing people")
		w.WriteHeader(http.StatusNotFound)
		return
	}

	renderTemplate(ctx, w, "person_list.tmpl", M{
		"People": people,
	})
}
コード例 #4
0
// DeletePerson accepts a request to delete a person.
//
//     DELETE /api/people/:person
//
func DeletePerson(c web.C, w http.ResponseWriter, r *http.Request) {
	var (
		ctx   = context.FromC(c)
		idStr = c.URLParams["person"]
	)

	id, err := strconv.ParseInt(idStr, 10, 64)
	if err != nil {
		w.WriteHeader(http.StatusBadRequest)
		return
	}

	err = datastore.DeletePerson(ctx, id)
	if err != nil {
		log.FromContext(ctx).WithField("err", err).Error("Error deleting person")
		w.WriteHeader(http.StatusNotFound)
		return
	}

	w.WriteHeader(http.StatusNoContent)
}
コード例 #5
0
// GetPerson accepts a request to retrieve information about a particular person.
//
//     GET /api/people/:person
//
func GetPerson(c web.C, w http.ResponseWriter, r *http.Request) {
	var (
		ctx   = context.FromC(c)
		idStr = c.URLParams["person"]
	)

	id, err := strconv.ParseInt(idStr, 10, 64)
	if err != nil {
		w.WriteHeader(http.StatusBadRequest)
		return
	}

	person, err := datastore.GetPerson(ctx, id)
	if err != nil {
		log.FromContext(ctx).WithField("err", err).Error("Error getting person")
		w.WriteHeader(http.StatusNotFound)
		return
	}

	json.NewEncoder(w).Encode(person)
}