mirror of
https://github.com/andwati/telecom-platform.git
synced 2026-10-23 13:12:16 +00:00
feat: Add Consul service discovery integration to API server with automatic registration
- Add discovery package import to server.go - Add fmt and strconv imports for service ID generation and port parsing - Add CONSUL_ADDR environment variable check to conditionally enable service discovery - Add discovery.NewServiceDiscovery initialization with error handling and logging - Add service registration with api-server name, dynamic port from config, and api/telecom tags - Add serviceID generation using api
This commit is contained in:
@@ -2,10 +2,12 @@ package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
"os/signal"
|
||||
"strconv"
|
||||
"syscall"
|
||||
"time"
|
||||
|
||||
@@ -15,6 +17,7 @@ import (
|
||||
|
||||
"github.com/nutcas3/telecom-platform/apps/api-server/internal/config"
|
||||
"github.com/nutcas3/telecom-platform/apps/api-server/internal/database"
|
||||
"github.com/nutcas3/telecom-platform/apps/api-server/internal/discovery"
|
||||
"github.com/nutcas3/telecom-platform/apps/api-server/internal/graphql"
|
||||
"github.com/nutcas3/telecom-platform/apps/api-server/internal/middleware"
|
||||
"github.com/nutcas3/telecom-platform/apps/api-server/internal/monitoring"
|
||||
@@ -30,6 +33,38 @@ func main() {
|
||||
}
|
||||
defer db.Close()
|
||||
|
||||
// Initialize service discovery (Consul)
|
||||
var serviceDiscovery *discovery.ServiceDiscovery
|
||||
consulAddr := os.Getenv("CONSUL_ADDR")
|
||||
if consulAddr != "" {
|
||||
sd, err := discovery.NewServiceDiscovery()
|
||||
if err != nil {
|
||||
log.Printf("Failed to initialize service discovery: %v", err)
|
||||
} else {
|
||||
serviceDiscovery = sd
|
||||
// Register this service with Consul
|
||||
port, err := strconv.Atoi(cfg.Server.Port)
|
||||
if err != nil {
|
||||
log.Printf("Failed to parse port: %v", err)
|
||||
port = 8080 // fallback
|
||||
}
|
||||
serviceID := fmt.Sprintf("api-server-%s", cfg.Server.Port)
|
||||
err = serviceDiscovery.Register(discovery.Service{
|
||||
ID: serviceID,
|
||||
Name: "api-server",
|
||||
Address: "0.0.0.0",
|
||||
Port: port,
|
||||
Tags: []string{"api", "telecom"},
|
||||
}, 10*time.Second)
|
||||
if err != nil {
|
||||
log.Printf("Failed to register service with Consul: %v", err)
|
||||
} else {
|
||||
log.Printf("Service registered with Consul: %s", serviceID)
|
||||
defer serviceDiscovery.Deregister(serviceID)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
prometheusCollector, metricsCollector, timeSeriesStorage := buildMetricsCollector(cfg)
|
||||
defer func() {
|
||||
if timeSeriesStorage != nil {
|
||||
|
||||
Reference in New Issue
Block a user