-
-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
315 additions
and
165 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
// +build go1.9 | ||
|
||
package kami | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"net/http" | ||
) | ||
|
||
// wrap tries to turn a HandlerType into a ContextHandler | ||
func wrap(h HandlerType) ContextHandler { | ||
switch x := h.(type) { | ||
case ContextHandler: | ||
return x | ||
case func(context.Context, http.ResponseWriter, *http.Request): | ||
return HandlerFunc(x) | ||
case http.Handler: | ||
return HandlerFunc(func(_ context.Context, w http.ResponseWriter, r *http.Request) { | ||
x.ServeHTTP(w, r) | ||
}) | ||
case func(http.ResponseWriter, *http.Request): | ||
return HandlerFunc(func(_ context.Context, w http.ResponseWriter, r *http.Request) { | ||
x(w, r) | ||
}) | ||
} | ||
panic(fmt.Errorf("unsupported HandlerType: %T", h)) | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
// +build go1.7 | ||
|
||
package kami | ||
|
||
import ( | ||
"context" | ||
"net/http" | ||
) | ||
|
||
// HandlerType is the type of Handlers and types that kami internally converts to | ||
// ContextHandler. In order to provide an expressive API, this type is an alias for | ||
// interface{} that is named for the purposes of documentation, however only the | ||
// following concrete types are accepted: | ||
// - types that implement http.Handler | ||
// - types that implement ContextHandler | ||
// - func(http.ResponseWriter, *http.Request) | ||
// - func(context.Context, http.ResponseWriter, *http.Request) | ||
type HandlerType interface{} | ||
|
||
// ContextHandler is like http.Handler but supports context. | ||
type ContextHandler interface { | ||
ServeHTTPContext(context.Context, http.ResponseWriter, *http.Request) | ||
} | ||
|
||
// HandlerFunc is like http.HandlerFunc with context. | ||
type HandlerFunc func(context.Context, http.ResponseWriter, *http.Request) | ||
|
||
func (h HandlerFunc) ServeHTTPContext(ctx context.Context, w http.ResponseWriter, r *http.Request) { | ||
h(ctx, w, r) | ||
} |
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
// +build go1.9 | ||
|
||
package kami | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"net/http" | ||
|
||
"github.com/zenazn/goji/web/mutil" | ||
) | ||
|
||
// convert turns standard http middleware into kami Middleware if needed. | ||
func convert(mw MiddlewareType) Middleware { | ||
switch x := mw.(type) { | ||
case Middleware: | ||
return x | ||
case func(context.Context, http.ResponseWriter, *http.Request) context.Context: | ||
return Middleware(x) | ||
case func(ContextHandler) ContextHandler: | ||
return func(ctx context.Context, w http.ResponseWriter, r *http.Request) context.Context { | ||
var dh dummyHandler | ||
x(&dh).ServeHTTPContext(ctx, w, r) | ||
if !dh { | ||
return nil | ||
} | ||
return ctx | ||
} | ||
case func(http.Handler) http.Handler: | ||
return func(ctx context.Context, w http.ResponseWriter, r *http.Request) context.Context { | ||
var dh dummyHandler | ||
x(&dh).ServeHTTP(w, r) | ||
if !dh { | ||
return nil | ||
} | ||
return ctx | ||
} | ||
case http.Handler: | ||
return Middleware(func(_ context.Context, w http.ResponseWriter, r *http.Request) context.Context { | ||
x.ServeHTTP(w, r) | ||
return r.Context() | ||
}) | ||
case func(w http.ResponseWriter, r *http.Request): | ||
return Middleware(func(_ context.Context, w http.ResponseWriter, r *http.Request) context.Context { | ||
x(w, r) | ||
return r.Context() | ||
}) | ||
case func(w http.ResponseWriter, r *http.Request) context.Context: | ||
return Middleware(func(_ context.Context, w http.ResponseWriter, r *http.Request) context.Context { | ||
return x(w, r) | ||
}) | ||
} | ||
panic(fmt.Errorf("unsupported MiddlewareType: %T", mw)) | ||
} | ||
|
||
// convertAW | ||
func convertAW(aw AfterwareType) Afterware { | ||
switch x := aw.(type) { | ||
case Afterware: | ||
return x | ||
case func(context.Context, mutil.WriterProxy, *http.Request) context.Context: | ||
return Afterware(x) | ||
case func(context.Context, *http.Request) context.Context: | ||
return func(ctx context.Context, _ mutil.WriterProxy, r *http.Request) context.Context { | ||
return x(ctx, r) | ||
} | ||
case func(context.Context) context.Context: | ||
return func(ctx context.Context, _ mutil.WriterProxy, _ *http.Request) context.Context { | ||
return x(ctx) | ||
} | ||
case Middleware: | ||
return func(ctx context.Context, w mutil.WriterProxy, r *http.Request) context.Context { | ||
return x(ctx, w, r) | ||
} | ||
case func(context.Context, http.ResponseWriter, *http.Request) context.Context: | ||
return func(ctx context.Context, w mutil.WriterProxy, r *http.Request) context.Context { | ||
return x(ctx, w, r) | ||
} | ||
case func(w http.ResponseWriter, r *http.Request) context.Context: | ||
return Afterware(func(_ context.Context, w mutil.WriterProxy, r *http.Request) context.Context { | ||
return x(w, r) | ||
}) | ||
case func(w mutil.WriterProxy, r *http.Request) context.Context: | ||
return Afterware(func(_ context.Context, w mutil.WriterProxy, r *http.Request) context.Context { | ||
return x(w, r) | ||
}) | ||
case http.Handler: | ||
return Afterware(func(_ context.Context, w mutil.WriterProxy, r *http.Request) context.Context { | ||
x.ServeHTTP(w, r) | ||
return r.Context() | ||
}) | ||
case func(w http.ResponseWriter, r *http.Request): | ||
return Afterware(func(_ context.Context, w mutil.WriterProxy, r *http.Request) context.Context { | ||
x(w, r) | ||
return r.Context() | ||
}) | ||
case func(w mutil.WriterProxy, r *http.Request): | ||
return Afterware(func(_ context.Context, w mutil.WriterProxy, r *http.Request) context.Context { | ||
x(w, r) | ||
return r.Context() | ||
}) | ||
} | ||
panic(fmt.Errorf("unsupported AfterwareType: %T", aw)) | ||
} |
Oops, something went wrong.