Comments (4)
@EVILoptimist You can do that by passing them into the context yourself through HTTP middleware. See https://twitchtv.github.io/twirp/docs/headers.html#read-http-headers-from-requests.
For example, if you wanted r.RemoteAddr
to be available:
var remoteAddrKey = new(int)
func RemoteAddr(ctx context.Context) string {
return ctx.Value(remoteAddrKey).(string)
}
func WithRemoteAddr(base http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
ctx = context.WithValue(remoteAddrKey, r.RemoteAddr)
r = r.WithContext(ctx)
base.ServeHTTP(w, r)
})
}
Now, wherever you're assembling your http.Handler
(probably package main
), you'd wrap the twirp server with that middleware:
app := NewHaberdasherImplementation()
s := example.NewHaberdasherServer(app, nil)
h := WithRemoteAddr(s)
http.ListenAndServe(":8080", h)
Now, requests that come in are passed through the WithRemoteAddr
wrapper, which pulls values out of the *http.Request
and stores them in *http.Request.Context()
, which is the context that will be passed to your service implementation's methods.
from twirp.
This question was asked on Slack recently. The owner of slack answered this with the following:
The simplest option is HTTP middleware:
func InjectAppengineContext(base http.Handler) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { r = r.WithContext(appengine.NewContext(r)) base.ServeHTTP(w, r) }) }
you'd wrap the generated server with that middleware before actually mounting it on a listener, for example:
s := NewAuthServer(...) http.ListenAndServe("host:port", InjectAppengineContext(s))
does that answer your question? like i mentioned, I'm a bit fuzzy on appengine and how this context thinger is supposed to be used
Hope i could help .... and spenczar dosent mind i copied his chat 🙈
from twirp.
Righto - HTTP middleware is the best way to go about this sort of thing. I think it's good to keep the raw Request hidden from the Twirp server.
from twirp.
So, am I missing something? Or is there another (probably obvious?) way to access things like r.Referer()
, r.UserAgent()
, and r.RemoteAddr
?
I can't figure out where they might be passed in the context. Or otherwise accessible.
from twirp.
Related Issues (20)
- Broken link in docs HOT 1
- Bug: Wrong generated file name error with `paths=imports` option & `module` option without trailing slash
- How to pronounce Twirp? HOT 1
- RFC 7807 HOT 1
- Proposal: write public constants for method names on code generation HOT 4
- Compatibility with `gogo/protobuf` HOT 2
- TwirpServer Constructor not capturing ServerOptions in v8.1.2 HOT 4
- [Bug/Support] Running server/client on kubernetes cluster with containerd runtime HOT 3
- bilibili use this twirp?
- Fasthttp support HOT 5
- Provide auto-instrumentation for OpenTelemetry standard HOT 3
- Generated Code for sending JSON requests is not in the sending path HOT 3
- [Documentation] Use protocurl to interact with twirp protobuf endpoint instead of curl+protoc HOT 2
- Varying Success Status Codes HOT 2
- Multiple WithHTTPRequestHeaders HOT 4
- Allow Interceptor to return *dynamicpb.Message HOT 3
- Deprecated usage of `ioutil` HOT 2
- Response bodies are not read to EOF in certain cases, potentially causing TCP issues. HOT 2
- Twirp handler only works with mux.Handler, doesn't work with chi.Handler HOT 3
- Documentation website domain triggers Chrome's warning HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from twirp.