diff --git a/cache.go b/cache.go index 4ff1819..ff8f384 100644 --- a/cache.go +++ b/cache.go @@ -17,7 +17,7 @@ func enableCache() { var err error cfg.Cache.CacheClient = cache.New(time.Duration(cfg.Cache.CacheTTL) * time.Second * 2) if err != nil { - fmt.Println(">> Error while creating cache client;", "error", err.Error()) + cfg.Logger.Critical("Can't create cache client", map[string]interface{}{"error": err.Error()}) panic(err) } } diff --git a/go.mod b/go.mod index b694688..0637401 100644 --- a/go.mod +++ b/go.mod @@ -9,6 +9,7 @@ require ( github.com/graphql-go/graphql v0.8.1 github.com/json-iterator/go v1.1.12 github.com/lukaszraczylo/ask v0.0.0-20230927103145-2ff1123b4415 + github.com/lukaszraczylo/go-simple-graphql v1.1.31 github.com/telegram-bot-app/libpack v0.0.0-20231007021518-909ce2741a36 ) @@ -16,6 +17,7 @@ require ( dario.cat/mergo v1.0.0 // indirect github.com/VictoriaMetrics/metrics v1.24.0 // indirect github.com/andybalholm/brotli v1.0.5 // indirect + github.com/avast/retry-go/v4 v4.5.0 // indirect github.com/google/uuid v1.3.1 // indirect github.com/gookit/color v1.5.4 // indirect github.com/klauspost/compress v1.17.0 // indirect @@ -35,6 +37,7 @@ require ( github.com/valyala/tcplisten v1.0.0 // indirect github.com/wI2L/jsondiff v0.4.0 // indirect github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e // indirect + golang.org/x/net v0.16.0 // indirect golang.org/x/sync v0.4.0 // indirect golang.org/x/sys v0.13.0 // indirect golang.org/x/term v0.13.0 // indirect diff --git a/go.sum b/go.sum index a70a9d4..fc42687 100644 --- a/go.sum +++ b/go.sum @@ -6,6 +6,8 @@ github.com/akyoto/cache v1.0.6 h1:5XGVVYoi2i+DZLLPuVIXtsNIJ/qaAM16XT0LaBaXd2k= github.com/akyoto/cache v1.0.6/go.mod h1:WfxTRqKhfgAG71Xh6E3WLpjhBtZI37O53G4h5s+3iM4= github.com/andybalholm/brotli v1.0.5 h1:8uQZIdzKmjc/iuPu7O2ioW48L81FgatrcpfFmiq/cCs= github.com/andybalholm/brotli v1.0.5/go.mod h1:fO7iG3H7G2nSZ7m0zPUDn85XEX2GTukHGRSepvi9Eig= +github.com/avast/retry-go/v4 v4.5.0 h1:QoRAZZ90cj5oni2Lsgl2GW8mNTnUCnmpx/iKpwVisHg= +github.com/avast/retry-go/v4 v4.5.0/go.mod h1:7hLEXp0oku2Nir2xBAsg0PTphp9z71bN5Aq1fboC3+I= github.com/buger/jsonparser v1.1.1 h1:2PnMjfWD7wBILjqQbt530v576A/cAbQvEW9gGIpYMUs= github.com/buger/jsonparser v1.1.1/go.mod h1:6RYKKt7H4d4+iWqouImQ9R2FZql3VbhNgx27UK13J/0= github.com/coreos/go-systemd/v22 v22.5.0/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc= @@ -30,6 +32,8 @@ github.com/klauspost/compress v1.17.0 h1:Rnbp4K9EjcDuVuHtd0dgA4qNuv9yKDYKK1ulpJw github.com/klauspost/compress v1.17.0/go.mod h1:ntbaceVETuRiXiv4DpjP66DpAtAGkEQskQzEyD//IeE= github.com/lukaszraczylo/ask v0.0.0-20230927103145-2ff1123b4415 h1:lvI8Wlbg4PxkRcg2f10wgoaRpfN19v+YdRek3+dLtlM= github.com/lukaszraczylo/ask v0.0.0-20230927103145-2ff1123b4415/go.mod h1:M+UVdyqZs++xtEPrascaVmZdOMhCnxjZ2SgH+xHpR0c= +github.com/lukaszraczylo/go-simple-graphql v1.1.31 h1:UA3f8M1cV+XnO8UZlAqveW0qF/2NN512eB/gRqe+BHs= +github.com/lukaszraczylo/go-simple-graphql v1.1.31/go.mod h1:MyftQ8jTdtkYImPXJpHoxz6+E53Ydv+7q9+Jr+eT8WU= github.com/lukaszraczylo/pandati v0.0.29 h1:WUEWm1+hWjE5KJbIL8OctG00x2dk4XKGJSlrjhxZ55k= github.com/lukaszraczylo/pandati v0.0.29/go.mod h1:+DyTWKFaXd+jIfe7GW5w2S5PyTko/RXxMyOa+Vl713A= github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA= @@ -77,6 +81,8 @@ github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e h1:JVG44RsyaB9T2KIHavM github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e/go.mod h1:RbqR21r5mrJuqunuUZ/Dhy/avygyECGrLceyNeo4LiM= golang.org/x/exp v0.0.0-20231006140011-7918f672742d h1:jtJma62tbqLibJ5sFQz8bKtEM8rJBtfilJ2qTU199MI= golang.org/x/exp v0.0.0-20231006140011-7918f672742d/go.mod h1:ldy0pHrwJyGW56pPQzzkH36rKxoZW1tw7ZJpeKx+hdo= +golang.org/x/net v0.16.0 h1:7eBu7KsSvFDtSXUIDbh3aqlK4DPsZ1rByC8PFfBThos= +golang.org/x/net v0.16.0/go.mod h1:NxSsAGuq816PNPmqtQdLE42eU2Fs7NoRIZrHJAlaCOE= golang.org/x/sync v0.4.0 h1:zxkM55ReGkDlKSM+Fu41A+zmbZuaPVbGMzvvdUPznYQ= golang.org/x/sync v0.4.0/go.mod h1:FU7BRWz2tNW+3quACPkgCx/L+uEAv1htQ0V83Z9Rj+Y= golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= diff --git a/main.go b/main.go index bc68cc7..1c93906 100644 --- a/main.go +++ b/main.go @@ -2,6 +2,7 @@ package main import ( "github.com/gookit/goutil/envutil" + graphql "github.com/lukaszraczylo/go-simple-graphql" libpack_config "github.com/telegram-bot-app/libpack/config" libpack_logging "github.com/telegram-bot-app/libpack/logging" ) @@ -18,6 +19,8 @@ func parseConfig() { c.Cache.CacheEnable = envutil.GetBool("CACHE_ENABLE", false) c.Cache.CacheTTL = envutil.GetInt("CACHE_TTL", 60) c.Logger = libpack_logging.NewLogger() + c.Client.GQLClient = graphql.NewConnection() + c.Client.GQLClient.SetEndpoint(c.Server.HostGraphQL) cfg = &c enableCache() // takes close to no resources, but can be used with dynamic query cache } diff --git a/proxy.go b/proxy.go index 5ac2afb..2bd335d 100644 --- a/proxy.go +++ b/proxy.go @@ -2,7 +2,6 @@ package main import ( "crypto/tls" - "fmt" fiber "github.com/gofiber/fiber/v2" "github.com/gofiber/fiber/v2/middleware/proxy" @@ -19,7 +18,7 @@ func proxyTheRequest(c *fiber.Ctx) error { err := proxy.DoRedirects(c, cfg.Server.HostGraphQL, 3) if err != nil { - fmt.Println("Can't proxy the request: ", err) + cfg.Logger.Error("Can't proxy the request", map[string]interface{}{"error": err.Error()}) cfg.Monitoring.Increment(libpack_monitoring.MetricsFailed, nil) return err } diff --git a/server.go b/server.go index 0cd901e..f45ec55 100644 --- a/server.go +++ b/server.go @@ -25,12 +25,19 @@ func StartHTTPProxy() { server.Get("/healthz", healthCheck) err := server.Listen(fmt.Sprintf(":%d", cfg.Server.PortGraphQL)) if err != nil { - fmt.Println("Can't start the service: ", err) + cfg.Logger.Critical("Can't start the service", map[string]interface{}{"error": err.Error()}) } } func healthCheck(c *fiber.Ctx) error { - return c.SendString("OK") + query := `{ __typename }` + _, err := cfg.Client.GQLClient.Query(query, nil, nil) + if err != nil { + cfg.Logger.Error("Can't reach the GraphQL server", map[string]interface{}{"error": err.Error()}) + cfg.Monitoring.Increment(libpack_monitoring.MetricsFailed, nil) + return c.SendStatus(500) + } + return c.SendStatus(200) } func processGraphQLRequest(c *fiber.Ctx) error { diff --git a/struct_config.go b/struct_config.go index 1ab544b..5b290fe 100644 --- a/struct_config.go +++ b/struct_config.go @@ -2,6 +2,7 @@ package main import ( "github.com/akyoto/cache" + graphql "github.com/lukaszraczylo/go-simple-graphql" libpack_logging "github.com/telegram-bot-app/libpack/logging" libpack_monitoring "github.com/telegram-bot-app/libpack/monitoring" ) @@ -20,6 +21,7 @@ type config struct { Client struct { JWTUserClaimPath string + GQLClient *graphql.BaseClient } Cache struct {