forked from vmware-archive/transport-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsample_services.go
146 lines (126 loc) · 4.35 KB
/
sample_services.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
// Copyright 2019-2020 VMware, Inc.
// SPDX-License-Identifier: BSD-2-Clause
package main
import (
"fmt"
"github.com/google/uuid"
"github.com/vmware/transport-go/model"
"github.com/vmware/transport-go/service"
"reflect"
"time"
)
const (
CalendarServiceChan = "calendar-service"
PongServiceChan = "services-PongService"
SimpleStreamChan = "simple-stream"
ServbotServiceChan = "servbot"
VmServiceChan = "vm-service"
VMWCloudServiceChan = "services-CloudServiceStatus"
)
type calendarService struct {}
func (cs *calendarService) HandleServiceRequest(
request *model.Request, core service.FabricServiceCore) {
switch request.Request {
case "time":
core.SendResponse(request, time.Now().Format("03:04:05.000 AM (-0700)"))
case "date":
core.SendResponse(request, time.Now().Format("Mon, 02 Jan 2006"))
default:
core.HandleUnknownRequest(request)
}
}
type pongService struct {}
func (cs *pongService) HandleServiceRequest(
request *model.Request, core service.FabricServiceCore) {
switch request.Request {
case "basic":
core.SendResponse(request, "Fabric Pong (Basic): Pong: " + request.Payload.(string))
case "full":
id := uuid.New()
tr := core.Bus().CreateAsyncTransaction()
tr.SendRequest(CalendarServiceChan, &model.Request{ Id: &id, Request: "date" })
tr.SendRequest(CalendarServiceChan, &model.Request{ Id: &id, Request: "time" })
tr.OnComplete(func(responses []*model.Message) {
core.SendResponse(request, fmt.Sprintf("Fabric Pong (Full): Calendar: %s %s / Pong: %s",
responses[0].Payload.(*model.Response).Payload,
responses[1].Payload.(*model.Response).Payload,
request.Payload))
})
tr.Commit()
default:
core.HandleUnknownRequest(request)
}
}
type simpleStreamTickerService struct {
online bool
}
func (fs *simpleStreamTickerService) HandleServiceRequest(request *model.Request, core service.FabricServiceCore) {
switch request.Request {
case "online":
fs.online = true
case "offline":
fs.online = false
default:
core.HandleUnknownRequest(request)
}
}
func (fs *simpleStreamTickerService) Init(core service.FabricServiceCore) error {
fs.online = true
ticker := time.NewTicker(500 * time.Millisecond)
go func() {
for {
<-ticker.C
if fs.online {
now := time.Now()
id := uuid.New()
response := &model.Response{
Payload: fmt.Sprintf("ping-%d", now.Nanosecond() + now.Second()),
Id: &id,
}
core.Bus().SendResponseMessage(SimpleStreamChan, response, nil)
}
}
}()
return nil
}
type Joke struct {
Id string `json:"id"`
Joke string `json:"joke"`
Status int `json:"status"`
}
type servbotService struct {}
func (s *servbotService) Init(core service.FabricServiceCore) error {
core.SetHeaders(map[string]string{
"Accept": "application/json",
})
return nil
}
func (s *servbotService) HandleServiceRequest(
request *model.Request, core service.FabricServiceCore) {
switch request.Request {
case "Joke":
core.RestServiceRequest(&service.RestServiceRequest{
Uri: "https://icanhazdadjoke.com",
Method: "GET",
ResponseType: reflect.TypeOf(&Joke{}),
}, func(response *model.Response) {
core.SendResponse(request, []string {response.Payload.(*Joke).Joke})
}, func(response *model.Response) {
core.SendErrorResponse(request, response.ErrorCode, response.ErrorMessage)
})
default:
core.HandleUnknownRequest(request)
}
}
type vmwCloudServiceService struct {}
func (s *vmwCloudServiceService) HandleServiceRequest(
request *model.Request, core service.FabricServiceCore) {
core.RestServiceRequest(&service.RestServiceRequest{
Uri: "https://status.vmware-services.io/api/v2/status.json",
Method: "GET",
}, func(response *model.Response) {
core.SendResponse(request, response.Payload)
}, func(response *model.Response) {
core.SendErrorResponse(request, response.ErrorCode, response.ErrorMessage)
})
}