This repository has been archived by the owner on Apr 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 19
/
joke-service.go
108 lines (91 loc) · 3.2 KB
/
joke-service.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
package services
import (
"github.com/google/uuid"
"github.com/vmware/transport-go/model"
"github.com/vmware/transport-go/service"
"net/http"
"reflect"
)
const (
JokeServiceChannel = "joke-service"
)
// Joke is a representation of what is returned by our providing JokeAPI.
type Joke struct {
Id string `json:"id"`
Joke string `json:"joke"`
Status int `json:"status"`
}
// JokeService will return a terrible joke, on demand.
type JokeService struct {
core service.FabricServiceCore
}
// NewJokeService will return an instance of JokeService.
func NewJokeService() *JokeService {
return &JokeService{}
}
// Init will fire when the service is being registered by the fabric, it passes a reference of the same core
// Passed through when implementing HandleServiceRequest
func (js *JokeService) Init(core service.FabricServiceCore) error {
// capture a reference to core for later.
js.core = core
// JokeService always returns JSON objects as responses.
core.SetDefaultJSONHeaders()
return nil
}
// HandleServiceRequest will listen for incoming requests with the command 'get-joke' and will then return a terrible
// Joke back to the requesting component.
func (js *JokeService) HandleServiceRequest(request *model.Request, core service.FabricServiceCore) {
switch request.Request {
case "get-joke":
js.getJoke(request, core)
default:
core.HandleUnknownRequest(request)
}
}
// getJoke calls our terrible joke service, and returns the response or error back to the requester.
func (js *JokeService) getJoke(request *model.Request, core service.FabricServiceCore) {
// make API call using inbuilt RestService to make network calls.
core.RestServiceRequest(&service.RestServiceRequest{
Uri: "https://icanhazdadjoke.com",
Method: "GET",
Headers: map[string]string{
"Accept": "application/json",
},
ResponseType: reflect.TypeOf(&Joke{}),
}, func(response *model.Response) {
// send back a successful joke.
core.SendResponse(request, response.Payload.(*Joke))
}, func(response *model.Response) {
// something went wrong with the API call, tell the requester.
fabricError := service.GetFabricError("Get Joke API Call Failed", response.ErrorCode, response.ErrorMessage)
core.SendErrorResponseWithPayload(request, response.ErrorCode, response.ErrorMessage, fabricError)
})
}
// GetRESTBridgeConfig returns a config for a REST endpoint for Jokes
func (js *JokeService) GetRESTBridgeConfig() []*service.RESTBridgeConfig {
return []*service.RESTBridgeConfig{
{
ServiceChannel: JokeServiceChannel,
Uri: "/rest/joke",
Method: http.MethodGet,
AllowHead: true,
AllowOptions: true,
FabricRequestBuilder: func(w http.ResponseWriter, r *http.Request) model.Request {
return model.Request{
Id: &uuid.UUID{},
Request: "get-joke",
BrokerDestination: nil,
}
},
},
}
}
// OnServerShutdown is not implemented in this service.
func (js *JokeService) OnServerShutdown() {}
// OnServiceReady has no functionality in this service, so it returns a fired channel.
func (js *JokeService) OnServiceReady() chan bool {
// ready right away, nothing to do.
readyChan := make(chan bool, 1)
readyChan <- true
return readyChan
}