Skip to content
This repository has been archived by the owner on Dec 5, 2023. It is now read-only.

Commit

Permalink
Initial commit of expansion
Browse files Browse the repository at this point in the history
Changed swagger spec
  • Loading branch information
Vishal Lal committed Oct 20, 2016
1 parent 059758c commit 3b45348
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 13 deletions.
24 changes: 17 additions & 7 deletions api-spec/payment.json
Original file line number Diff line number Diff line change
Expand Up @@ -64,16 +64,26 @@
"title": "Health",
"type": "object",
"properties": {
"status": {
"type": "string"
},
"time": {
"type": "string"
"health": {
"type": "array",
"items": {
"type": "object",
"properties": {
"service": {
"type": "string"
},
"status": {
"type": "string"
},
"time": {
"type": "string"
}
}
}
}
},
"required": [
"status",
"time"
"health"
]
},
"paymentAuth": {
Expand Down
8 changes: 3 additions & 5 deletions endpoints.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package payment

import (
"time"

"github.com/go-kit/kit/endpoint"
"golang.org/x/net/context"
)
Expand Down Expand Up @@ -34,7 +32,8 @@ func MakeAuthoriseEndpoint(s Service) endpoint.Endpoint {
// MakeHealthEndpoint returns current health of the given service.
func MakeHealthEndpoint(s Service) endpoint.Endpoint {
return func(ctx context.Context, request interface{}) (response interface{}, err error) {
return healthResponse{Status: "OK", Time: time.Now().String()}, nil
health := s.Health()
return healthResponse{Health: health}, nil
}
}

Expand All @@ -55,6 +54,5 @@ type healthRequest struct {
}

type healthResponse struct {
Status string `json:"status"`
Time string `json:"time"`
Health []Health `json:"health"`
}
20 changes: 20 additions & 0 deletions middlewares.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,17 @@ func (mw loggingMiddleware) Authorise(amount float32) (auth Authorisation, err e
return mw.next.Authorise(amount)
}

func (mw loggingMiddleware) Health() (health []Health) {
defer func(begin time.Time) {
mw.logger.Log(
"method", "Health",
"result", len(health),
"took", time.Since(begin),
)
}(time.Now())
return mw.next.Health()
}

type instrumentingService struct {
requestCount metrics.Counter
requestLatency metrics.Histogram
Expand All @@ -58,3 +69,12 @@ func (s *instrumentingService) Authorise(amount float32) (auth Authorisation, er

return s.Service.Authorise(amount)
}

func (s *instrumentingService) Health() []Health {
defer func(begin time.Time) {
s.requestCount.With("method", "health").Add(1)
s.requestLatency.With("method", "health").Observe(time.Since(begin).Seconds())
}(time.Now())

return s.Service.Health()
}
19 changes: 18 additions & 1 deletion service.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,25 @@
package payment

import "errors"
import (
"errors"
"time"
)

type Service interface {
Authorise(total float32) (Authorisation, error) // GET /paymentAuth
Health() []Health // GET /health
}

type Authorisation struct {
Authorised bool `json:"authorised"`
}

type Health struct {
Service string `json:"service"`
Status string `json:"status"`
Time string `json:"time"`
}

// NewFixedService returns a simple implementation of the Service interface,
// fixed over a predefined set of socks and tags. In a real service you'd
// probably construct this with a database handle to your socks DB, etc.
Expand Down Expand Up @@ -39,4 +49,11 @@ func (s *service) Authorise(amount float32) (Authorisation, error) {
}, nil
}

func (s *service) Health() []Health {
var health []Health
app := Health{"payment", "OK", time.Now().String()}
health = append(health, app)
return health
}

var ErrInvalidPaymentAmount = errors.New("Invalid payment amount")

0 comments on commit 3b45348

Please sign in to comment.