mirror of
https://github.com/andwati/telecom-platform.git
synced 2026-10-23 05:02:17 +00:00
- Add gorm.io/driver/postgres v1.6.0 and gorm.io/gorm v1.31.1 dependencies with transitive dependencies (jackc/pgx/v5, jackc/puddle, jinzhu/inflection, jinzhu/now, golang.org/x/crypto/sync/text) - Replace NewInMemoryProfileStore with NewPostgresProfileStore using DATABASE_DSN environment variable - Add fatal error handling for missing DATABASE_DSN and Postgres connection failures - Add defer
62 lines
2.0 KiB
Go
62 lines
2.0 KiB
Go
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
"time"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
"github.com/joho/godotenv"
|
|
"github.com/nutcas3/telecom-platform/apps/carrier-connector/internal/config"
|
|
"github.com/nutcas3/telecom-platform/apps/carrier-connector/internal/es2"
|
|
handler "github.com/nutcas3/telecom-platform/apps/carrier-connector/internal/handler"
|
|
"github.com/nutcas3/telecom-platform/apps/carrier-connector/internal/repository"
|
|
"github.com/rs/zerolog"
|
|
)
|
|
|
|
func main() {
|
|
handler.Logger = zerolog.New(os.Stdout).With().
|
|
Timestamp().
|
|
Str("service", "carrier-connector").
|
|
Logger()
|
|
|
|
if err := godotenv.Load(); err != nil {
|
|
handler.Logger.Info().Msg("No .env file found, using system environment")
|
|
}
|
|
|
|
client := es2.NewES2Client(&config.ES2Config{
|
|
BaseURL: handler.GetEnv("SMDP_URL", "https://smdp.example.com"),
|
|
APIKey: handler.GetEnv("SMDP_API_KEY", "test-api-key"),
|
|
FunctionalityRequesterID: handler.GetEnv("FUNCTIONALITY_REQUESTER_ID", "carrier-connector"),
|
|
InsecureSkipVerify: handler.GetEnv("INSECURE_SKIP_VERIFY", "true") == "true",
|
|
})
|
|
port := handler.GetEnv("PORT", "8080")
|
|
|
|
router := gin.Default()
|
|
router.Use(gin.LoggerWithFormatter(func(p gin.LogFormatterParams) string {
|
|
return fmt.Sprintf("%s - [%s] \"%s %s %s %d %s \"%s\" %s\"\n",
|
|
p.ClientIP, p.TimeStamp.Format(time.RFC1123),
|
|
p.Method, p.Path, p.Request.Proto,
|
|
p.StatusCode, p.Latency, p.Request.UserAgent(), p.ErrorMessage,
|
|
)
|
|
}))
|
|
router.Use(gin.Recovery())
|
|
|
|
dsn := handler.GetEnv("DATABASE_DSN", "")
|
|
if dsn == "" {
|
|
handler.Logger.Fatal().Msg("DATABASE_DSN is required (no in-memory fallback)")
|
|
}
|
|
profileRepo, err := repository.NewPostgresProfileStore(dsn)
|
|
if err != nil {
|
|
handler.Logger.Fatal().Err(err).Msg("Failed to connect to Postgres")
|
|
}
|
|
defer profileRepo.Close()
|
|
|
|
setupRoutes(router, client, profileRepo)
|
|
|
|
handler.Logger.Info().Str("port", port).Msg("Carrier Connector API server starting")
|
|
if err := router.Run(":" + port); err != nil {
|
|
handler.Logger.Fatal().Err(err).Msg("Failed to start server")
|
|
}
|
|
}
|