Skip to content

Commit e75ef09

Browse files
authored
feat: implement grpc health service checking database connection (#4499)
1 parent fc7dc58 commit e75ef09

File tree

2 files changed

+25
-0
lines changed

2 files changed

+25
-0
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package v1
2+
3+
import (
4+
"context"
5+
6+
"google.golang.org/grpc/codes"
7+
"google.golang.org/grpc/health/grpc_health_v1"
8+
"google.golang.org/grpc/status"
9+
10+
"github.com/usememos/memos/store"
11+
)
12+
13+
func (s *APIV1Service) Check(ctx context.Context,
14+
_ *grpc_health_v1.HealthCheckRequest) (*grpc_health_v1.HealthCheckResponse, error) {
15+
history, err := s.Store.GetDriver().FindMigrationHistoryList(ctx, &store.FindMigrationHistory{})
16+
if err != nil || len(history) == 0 {
17+
return nil, status.Errorf(codes.Unavailable, "not available")
18+
}
19+
20+
return &grpc_health_v1.HealthCheckResponse{Status: grpc_health_v1.HealthCheckResponse_SERVING}, nil
21+
}

server/router/api/v1/v1.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"github.com/labstack/echo/v4/middleware"
1212
"google.golang.org/grpc"
1313
"google.golang.org/grpc/credentials/insecure"
14+
"google.golang.org/grpc/health/grpc_health_v1"
1415
"google.golang.org/grpc/reflection"
1516

1617
v1pb "github.com/usememos/memos/proto/gen/api/v1"
@@ -19,6 +20,8 @@ import (
1920
)
2021

2122
type APIV1Service struct {
23+
grpc_health_v1.UnimplementedHealthServer
24+
2225
v1pb.UnimplementedWorkspaceServiceServer
2326
v1pb.UnimplementedWorkspaceSettingServiceServer
2427
v1pb.UnimplementedAuthServiceServer
@@ -46,6 +49,7 @@ func NewAPIV1Service(secret string, profile *profile.Profile, store *store.Store
4649
Store: store,
4750
grpcServer: grpcServer,
4851
}
52+
grpc_health_v1.RegisterHealthServer(grpcServer, apiv1Service)
4953
v1pb.RegisterWorkspaceServiceServer(grpcServer, apiv1Service)
5054
v1pb.RegisterWorkspaceSettingServiceServer(grpcServer, apiv1Service)
5155
v1pb.RegisterAuthServiceServer(grpcServer, apiv1Service)

0 commit comments

Comments
 (0)