|
| 1 | +package service |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "time" |
| 6 | + |
| 7 | + "github.com/go-kit/kit/log" |
| 8 | + |
| 9 | + assignment "github.com/expandorg/assignment/pkg/assignment" |
| 10 | + authentication "github.com/expandorg/assignment/pkg/authentication" |
| 11 | + datastore "github.com/expandorg/assignment/pkg/datastore" |
| 12 | + registrysvc "github.com/expandorg/assignment/pkg/registrysvc" |
| 13 | +) |
| 14 | + |
| 15 | +type Middleware func(AssignmentService) AssignmentService |
| 16 | + |
| 17 | +type loggingMiddleware struct { |
| 18 | + next AssignmentService |
| 19 | + logger log.Logger |
| 20 | +} |
| 21 | + |
| 22 | +func ServiceMiddleware(logger log.Logger) Middleware { |
| 23 | + return func(next AssignmentService) AssignmentService { |
| 24 | + return &loggingMiddleware{ |
| 25 | + next: next, |
| 26 | + logger: logger, |
| 27 | + } |
| 28 | + } |
| 29 | +} |
| 30 | + |
| 31 | +func (mw loggingMiddleware) CreateAssignment(ctx context.Context, asgn assignment.Asgn, set *assignment.Settings) (*assignment.Assignment, error) { |
| 32 | + defer func(begin time.Time) { |
| 33 | + mw.logger.Log("method", "CreateAssignment", "id", "took", time.Since(begin)) |
| 34 | + }(time.Now()) |
| 35 | + return mw.next.CreateAssignment(ctx, asgn, set) |
| 36 | +} |
| 37 | + |
| 38 | +func (mw loggingMiddleware) Healthy() bool { |
| 39 | + return mw.next.Healthy() |
| 40 | +} |
| 41 | + |
| 42 | +func (mw loggingMiddleware) SetAuthData(data authentication.AuthData) { |
| 43 | + mw.next.SetAuthData(data) |
| 44 | +} |
| 45 | +func (mw loggingMiddleware) GetAssignments(a assignment.Params) (assignment.Assignments, error) { |
| 46 | + return mw.next.GetAssignments(a) |
| 47 | +} |
| 48 | +func (mw loggingMiddleware) GetAssignment(id string) (*assignment.Assignment, error) { |
| 49 | + return mw.next.GetAssignment(id) |
| 50 | +} |
| 51 | +func (mw loggingMiddleware) GetSettings(jobID uint64) (*assignment.Settings, error) { |
| 52 | + return mw.next.GetSettings(jobID) |
| 53 | +} |
| 54 | +func (mw loggingMiddleware) DeleteAssignment(id string) (bool, error) { |
| 55 | + return mw.next.DeleteAssignment(id) |
| 56 | +} |
| 57 | +func (mw loggingMiddleware) DeleteAssignments(ids []string) error { |
| 58 | + return mw.next.DeleteAssignments(ids) |
| 59 | +} |
| 60 | +func (mw loggingMiddleware) UpdateAssignment(workerID, jobID, responseID uint64, status string) (bool, error) { |
| 61 | + return mw.next.UpdateAssignment(workerID, jobID, responseID, status) |
| 62 | +} |
| 63 | +func (mw loggingMiddleware) CreateSettings(s assignment.Settings) (*assignment.Settings, error) { |
| 64 | + return mw.next.CreateSettings(s) |
| 65 | +} |
| 66 | +func (mw loggingMiddleware) GetStore() datastore.Storage { |
| 67 | + return mw.next.GetStore() |
| 68 | +} |
| 69 | +func (mw loggingMiddleware) ValidateAssignment(a assignment.NewAssignment, set *assignment.Settings) (bool, error) { |
| 70 | + return mw.next.ValidateAssignment(a, set) |
| 71 | +} |
| 72 | +func (mw loggingMiddleware) GetRegistration(jobID uint64) (*registrysvc.Registration, error) { |
| 73 | + return mw.next.GetRegistration(jobID) |
| 74 | +} |
0 commit comments