Created
June 22, 2019 23:05
-
-
Save joaoh82/31a388287d69404299c3be1b0544425e 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 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