-
Notifications
You must be signed in to change notification settings - Fork 7
/
terminus.go
43 lines (34 loc) · 1.42 KB
/
terminus.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
package strategies
import (
"context"
"fmt"
"time"
pb "github.com/buoyantio/bb/gen"
"github.com/buoyantio/bb/service"
)
// TerminusStrategyName Name is the user-friendly name of this strategy
const TerminusStrategyName = "terminus"
// TerminusResponseTextArgName is the parameter used to supply the text to be returned by the TerminusStrategy
const TerminusResponseTextArgName = "response-text"
// TerminusStrategy is a strategy that always returns a pre-configured text message as the response to any requests.
type TerminusStrategy struct {
config *service.Config
}
// Do executes the request
func (s *TerminusStrategy) Do(_ context.Context, req *pb.TheRequest) (*pb.TheResponse, error) {
messageToReturn := fmt.Sprintf("terminus at [%d]", time.Now().Nanosecond())
if s.config.ExtraArguments[TerminusResponseTextArgName] != "" {
messageToReturn = s.config.ExtraArguments[TerminusResponseTextArgName]
}
resp := pb.TheResponse{
Payload: messageToReturn,
}
return &resp, nil
}
// NewTerminusStrategy creates a new TerminusStrategy
func NewTerminusStrategy(config *service.Config, servers []service.Server, clients []service.Client) (service.Strategy, error) {
if len(clients) != 0 || len(servers) == 0 {
return nil, fmt.Errorf("strategy [%s] requires at least one server port and exactly zero downstream services, but was configured as: %+v", TerminusStrategyName, config)
}
return &TerminusStrategy{config: config}, nil
}