Skip to content

Instantly share code, notes, and snippets.

@cep21
Last active December 14, 2018 08:35
Show Gist options
  • Save cep21/136cbfd3542fced17c7aef3bec49936f to your computer and use it in GitHub Desktop.
Save cep21/136cbfd3542fced17c7aef3bec49936f to your computer and use it in GitHub Desktop.
package goexperiments
import (
"context"
"net/http"
)
type HandlerMiddleware interface {
HandleHTTPC(ctx context.Context, rw http.ResponseWriter, req *http.Request, next http.Handler)
}
var function1 HandlerMiddleware = nil
var function2 HandlerMiddleware = nil
func makeChain(chain ...HandlerMiddleware) 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(function1, function2, &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