@@ -5,43 +5,61 @@ import (
55 "context"
66 "fmt"
77 "log"
8+ "net/http"
89 "os"
910 "os/signal"
11+ "sync/atomic"
1012 "syscall"
1113
1214 "github.com/hasirciogli/xdatabase-proxy/pkg/kubernetes"
1315)
1416
15- // func main() {
16- // // Create proxies
17- // postgresProxy := postgresql.NewPostgresProxy(3001, "localhost", 5432)
18- // mysqlProxy := mysql.NewMySQLProxy(3002, "localhost", 3306)
19- // mongoProxy := mongodb.NewMongoDBProxy(3003, "localhost", 27017)
20-
21- // // Start PostgreSQL proxy
22- // go func() {
23- // if err := postgresProxy.Start(postgresProxy.ListenPort); err != nil {
24- // log.Printf("PostgreSQL proxy error: %v", err)
25- // }
26- // }()
27-
28- // // Start MySQL proxy
29- // go func() {
30- // if err := mysqlProxy.Start(mysqlProxy.ListenPort); err != nil {
31- // log.Printf("MySQL proxy error: %v", err)
32- // }
33- // }()
34-
35- // // Start MongoDB proxy
36- // if err := mongoProxy.Start(mongoProxy.ListenPort); err != nil {
37- // log.Printf("MongoDB proxy error: %v", err)
38- // }
39- // }
40-
41- // sadece postgres proxy
17+ var (
18+ isReady atomic.Bool
19+ isHealthy atomic.Bool
20+ )
21+
22+ func setupHealthChecks () {
23+ // Set initial state
24+ isHealthy .Store (true )
25+ isReady .Store (true )
26+
27+ // Health check endpoint
28+ http .HandleFunc ("/healthz" , func (w http.ResponseWriter , r * http.Request ) {
29+ if isHealthy .Load () {
30+ w .WriteHeader (http .StatusOK )
31+ w .Write ([]byte ("healthy" ))
32+ return
33+ }
34+ w .WriteHeader (http .StatusServiceUnavailable )
35+ w .Write ([]byte ("unhealthy" ))
36+ })
37+
38+ // Readiness check endpoint
39+ http .HandleFunc ("/ready" , func (w http.ResponseWriter , r * http.Request ) {
40+ if isReady .Load () {
41+ w .WriteHeader (http .StatusOK )
42+ w .Write ([]byte ("ready" ))
43+ return
44+ }
45+ w .WriteHeader (http .StatusServiceUnavailable )
46+ w .Write ([]byte ("not ready" ))
47+ })
48+
49+ // Start HTTP server for health checks
50+ go func () {
51+ if err := http .ListenAndServe (":80" , nil ); err != nil {
52+ log .Printf ("Health check server error: %v" , err )
53+ }
54+ }()
55+ }
56+
4257func main () {
58+ // Setup health check endpoints (!!!CURRENTLY NOT USED!!!)
59+ setupHealthChecks ()
60+
4361 // Create a new Kubernetes client with specific context
44- contextName := os .Getenv ("KUBE_CONTEXT" ) // Take context name from environment variable
62+ contextName := os .Getenv ("KUBE_CONTEXT" )
4563 if contextName == "" {
4664 contextName = "local-test"
4765 }
@@ -60,6 +78,8 @@ func main() {
6078 log .Printf ("Service Info: Name=%s, Namespace=%s, DB Type=%s, PooledConnection=%v, ClusterDNS=%s" ,
6179 svc .Name , svc .Namespace , svc .DatabaseType , svc .PooledConnection , svc .ClusterDNS )
6280 }
81+ // Mark as ready once we've successfully started watching services
82+ isReady .Store (true )
6383 }); err != nil {
6484 log .Fatalf ("Failed to start watching: %v" , err )
6585 }
@@ -71,5 +91,9 @@ func main() {
7191 signal .Notify (sigChan , syscall .SIGINT , syscall .SIGTERM )
7292 <- sigChan
7393
94+ // Mark as not ready and unhealthy during shutdown
95+ isReady .Store (false )
96+ isHealthy .Store (false )
97+
7498 log .Println ("Shutting down..." )
7599}
0 commit comments