Last active
December 14, 2018 08:35
-
-
Save cep21/136cbfd3542fced17c7aef3bec49936f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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