Skip to content

Commit 48e2fab

Browse files
committed
Add authority.MustFromContext
1 parent d5070ec commit 48e2fab

File tree

2 files changed

+9
-14
lines changed

2 files changed

+9
-14
lines changed

api/api.go

+1-7
Original file line numberDiff line numberDiff line change
@@ -52,15 +52,9 @@ type Authority interface {
5252
Version() authority.Version
5353
}
5454

55-
var errAuthority = errors.New("authority is not in context")
56-
5755
// mustAuthority will be replaced on unit tests.
5856
var mustAuthority = func(ctx context.Context) Authority {
59-
a, ok := authority.FromContext(ctx)
60-
if !ok {
61-
panic(errAuthority)
62-
}
63-
return a
57+
return authority.MustFromContext(ctx)
6458
}
6559

6660
// TimeDuration is an alias of provisioner.TimeDuration

authority/authority.go

+8-7
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import (
77
"crypto/x509"
88
"encoding/hex"
99
"log"
10-
"net/http"
1110
"strings"
1211
"sync"
1312
"time"
@@ -167,12 +166,14 @@ func FromContext(ctx context.Context) (a *Authority, ok bool) {
167166
return
168167
}
169168

170-
// Middleware adds the current authority to the request context.
171-
func (a *Authority) Middleware(next http.Handler) http.Handler {
172-
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
173-
ctx := NewContext(r.Context(), a)
174-
next.ServeHTTP(w, r.WithContext(ctx))
175-
})
169+
// MustFromContext returns the current authority from the given context. It will
170+
// panic if the authority is not in the context.
171+
func MustFromContext(ctx context.Context) *Authority {
172+
if a, ok := FromContext(ctx); !ok {
173+
panic("authority is not in the context")
174+
} else {
175+
return a
176+
}
176177
}
177178

178179
// reloadAdminResources reloads admins and provisioners from the DB.

0 commit comments

Comments
 (0)