Skip to content

Commit 324bace

Browse files
committed
feat: health check api
1 parent d1e7f4a commit 324bace

File tree

5 files changed

+35
-1
lines changed

5 files changed

+35
-1
lines changed

api/api_impl/server.go

+7
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ import (
99
"net/url"
1010
"strings"
1111

12+
"github.com/hellofresh/health-go/v5"
13+
1214
"github.com/getkin/kin-openapi/openapi3"
1315
"github.com/getkin/kin-openapi/routers/gorillamux"
1416

@@ -74,6 +76,11 @@ func SetupAPI(lc fx.Lifecycle, apiConfig *config.APIConfig, secretStore crypt.Se
7476

7577
api.HandlerFromMuxWithBaseURL(controller, apiRouter, APIV1Prefix)
7678
r.Handle("/api/docs/*", http.StripPrefix("/api/docs", swaggerui.Handler(raw)))
79+
h, _ := health.New(health.WithComponent(health.Component{
80+
Name: "myservice",
81+
Version: "v1.0",
82+
}))
83+
r.Get("/status", h.HandlerFunc)
7784

7885
url, err := url.Parse(apiConfig.Listen)
7986
if err != nil {

go.mod

+2-1
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ require (
117117
github.com/go-logr/logr v1.4.1 // indirect
118118
github.com/go-logr/stdr v1.2.2 // indirect
119119
github.com/go-openapi/jsonpointer v0.19.6 // indirect
120-
github.com/go-sql-driver/mysql v1.7.0 // indirect
120+
github.com/go-sql-driver/mysql v1.7.1 // indirect
121121
github.com/gogo/protobuf v1.3.2 // indirect
122122
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
123123
github.com/golang/protobuf v1.5.3 // indirect
@@ -134,6 +134,7 @@ require (
134134
github.com/hashicorp/golang-lru v1.0.2 // indirect
135135
github.com/hashicorp/golang-lru/v2 v2.0.7 // indirect
136136
github.com/hashicorp/hcl v1.0.0 // indirect
137+
github.com/hellofresh/health-go/v5 v5.5.2 // indirect
137138
github.com/imdario/mergo v0.3.15 // indirect
138139
github.com/inconshreveable/mousetrap v1.1.0 // indirect
139140
github.com/invopop/yaml v0.2.0 // indirect

go.sum

+4
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,8 @@ github.com/go-openapi/swag v0.22.4 h1:QLMzNJnMGPRNDCbySlcj1x01tzU8/9LTTL9hZZZogB
241241
github.com/go-openapi/swag v0.22.4/go.mod h1:UzaqsxGiab7freDnrUUra0MwWfN/q7tE4j+VcZ0yl14=
242242
github.com/go-sql-driver/mysql v1.7.0 h1:ueSltNNllEqE3qcWBTD0iQd3IpL/6U+mJxLkazJ7YPc=
243243
github.com/go-sql-driver/mysql v1.7.0/go.mod h1:OXbVy3sEdcQ2Doequ6Z5BW6fXNQTmx+9S1MCJN5yJMI=
244+
github.com/go-sql-driver/mysql v1.7.1 h1:lUIinVbN1DY0xBg0eMOzmmtGoHwWBbvnWubQUrtU8EI=
245+
github.com/go-sql-driver/mysql v1.7.1/go.mod h1:OXbVy3sEdcQ2Doequ6Z5BW6fXNQTmx+9S1MCJN5yJMI=
244246
github.com/go-task/slim-sprig v0.0.0-20230315185526-52ccab3ef572 h1:tfuBGBXKqDEevZMzYi5KSi8KkcZtzBcTgAUUtapy0OI=
245247
github.com/go-task/slim-sprig v0.0.0-20230315185526-52ccab3ef572/go.mod h1:9Pwr4B2jHnOSGXyyzV8ROjYa2ojvAY6HCGYYfMoC3Ls=
246248
github.com/go-test/deep v1.0.8/go.mod h1:5C2ZWiW0ErCdrYzpqxLbTX7MG14M9iiw8DgHncVwcsE=
@@ -369,6 +371,8 @@ github.com/hashicorp/golang-lru/v2 v2.0.7 h1:a+bsQ5rvGLjzHuww6tVxozPZFVghXaHOwFs
369371
github.com/hashicorp/golang-lru/v2 v2.0.7/go.mod h1:QeFd9opnmA6QUJc5vARoKUSoFhyfM2/ZepoAG6RGpeM=
370372
github.com/hashicorp/hcl v1.0.0 h1:0Anlzjpi4vEasTeNFn2mLJgTSwt0+6sfsiTG8qcWGx4=
371373
github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ=
374+
github.com/hellofresh/health-go/v5 v5.5.2 h1:h17lycd6foR2AUcd2tLYRKyU9YM7oyVtTAqUDqcM5JE=
375+
github.com/hellofresh/health-go/v5 v5.5.2/go.mod h1:+ENHPehFhTo9xwpJ/vQIe4iP2Uh3a4fUIcA6Rl8DqJ0=
372376
github.com/hnlq715/golang-lru v0.4.0 h1:gyo/wIvLE6Upf1wucAfwTjpR+BQ5Lli2766H2MnNPv0=
373377
github.com/hnlq715/golang-lru v0.4.0/go.mod h1:RBkgDAtlu0SgTPvpb4VW2/RQnkCBMRD3Lr6B9RhsAS8=
374378
github.com/hokaccha/go-prettyjson v0.0.0-20211117102719-0474bc63780f h1:7LYC+Yfkj3CTRcShK0KOL/w6iTiKyqqBA9a41Wnggw8=

integrationtest/root_test.go

+2
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ func TestSpec(t *testing.T) {
1212
urlStr, cancel := SetupDaemon(t, ctx)
1313
defer cancel()
1414

15+
convey.Convey("status test", t, StatusSpec(ctx, urlStr))
16+
1517
convey.Convey("user test", t, UserSpec(ctx, urlStr))
1618
convey.Convey("repo test", t, RepoSpec(ctx, urlStr))
1719
convey.Convey("branch test", t, BranchSpec(ctx, urlStr))

integrationtest/status_test.go

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package integrationtest
2+
3+
import (
4+
"context"
5+
"net/http"
6+
"net/url"
7+
8+
"github.com/smartystreets/goconvey/convey"
9+
)
10+
11+
func StatusSpec(ctx context.Context, urlStr string) func(c convey.C) {
12+
return func(c convey.C) {
13+
url, err := url.Parse(urlStr)
14+
convey.ShouldBeNil(err)
15+
url.Path = "/status"
16+
resp, err := http.Get(url.String())
17+
convey.ShouldBeNil(err)
18+
convey.ShouldEqual(resp.StatusCode, http.StatusOK)
19+
}
20+
}

0 commit comments

Comments
 (0)