-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathmain.go
More file actions
83 lines (69 loc) · 2.18 KB
/
Copy pathmain.go
File metadata and controls
83 lines (69 loc) · 2.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
package main
import (
"context"
"net/http"
"os"
"os/signal"
"syscall"
"k8s.io/klog/v2"
"github.com/pluralsh/console/go/cloud-query/cmd/args"
"github.com/pluralsh/console/go/cloud-query/internal/pool"
"github.com/pluralsh/console/go/cloud-query/internal/server"
"github.com/pluralsh/console/go/cloud-query/internal/service"
pythontools "github.com/pluralsh/console/go/cloud-query/internal/tools/python"
)
func startHealthzHandler() {
http.HandleFunc("/healthz", healthz)
go func() {
klog.InfoS("starting /healthz endpoint", "address", ":8080")
err := http.ListenAndServe(":8080", nil)
if err != nil {
klog.Fatalf("failed to start healthz server: %v", err)
}
}()
}
func main() {
if len(os.Args) == 2 && os.Args[1] == "python-worker" {
if err := pythontools.NewWorker().Run(os.Stdin, os.Stdout); err != nil {
os.Exit(1)
}
return
}
startHealthzHandler()
toolQueryService, err := service.NewToolQueryService(context.Background())
if err != nil {
klog.Fatalf("failed to initialize tool query service: %v", err)
}
services := []service.Service{toolQueryService}
if args.DatabaseEnabled() {
p, err := pool.NewConnectionPool(args.DatabaseConnectionTTL())
if err != nil {
klog.Fatalf("failed to create connection pool: %v", err)
}
services = append(services, service.NewCloudQueryService(p))
} else {
klog.Info("database disabled: skipping CloudQuery service registration")
}
s, err := server.New(&server.Config{
Address: args.ServerAddress(),
TLSCertPath: args.ServerTLSCertPath(),
TLSKeyPath: args.ServerTLSKeyPath(),
EnableReflection: args.ServerEnableReflection(),
}, services...)
if err != nil {
klog.Fatalf("failed to create server: %v", err)
}
// Setup signal handling for graceful shutdown
ctx, cancel := context.WithCancel(context.Background())
go handleShutdown(cancel, s)
if err = s.Start(ctx); err != nil {
klog.Fatalf("failed to start server: %v", err)
}
}
func handleShutdown(cancel context.CancelFunc, s *server.Server) {
signalChan := make(chan os.Signal, 1)
signal.Notify(signalChan, syscall.SIGINT, syscall.SIGTERM)
<-signalChan
klog.Info("received shutdown signal, shutting down gracefully...")
cancel()
}