Skip to content

Commit

Permalink
improve naming convention for http-service example (#261)
Browse files Browse the repository at this point in the history
* improve naming convention

* fix test
  • Loading branch information
aryanmehrotra authored Feb 9, 2024
1 parent 109d8d4 commit 4f11230
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 31 deletions.
59 changes: 31 additions & 28 deletions examples/using-http-service/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,32 +12,10 @@ import (
func main() {
a := gofr.New()

a.GET("/service", func(c *gofr.Context) (interface{}, error) {
var data = struct {
Fact string `json:"fact"`
Length int `json:"length"`
}{}

var service1 = c.GetHTTPService("service1")
resp, err := service1.Get(c, "fact", map[string]interface{}{
"max_length": 20,
})
if err != nil {
return nil, err
}

b, _ := io.ReadAll(resp.Body)
err = json.Unmarshal(b, &data)
if err != nil {
return nil, err
}

return data, nil
})

// HTTP service with Circuit Breaker config given, uses default health check
// HTTP service with Circuit Breaker config given, uses custom health check
// either of circuit breaker or health can be used as well, as both implement service.Options interface.
// Note: /breeds is not an actual health check endpoint for "https://catfact.ninja"
a.AddHTTPService("service1", "https://catfact.ninja",
a.AddHTTPService("cat-facts", "https://catfact.ninja",
&service.CircuitBreakerConfig{
Threshold: 4,
Interval: 1 * time.Second,
Expand All @@ -47,13 +25,38 @@ func main() {
},
)

// HTTP service with Health check config for custom health check endpoint
// Note: The health endpoint here /breed for "https://catfact.ninja" will give 404
a.AddHTTPService("service2", "https://catfact.ninja",
// service with improper health-check to test health check
a.AddHTTPService("fact-checker", "https://catfact.ninja",
&service.HealthConfig{
HealthEndpoint: "breed",
},
)

a.GET("/fact", Handler)

a.Run()
}

func Handler(c *gofr.Context) (any, error) {
var data = struct {
Fact string `json:"fact"`
Length int `json:"length"`
}{}

var catFacts = c.GetHTTPService("cat-facts")

resp, err := catFacts.Get(c, "fact", map[string]interface{}{
"max_length": 20,
})
if err != nil {
return nil, err
}

b, _ := io.ReadAll(resp.Body)
err = json.Unmarshal(b, &data)
if err != nil {
return nil, err
}

return data, nil
}
6 changes: 3 additions & 3 deletions examples/using-http-service/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,15 @@ func Test_main(t *testing.T) {
}{
{
desc: "simple service handler",
path: "/service",
path: "/fact",
expectedRes: `{"data":{"fact":"Cats have 3 eyelids.","length":20}}` + "\n",
statusCode: 200,
},
{
desc: "health check",
path: "/.well-known/health",
expectedRes: `{"data":{"service1":{"status":"UP","details":{"host":"catfact.ninja"}},` +
`"service2":{"status":"DOWN","details":{"error":"service down","host":"catfact.ninja"}}}}` + "\n",
expectedRes: `{"data":{"cat-facts":{"status":"UP","details":{"host":"catfact.ninja"}},` +
`"fact-checker":{"status":"DOWN","details":{"error":"service down","host":"catfact.ninja"}}}}` + "\n",
statusCode: 200,
},
}
Expand Down

0 comments on commit 4f11230

Please sign in to comment.