Пример #1
0
func ServerError(w http.ResponseWriter, r *http.Request) {
	SetCacheControl(w, CacheNoStore, -1)

	c := appengine.NewContext(r)
	requestID := appengine.RequestID(c)
	http.Error(w, fmt.Sprintf("There was an internal server error while processing your request. Administrators have been notified.\nRequest ID: %s", requestID), http.StatusInternalServerError)
}
Пример #2
0
func aboutPage(w http.ResponseWriter, r *http.Request) {
	c := appengine.NewContext(r)
	fmt.Fprintf(w, "<h1>%v</h1>", appengine.DefaultVersionHostname(c))

	token, expire, _ := appengine.AccessToken(c, "test")
	fmt.Fprintf(w, "<p>AccessToken: %v %v", token, expire)

	fmt.Fprintf(w, "<p>AppID: %v", appengine.AppID(c))
	fmt.Fprintf(w, "<p>FQAppID: %v", c.FullyQualifiedAppID())
	fmt.Fprintf(w, "<p>Go version: %v", runtime.Version())
	fmt.Fprintf(w, "<p>Datacenter: %v", appengine.Datacenter())
	fmt.Fprintf(w, "<p>InstanceID: %v", appengine.InstanceID())
	fmt.Fprintf(w, "<p>IsDevAppServer: %v", appengine.IsDevAppServer())
	fmt.Fprintf(w, "<p>RequestID: %v", appengine.RequestID(c))
	fmt.Fprintf(w, "<p>ServerSoftware: %v", appengine.ServerSoftware())

	sa, _ := appengine.ServiceAccount(c)
	fmt.Fprintf(w, "<p>ServiceAccount: %v", sa)

	keyname, signed, _ := appengine.SignBytes(c, []byte("test"))
	fmt.Fprintf(w, "<p>SignBytes: %v %v", keyname, signed)
	fmt.Fprintf(w, "<p>VersionID: %v", appengine.VersionID(c))

	fmt.Fprintf(w, "<p>Request: %v", r)
	r2 := c.Request()
	fmt.Fprintf(w, "<p>Context Request type/value: %T %v", r2, r2)
}
Пример #3
0
// AppEngineLogCtx returns a goa middleware that sets the appengine context in the log records.
func AppEngineLogCtx() goa.Middleware {
	return func(h goa.Handler) goa.Handler {
		return func(ctx *goa.Context) error {
			actx := appengine.NewContext(ctx.Request())
			ctx.SetValue(goa.ReqIDKey, appengine.RequestID(actx))
			ctx.Logger = ctx.Logger.New("aeCtx", actx)
			return h(ctx)
		}
	}
}
Пример #4
0
func CreateHandler(handlerFunc func(appengine.Context, http.ResponseWriter, *http.Request)) appstats.Handler {
	return appstats.NewHandler(func(c appengine.Context, w http.ResponseWriter, r *http.Request) {
		headers := w.Header()
		headers.Set("X-Frame-Options", "SAMEORIGIN")
		headers.Set("X-XSS-Protection", "0")

		c.Infof("Request ID: %s", appengine.RequestID(c))

		handlerFunc(c, w, r)
	})
}
Пример #5
0
// Handler function for /nonce
func handleGetNonce(w http.ResponseWriter, r *http.Request) {
	c := appengine.NewContext(r)
	hash := md5.New()
	now := time.Now()

	fmt.Fprintf(hash, "%v%v", appengine.RequestID(c), now.UnixNano())
	nonce := fmt.Sprintf("%x", hash.Sum(make([]byte, 0, 16)))

	obj := &Nonce{
		now,
		now.Add(180 * time.Second),
		r.UserAgent(),
		r.RemoteAddr}

	err := datastore.RunInTransaction(c, func(c appengine.Context) error {
		_, err := datastore.Put(c, NonceKey(c, nonce), obj)
		return err
	}, nil)
	if err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}

	w.Header().Set("Content-Type", "text/plain")

	// Make _really_ sure this is not cached
	w.Header().Set("Cache-Control", "no-cache, no-store, must-revalidate")
	w.Header().Set("Pragma", "no-cache")
	w.Header().Set("Expires", "0")

	w.Write([]byte(nonce))

	go func() {
		// Delete expired nonces of a different shard
		if err := deleteExpired(c, now, nonceShardKey(c, nonce[2:])); err != nil {
			c.Warningf("deleteExpired failed: %v", err)
		}
	}()
}
Пример #6
0
func RequestID(ctx netcontext.Context) string  { return appengine.RequestID(fromContext(ctx)) }