コード例 #1
0
ファイル: main.go プロジェクト: rillomas/gae-go-dart-skeleton
func handleIndexPageRequest(w http.ResponseWriter, r *http.Request) {
	c := appengine.NewContext(r)
	// set the initial visitor info to the web site by templating
	k := storage.GetVisitorInfoKey(c)
	info, err := storage.GetVisitorInfo(c, k)
	// ignore the NoSuchEntityError and return the default value if we don't have the entity stored yet
	if err != nil && err != datastore.ErrNoSuchEntity {
		log.Errorf(c, "Failed getting visitor info: %v", err)
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}
	outBuf, err := json.Marshal(info)
	if err != nil {
		log.Errorf(c, "Error while marshaling visitor info: %v", err)
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}
	param := struct {
		VisitorInfo string
	}{
		string(outBuf),
	}
	err = xt.ExecuteTemplate(w, indexFileName, &param)
	if err != nil {
		log.Errorf(c, "Failed executing template: %v", err)
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}
}
コード例 #2
0
ファイル: main.go プロジェクト: rillomas/gae-go-dart-skeleton
func getVisitorInfo(c context.Context, w http.ResponseWriter, r *http.Request) {
	k := storage.GetVisitorInfoKey(c)
	info, err := storage.GetVisitorInfo(c, k)
	// ignore the NoSuchEntityError and return the default value if we don't have the entity stored yet
	if err != nil && err != datastore.ErrNoSuchEntity {
		log.Errorf(c, "Failed getting visitor info: %v", err)
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}
	enc := json.NewEncoder(w)
	err = enc.Encode(info)
	if err != nil {
		log.Errorf(c, "Failed encoding json response: %v", err)
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}
}