Giter VIP home page Giter VIP logo

Comments (4)

spenczar avatar spenczar commented on May 18, 2024 2

@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.

nawuko avatar nawuko commented on May 18, 2024 1

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.

spenczar avatar spenczar commented on May 18, 2024

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.

EVILoptimist avatar EVILoptimist commented on May 18, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.