https://github.com/captaincodeman/appengine-context
Middleware to access AppEngine context
https://github.com/captaincodeman/appengine-context
Last synced: 11 months ago
JSON representation
Middleware to access AppEngine context
- Host: GitHub
- URL: https://github.com/captaincodeman/appengine-context
- Owner: CaptainCodeman
- Created: 2018-06-14T18:03:30.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2018-06-14T18:05:06.000Z (over 7 years ago)
- Last Synced: 2025-01-08T14:15:28.668Z (about 1 year ago)
- Language: Go
- Size: 1.95 KB
- Stars: 2
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# appengine-context
Http middleware to create an AppEngine context from the request
and make it easy to retrieve it further down the call stack by
saving it into the request context.
See [this issue](https://github.com/golang/appengine/issues/99) for
why this is necessary - calls to AppEngine services must be made
using a context created using the _original_ request and if you
have middleware storing things into the context, this replaces
the request with a clone which then interferes with your ability
to call AppEngine services.
The code is tiny but I end up needing it in nearly every AppEngine
project so decided to package it up.
## Installation
Install using `go get`
go get -i github.com/captaincodeman/appengine-context
## Usage
Add the `Middleware` to your Router package of choice (or wrap
the standard http package Mux) and use the `Context` function
whenever you need to retrieve the AppEngine context to call any
AppEngine services. The middleware should be added _before_ any
other middleware that adds things to the request context.
Example:
```go
package demo
import (
"fmt"
"net/http"
"google.golang.org/appengine"
"google.golang.org/appengine/log"
"github.com/captaincodeman/appengine-context"
)
func main() {
handler := http.HandlerFunc(handle)
http.Handle("/", gaecontext.Middleware(handler))
appengine.Main()
}
func handle(w http.ResponseWriter, r *http.Request) {
ctx := gaecontext.Context(r)
// simple example just to demonstrate a call to
// an Appengine Service (stackdriver logging)
// using the context. This would work even if
// some other middleware added things into the
// request context ...
log.Debugf(ctx, "saying hello")
fmt.Fprint(w, "hello world!")
}
```