Skip to content

Instantly share code, notes, and snippets.

@joaoh82
Created June 22, 2019 23:05
Show Gist options
  • Save joaoh82/31a388287d69404299c3be1b0544425e to your computer and use it in GitHub Desktop.
Save joaoh82/31a388287d69404299c3be1b0544425e to your computer and use it in GitHub Desktop.
package gocontext
import (
"context"
"net/http"
)
type SomeMiddleware interface {
HandleHTTPC(ctx context.Context, rw http.ResponseWriter, req *http.Request, next http.Handler)
}
var FunctionA SomeMiddleware = nil
var FunctionB SomeMiddleware = nil
func makeChain(chain ...SomeMiddleware) http.Handler {return nil}
type AddUserID struct {
OnUserID func(userID string) http.Handler
}
func (a *AddUserID) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
userID := req.Header.Get("userid")
a.OnUserID(userID).ServeHTTP(rw, req)
}
type UseUserID struct {
UserID string
}
func (u *UseUserID) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
rw.Write([]byte(u.UserID))
}
type ServerNoAbuseContext struct{}
func (s *ServerNoAbuseContext) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
req = req.WithContext(context.Background())
chainWithAuth := func(userID string) http.Handler {
return makeChain(FunctionA, FunctionA, &UseUserID{
UserID: userID,
})
}
chainPartOne := &AddUserID{
OnUserID: chainWithAuth,
}
chainPartOne.ServeHTTP(rw, req)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment