@@ -12,32 +12,10 @@ import (
1212func 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+ }
0 commit comments