github.com/vmware/transport-go@v1.3.4/plank/services/joke-service.go (about) 1 package services 2 3 import ( 4 "github.com/google/uuid" 5 "github.com/vmware/transport-go/model" 6 "github.com/vmware/transport-go/service" 7 "net/http" 8 "reflect" 9 ) 10 11 const ( 12 JokeServiceChannel = "joke-service" 13 ) 14 15 // Joke is a representation of what is returned by our providing JokeAPI. 16 type Joke struct { 17 Id string `json:"id"` 18 Joke string `json:"joke"` 19 Status int `json:"status"` 20 } 21 22 // JokeService will return a terrible joke, on demand. 23 type JokeService struct { 24 core service.FabricServiceCore 25 } 26 27 // NewJokeService will return an instance of JokeService. 28 func NewJokeService() *JokeService { 29 return &JokeService{} 30 } 31 32 // Init will fire when the service is being registered by the fabric, it passes a reference of the same core 33 // Passed through when implementing HandleServiceRequest 34 func (js *JokeService) Init(core service.FabricServiceCore) error { 35 36 // capture a reference to core for later. 37 js.core = core 38 39 // JokeService always returns JSON objects as responses. 40 core.SetDefaultJSONHeaders() 41 return nil 42 } 43 44 // HandleServiceRequest will listen for incoming requests with the command 'get-joke' and will then return a terrible 45 // Joke back to the requesting component. 46 func (js *JokeService) HandleServiceRequest(request *model.Request, core service.FabricServiceCore) { 47 switch request.Request { 48 case "get-joke": 49 js.getJoke(request, core) 50 default: 51 core.HandleUnknownRequest(request) 52 } 53 } 54 55 // getJoke calls our terrible joke service, and returns the response or error back to the requester. 56 func (js *JokeService) getJoke(request *model.Request, core service.FabricServiceCore) { 57 58 // make API call using inbuilt RestService to make network calls. 59 core.RestServiceRequest(&service.RestServiceRequest{ 60 Uri: "https://icanhazdadjoke.com", 61 Method: "GET", 62 Headers: map[string]string{ 63 "Accept": "application/json", 64 }, 65 ResponseType: reflect.TypeOf(&Joke{}), 66 }, func(response *model.Response) { 67 68 // send back a successful joke. 69 core.SendResponse(request, response.Payload.(*Joke)) 70 }, func(response *model.Response) { 71 72 // something went wrong with the API call, tell the requester. 73 fabricError := service.GetFabricError("Get Joke API Call Failed", response.ErrorCode, response.ErrorMessage) 74 core.SendErrorResponseWithPayload(request, response.ErrorCode, response.ErrorMessage, fabricError) 75 }) 76 } 77 78 // GetRESTBridgeConfig returns a config for a REST endpoint for Jokes 79 func (js *JokeService) GetRESTBridgeConfig() []*service.RESTBridgeConfig { 80 return []*service.RESTBridgeConfig{ 81 { 82 ServiceChannel: JokeServiceChannel, 83 Uri: "/rest/joke", 84 Method: http.MethodGet, 85 AllowHead: true, 86 AllowOptions: true, 87 FabricRequestBuilder: func(w http.ResponseWriter, r *http.Request) model.Request { 88 return model.Request{ 89 Id: &uuid.UUID{}, 90 Request: "get-joke", 91 BrokerDestination: nil, 92 } 93 }, 94 }, 95 } 96 } 97 98 // OnServerShutdown is not implemented in this service. 99 func (js *JokeService) OnServerShutdown() {} 100 101 // OnServiceReady has no functionality in this service, so it returns a fired channel. 102 func (js *JokeService) OnServiceReady() chan bool { 103 104 // ready right away, nothing to do. 105 readyChan := make(chan bool, 1) 106 readyChan <- true 107 return readyChan 108 }