Make ResponseWriter an interface
Make ResponseWriter an interface with an unexported method. Implementors must embed a ResponseWriter from elsewhere. This gives us the flexibility of an interface while allowing us to add new methods in the future.
This commit is contained in:
12
handler.go
12
handler.go
@@ -21,23 +21,23 @@ import (
|
||||
//
|
||||
// Handlers should not modify the provided Request.
|
||||
type Handler interface {
|
||||
ServeGemini(context.Context, *ResponseWriter, *Request)
|
||||
ServeGemini(context.Context, ResponseWriter, *Request)
|
||||
}
|
||||
|
||||
// The HandlerFunc type is an adapter to allow the use of ordinary functions
|
||||
// as Gemini handlers. If f is a function with the appropriate signature,
|
||||
// HandlerFunc(f) is a Handler that calls f.
|
||||
type HandlerFunc func(context.Context, *ResponseWriter, *Request)
|
||||
type HandlerFunc func(context.Context, ResponseWriter, *Request)
|
||||
|
||||
// ServeGemini calls f(ctx, w, r).
|
||||
func (f HandlerFunc) ServeGemini(ctx context.Context, w *ResponseWriter, r *Request) {
|
||||
func (f HandlerFunc) ServeGemini(ctx context.Context, w ResponseWriter, r *Request) {
|
||||
f(ctx, w, r)
|
||||
}
|
||||
|
||||
// StatusHandler returns a request handler that responds to each request
|
||||
// with the provided status code and meta.
|
||||
func StatusHandler(status Status, meta string) Handler {
|
||||
return HandlerFunc(func(ctx context.Context, w *ResponseWriter, r *Request) {
|
||||
return HandlerFunc(func(ctx context.Context, w ResponseWriter, r *Request) {
|
||||
w.WriteHeader(status, meta)
|
||||
})
|
||||
}
|
||||
@@ -58,7 +58,7 @@ func StripPrefix(prefix string, h Handler) Handler {
|
||||
if prefix == "" {
|
||||
return h
|
||||
}
|
||||
return HandlerFunc(func(ctx context.Context, w *ResponseWriter, r *Request) {
|
||||
return HandlerFunc(func(ctx context.Context, w ResponseWriter, r *Request) {
|
||||
p := strings.TrimPrefix(r.URL.Path, prefix)
|
||||
rp := strings.TrimPrefix(r.URL.RawPath, prefix)
|
||||
if len(p) < len(r.URL.Path) && (r.URL.RawPath == "" || len(rp) < len(r.URL.RawPath)) {
|
||||
@@ -92,7 +92,7 @@ type timeoutHandler struct {
|
||||
dt time.Duration
|
||||
}
|
||||
|
||||
func (t *timeoutHandler) ServeGemini(ctx context.Context, w *ResponseWriter, r *Request) {
|
||||
func (t *timeoutHandler) ServeGemini(ctx context.Context, w ResponseWriter, r *Request) {
|
||||
ctx, cancel := context.WithTimeout(ctx, t.dt)
|
||||
defer cancel()
|
||||
|
||||
|
||||
Reference in New Issue
Block a user