Skip to content

Commit 4588d70

Browse files
committed
add skeleton gRPC server with HealthCheck
Relates to https://github.com/corgibytes/freshli-cli/issues/695
1 parent f827e9b commit 4588d70

File tree

3 files changed

+53
-5
lines changed

3 files changed

+53
-5
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,4 @@
2121
go.work
2222

2323
tmp/
24+
vendor/

agent/server/main.go

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"google.golang.org/grpc"
6+
"google.golang.org/grpc/health"
7+
healthgrpc "google.golang.org/grpc/health/grpc_health_v1"
8+
"google.golang.org/grpc/reflection"
9+
"log"
10+
"net"
11+
"os"
12+
"strconv"
13+
)
14+
15+
func main() {
16+
port, err := parsePortArgument()
17+
if err != nil || port < 0 {
18+
log.Fatalf("port is required: %v", err)
19+
return
20+
}
21+
log.Printf("gRPC server listening on %d", port)
22+
23+
lis, err := net.Listen("tcp", fmt.Sprintf(":%d", port))
24+
if err != nil {
25+
log.Fatalf("failed to listen: %v", err)
26+
}
27+
s := grpc.NewServer()
28+
healthcheck := health.NewServer()
29+
healthgrpc.RegisterHealthServer(s, healthcheck)
30+
31+
// TODO should this be only in dev/debug mode?
32+
reflection.Register(s)
33+
34+
if err := s.Serve(lis); err != nil {
35+
log.Fatalf("failed to serve: %v", err)
36+
}
37+
}
38+
39+
func parsePortArgument() (int, error) {
40+
if len(os.Args) > 1 {
41+
return strconv.Atoi(os.Args[1])
42+
}
43+
return -1, nil
44+
}

go.mod

+8-5
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ toolchain go1.21.1
77
require (
88
github.com/anchore/clio v0.0.0-20230823172630-c42d666061af
99
github.com/anchore/syft v0.91.0
10+
github.com/golang/protobuf v1.5.3
11+
google.golang.org/grpc v1.58.2
12+
google.golang.org/protobuf v1.31.0
1013
modernc.org/sqlite v1.25.0
1114
)
1215

@@ -80,7 +83,6 @@ require (
8083
github.com/go-restruct/restruct v1.2.0-alpha // indirect
8184
github.com/gogo/protobuf v1.3.2 // indirect
8285
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
83-
github.com/golang/protobuf v1.5.3 // indirect
8486
github.com/golang/snappy v0.0.4 // indirect
8587
github.com/google/go-cmp v0.5.9 // indirect
8688
github.com/google/go-containerregistry v0.16.1 // indirect
@@ -183,9 +185,8 @@ require (
183185
golang.org/x/text v0.13.0 // indirect
184186
golang.org/x/tools v0.13.0 // indirect
185187
golang.org/x/xerrors v0.0.0-20220907171357-04be3eba64a2 // indirect
186-
google.golang.org/genproto v0.0.0-20230410155749-daa745c078e1 // indirect
187-
google.golang.org/grpc v1.55.0 // indirect
188-
google.golang.org/protobuf v1.30.0 // indirect
188+
google.golang.org/genproto v0.0.0-20230711160842-782d3b101e98 // indirect
189+
google.golang.org/genproto/googleapis/rpc v0.0.0-20230711160842-782d3b101e98 // indirect
189190
gopkg.in/ini.v1 v1.67.0 // indirect
190191
gopkg.in/warnings.v0 v0.1.2 // indirect
191192
gopkg.in/yaml.v3 v3.0.1 // indirect
@@ -197,5 +198,7 @@ require (
197198
modernc.org/memory v1.6.0 // indirect
198199
modernc.org/opt v0.1.3 // indirect
199200
modernc.org/strutil v1.1.3 // indirect
200-
modernc.org/token v1.0.1 // indirect
201+
modernc.org/token v1.1.0 // indirect
201202
)
203+
204+
replace google.golang.org/grpc => github.com/grpc/grpc-go v1.58.2

0 commit comments

Comments
 (0)