Skip to content

Commit

Permalink
Rename http handlers that end in HandlerFunc to just Handler (as …
Browse files Browse the repository at this point in the history
…an `http.HandlerFunc` fits the `http.Handler` interface)
  • Loading branch information
jessepeterson committed Apr 4, 2022
1 parent adc65ef commit 3713821
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 18 deletions.
12 changes: 6 additions & 6 deletions cmd/nanomdm/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,10 +118,10 @@ func main() {
var mdmHandler http.Handler
if *flCheckin {
// if we use the check-in handler then only handle commands
mdmHandler = mdmhttp.CommandAndReportResultsHandlerFunc(mdmService, logger.With("handler", "command"))
mdmHandler = mdmhttp.CommandAndReportResultsHandler(mdmService, logger.With("handler", "command"))
} else {
// if we don't use a check-in handler then do both
mdmHandler = mdmhttp.CheckinAndCommandHandlerFunc(mdmService, logger.With("handler", "checkin-command"))
mdmHandler = mdmhttp.CheckinAndCommandHandler(mdmService, logger.With("handler", "checkin-command"))
}
mdmHandler = mdmhttp.CertVerifyMiddleware(mdmHandler, verifier, logger.With("handler", "cert-verify"))
if *flCertHeader != "" {
Expand All @@ -134,7 +134,7 @@ func main() {
if *flCheckin {
// if we specified a separate check-in handler, set it up
var checkinHandler http.Handler
checkinHandler = mdmhttp.CheckinHandlerFunc(mdmService, logger.With("handler", "checkin"))
checkinHandler = mdmhttp.CheckinHandler(mdmService, logger.With("handler", "checkin"))
checkinHandler = mdmhttp.CertVerifyMiddleware(checkinHandler, verifier, logger.With("handler", "cert-verify"))
if *flCertHeader != "" {
checkinHandler = mdmhttp.CertExtractPEMHeaderMiddleware(checkinHandler, *flCertHeader, logger.With("handler", "cert-extract"))
Expand All @@ -154,14 +154,14 @@ func main() {

// register API handler for push cert storage/upload.
var pushCertHandler http.Handler
pushCertHandler = mdmhttp.StorePushCertHandlerFunc(mdmStorage, logger.With("handler", "store-cert"))
pushCertHandler = mdmhttp.StorePushCertHandler(mdmStorage, logger.With("handler", "store-cert"))
pushCertHandler = mdmhttp.BasicAuthMiddleware(pushCertHandler, apiUsername, *flAPIKey, "nanomdm")
mux.Handle(endpointAPIPushCert, pushCertHandler)

// register API handler for push notifications.
// we strip the prefix to use the path as an id.
var pushHandler http.Handler
pushHandler = mdmhttp.PushHandlerFunc(pushService, logger.With("handler", "push"))
pushHandler = mdmhttp.PushHandler(pushService, logger.With("handler", "push"))
pushHandler = http.StripPrefix(endpointAPIPush, pushHandler)
pushHandler = mdmhttp.BasicAuthMiddleware(pushHandler, apiUsername, *flAPIKey, "nanomdm")
mux.Handle(endpointAPIPush, pushHandler)
Expand All @@ -184,7 +184,7 @@ func main() {
// generate "enrollments" then this effively allows us to
// migrate MDM enrollments between servers.
var migHandler http.Handler
migHandler = mdmhttp.CheckinHandlerFunc(nano, logger.With("handler", "migration"))
migHandler = mdmhttp.CheckinHandler(nano, logger.With("handler", "migration"))
migHandler = mdmhttp.BasicAuthMiddleware(migHandler, apiUsername, *flAPIKey, "nanomdm")
mux.Handle(endpointAPIMigration, migHandler)
}
Expand Down
8 changes: 4 additions & 4 deletions http/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,13 +69,13 @@ func setupCtxLog(ctx context.Context, ids []string, logger log.Logger) (context.
return ctx, ctxlog.Logger(ctx, logger)
}

// PushHandlerFunc sends APNs push notifications to MDM enrollments.
// PushHandler sends APNs push notifications to MDM enrollments.
//
// Note the whole URL path is used as the identifier to push to. This
// probably necessitates stripping the URL prefix before using. Also
// note we expose Go errors to the output as this is meant for "API"
// users.
func PushHandlerFunc(pusher push.Pusher, logger log.Logger) http.HandlerFunc {
func PushHandler(pusher push.Pusher, logger log.Logger) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
ids := strings.Split(r.URL.Path, ",")
ctx, logger := setupCtxLog(r.Context(), ids, logger)
Expand Down Expand Up @@ -234,12 +234,12 @@ func RawCommandEnqueueHandler(enqueuer storage.CommandEnqueuer, pusher push.Push
}
}

// StorePushCertHandlerFunc reads a PEM-encoded certificate and private
// StorePushCertHandler reads a PEM-encoded certificate and private
// key from the HTTP body and saves it to storage. This effectively
// enables us to do something like:
// "% cat push.pem push.key | curl -T - http://api.example.com/" to
// upload our push certs.
func StorePushCertHandlerFunc(storage storage.PushCertStore, logger log.Logger) http.HandlerFunc {
func StorePushCertHandler(storage storage.PushCertStore, logger log.Logger) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
logger := ctxlog.Logger(r.Context(), logger)
b, err := ReadAllAndReplaceBody(r)
Expand Down
16 changes: 8 additions & 8 deletions http/mdm.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ func mdmReqFromHTTPReq(r *http.Request) *mdm.Request {
}
}

// CheckinHandlerFunc decodes an MDM check-in request and adapts it to service.
func CheckinHandlerFunc(svc service.Checkin, logger log.Logger) http.HandlerFunc {
// CheckinHandler decodes an MDM check-in request and adapts it to service.
func CheckinHandler(svc service.Checkin, logger log.Logger) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
logger := ctxlog.Logger(r.Context(), logger)
bodyBytes, err := ReadAllAndReplaceBody(r)
Expand All @@ -48,8 +48,8 @@ func CheckinHandlerFunc(svc service.Checkin, logger log.Logger) http.HandlerFunc
}
}

// CommandAndReportResultsHandlerFunc decodes an MDM command request and adapts it to service.
func CommandAndReportResultsHandlerFunc(svc service.CommandAndReportResults, logger log.Logger) http.HandlerFunc {
// CommandAndReportResultsHandler decodes an MDM command request and adapts it to service.
func CommandAndReportResultsHandler(svc service.CommandAndReportResults, logger log.Logger) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
logger := ctxlog.Logger(r.Context(), logger)
bodyBytes, err := ReadAllAndReplaceBody(r)
Expand All @@ -72,15 +72,15 @@ func CommandAndReportResultsHandlerFunc(svc service.CommandAndReportResults, log
}
}

// CheckinAndCommandHandlerFunc handles both check-in and command requests.
func CheckinAndCommandHandlerFunc(service service.CheckinAndCommandService, logger log.Logger) http.HandlerFunc {
// CheckinAndCommandHandler handles both check-in and command requests.
func CheckinAndCommandHandler(service service.CheckinAndCommandService, logger log.Logger) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
contentType := r.Header.Get("Content-Type")
if strings.HasPrefix(contentType, "application/x-apple-aspen-mdm-checkin") {
CheckinHandlerFunc(service, logger).ServeHTTP(w, r)
CheckinHandler(service, logger).ServeHTTP(w, r)
return
}
// assume a non-check-in is a command request
CommandAndReportResultsHandlerFunc(service, logger).ServeHTTP(w, r)
CommandAndReportResultsHandler(service, logger).ServeHTTP(w, r)
}
}

0 comments on commit 3713821

Please sign in to comment.