Skip to content

Commit 4f11230

Browse files
improve naming convention for http-service example (#261)
* improve naming convention * fix test
1 parent 109d8d4 commit 4f11230

File tree

2 files changed

+34
-31
lines changed

2 files changed

+34
-31
lines changed

examples/using-http-service/main.go

Lines changed: 31 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -12,32 +12,10 @@ import (
1212
func main() {
1313
a := gofr.New()
1414

15-
a.GET("/service", func(c *gofr.Context) (interface{}, error) {
16-
var data = struct {
17-
Fact string `json:"fact"`
18-
Length int `json:"length"`
19-
}{}
20-
21-
var service1 = c.GetHTTPService("service1")
22-
resp, err := service1.Get(c, "fact", map[string]interface{}{
23-
"max_length": 20,
24-
})
25-
if err != nil {
26-
return nil, err
27-
}
28-
29-
b, _ := io.ReadAll(resp.Body)
30-
err = json.Unmarshal(b, &data)
31-
if err != nil {
32-
return nil, err
33-
}
34-
35-
return data, nil
36-
})
37-
38-
// HTTP service with Circuit Breaker config given, uses default health check
15+
// HTTP service with Circuit Breaker config given, uses custom health check
16+
// either of circuit breaker or health can be used as well, as both implement service.Options interface.
3917
// Note: /breeds is not an actual health check endpoint for "https://catfact.ninja"
40-
a.AddHTTPService("service1", "https://catfact.ninja",
18+
a.AddHTTPService("cat-facts", "https://catfact.ninja",
4119
&service.CircuitBreakerConfig{
4220
Threshold: 4,
4321
Interval: 1 * time.Second,
@@ -47,13 +25,38 @@ func main() {
4725
},
4826
)
4927

50-
// HTTP service with Health check config for custom health check endpoint
51-
// Note: The health endpoint here /breed for "https://catfact.ninja" will give 404
52-
a.AddHTTPService("service2", "https://catfact.ninja",
28+
// service with improper health-check to test health check
29+
a.AddHTTPService("fact-checker", "https://catfact.ninja",
5330
&service.HealthConfig{
5431
HealthEndpoint: "breed",
5532
},
5633
)
5734

35+
a.GET("/fact", Handler)
36+
5837
a.Run()
5938
}
39+
40+
func Handler(c *gofr.Context) (any, error) {
41+
var data = struct {
42+
Fact string `json:"fact"`
43+
Length int `json:"length"`
44+
}{}
45+
46+
var catFacts = c.GetHTTPService("cat-facts")
47+
48+
resp, err := catFacts.Get(c, "fact", map[string]interface{}{
49+
"max_length": 20,
50+
})
51+
if err != nil {
52+
return nil, err
53+
}
54+
55+
b, _ := io.ReadAll(resp.Body)
56+
err = json.Unmarshal(b, &data)
57+
if err != nil {
58+
return nil, err
59+
}
60+
61+
return data, nil
62+
}

examples/using-http-service/main_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,15 @@ func Test_main(t *testing.T) {
2424
}{
2525
{
2626
desc: "simple service handler",
27-
path: "/service",
27+
path: "/fact",
2828
expectedRes: `{"data":{"fact":"Cats have 3 eyelids.","length":20}}` + "\n",
2929
statusCode: 200,
3030
},
3131
{
3232
desc: "health check",
3333
path: "/.well-known/health",
34-
expectedRes: `{"data":{"service1":{"status":"UP","details":{"host":"catfact.ninja"}},` +
35-
`"service2":{"status":"DOWN","details":{"error":"service down","host":"catfact.ninja"}}}}` + "\n",
34+
expectedRes: `{"data":{"cat-facts":{"status":"UP","details":{"host":"catfact.ninja"}},` +
35+
`"fact-checker":{"status":"DOWN","details":{"error":"service down","host":"catfact.ninja"}}}}` + "\n",
3636
statusCode: 200,
3737
},
3838
}

0 commit comments

Comments
 (0)