mirror of
https://github.com/micromdm/micromdm/
synced 2026-08-10 11:36:06 +08:00
organize essential APIs into platform, workflow and pkg folders (#337)
Add more logic to the way code is organized. /pkg -- library code not directly connected to micromdm /mdm -- packages meant for the services devices interract with. The MDM protocol. /dep -- DEP API and related packages. /platform -- Core APIs the server provides. Commands API, Devices API, queue, pubsub etc. /workflow -- Packages/API that build on top of platform. Today that's the webhook package. Depending on what ends up here, the workflow folder might become its own repository.
This commit is contained in:
67
platform/command/service.go
Normal file
67
platform/command/service.go
Normal file
@@ -0,0 +1,67 @@
|
||||
// Package command provides utilities for creating MDM Payloads.
|
||||
package command
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"github.com/go-kit/kit/log"
|
||||
"github.com/go-kit/kit/metrics"
|
||||
"github.com/micromdm/mdm"
|
||||
"golang.org/x/net/context"
|
||||
)
|
||||
|
||||
type Service interface {
|
||||
NewCommand(context.Context, *mdm.CommandRequest) (*mdm.Payload, error)
|
||||
}
|
||||
|
||||
// Middleware describes a service (as opposed to endpoint) middleware.
|
||||
type Middleware func(Service) Service
|
||||
|
||||
// ServiceLoggingMiddleware returns a service middleware that logs the
|
||||
// parameters and result of each method invocation.
|
||||
func ServiceLoggingMiddleware(logger log.Logger) Middleware {
|
||||
return func(next Service) Service {
|
||||
return serviceLoggingMiddleware{
|
||||
logger: logger,
|
||||
next: next,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (mw serviceLoggingMiddleware) NewCommand(ctx context.Context, req *mdm.CommandRequest) (p *mdm.Payload, err error) {
|
||||
defer func(begin time.Time) {
|
||||
mw.logger.Log(
|
||||
"method", "NewCommand",
|
||||
"error", err,
|
||||
"took", time.Since(begin),
|
||||
)
|
||||
}(time.Now())
|
||||
return mw.next.NewCommand(ctx, req)
|
||||
}
|
||||
|
||||
type serviceLoggingMiddleware struct {
|
||||
logger log.Logger
|
||||
next Service
|
||||
}
|
||||
|
||||
// ServiceInstrumentingMiddleware returns a service middleware that tracks the
|
||||
// number of payloads created by the service.
|
||||
func ServiceInstrumentingMiddleware(p metrics.Counter) Middleware {
|
||||
return func(next Service) Service {
|
||||
return serviceInstrumentingMiddleware{
|
||||
payloads: p,
|
||||
next: next,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
type serviceInstrumentingMiddleware struct {
|
||||
payloads metrics.Counter
|
||||
next Service
|
||||
}
|
||||
|
||||
func (mw serviceInstrumentingMiddleware) NewCommand(ctx context.Context, req *mdm.CommandRequest) (*mdm.Payload, error) {
|
||||
p, err := mw.next.NewCommand(ctx, req)
|
||||
mw.payloads.Add(1)
|
||||
return p, err
|
||||
}
|
||||
Reference in New Issue
Block a user