Selaa lähdekoodia

Add authority.MustFromContext

Mariano Cano 4 vuotta sitten
vanhempi
sitoutus
48e2fabeb8
2 muutettua tiedostoa jossa 9 lisäystä ja 14 poistoa
  1. 1 7
      api/api.go
  2. 8 7
      authority/authority.go

+ 1 - 7
api/api.go

@@ -52,15 +52,9 @@ type Authority interface {
 	Version() authority.Version
 }
 
-var errAuthority = errors.New("authority is not in context")
-
 // mustAuthority will be replaced on unit tests.
 var mustAuthority = func(ctx context.Context) Authority {
-	a, ok := authority.FromContext(ctx)
-	if !ok {
-		panic(errAuthority)
-	}
-	return a
+	return authority.MustFromContext(ctx)
 }
 
 // TimeDuration is an alias of provisioner.TimeDuration

+ 8 - 7
authority/authority.go

@@ -7,7 +7,6 @@ import (
 	"crypto/x509"
 	"encoding/hex"
 	"log"
-	"net/http"
 	"strings"
 	"sync"
 	"time"
@@ -167,12 +166,14 @@ func FromContext(ctx context.Context) (a *Authority, ok bool) {
 	return
 }
 
-// Middleware adds the current authority to the request context.
-func (a *Authority) Middleware(next http.Handler) http.Handler {
-	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
-		ctx := NewContext(r.Context(), a)
-		next.ServeHTTP(w, r.WithContext(ctx))
-	})
+// MustFromContext returns the current authority from the given context. It will
+// panic if the authority is not in the context.
+func MustFromContext(ctx context.Context) *Authority {
+	if a, ok := FromContext(ctx); !ok {
+		panic("authority is not in the context")
+	} else {
+		return a
+	}
 }
 
 // reloadAdminResources reloads admins and provisioners from the DB.