gcx/backend/services/translate-service/cmd/server/main.go

62 lines
1.5 KiB
Go

package main
import (
"context"
"net/http"
"os"
"os/signal"
"syscall"
"time"
"github.com/gin-gonic/gin"
"go.uber.org/zap"
"github.com/genex/translate-service/internal/application/service"
"github.com/genex/translate-service/internal/interface/http/handler"
)
func main() {
logger, _ := zap.NewProduction()
defer logger.Sync()
port := os.Getenv("PORT")
if port == "" {
port = "3007"
}
svc := service.NewTranslateService()
h := handler.NewTranslateHandler(svc)
r := gin.New()
r.Use(gin.Recovery())
r.GET("/health", func(c *gin.Context) {
c.JSON(200, gin.H{"status": "ok", "service": "translate-service"})
})
r.GET("/health/ready", func(c *gin.Context) { c.JSON(200, gin.H{"status": "ready"}) })
r.GET("/health/live", func(c *gin.Context) { c.JSON(200, gin.H{"status": "alive"}) })
api := r.Group("/api/v1/translate")
api.POST("/mappings", h.CreateMapping)
api.GET("/resolve", h.Resolve)
api.GET("/user/:userId", h.GetByUser)
server := &http.Server{Addr: ":" + port, Handler: r, ReadTimeout: 15 * time.Second, WriteTimeout: 15 * time.Second}
go func() {
logger.Info("Translate Service starting", zap.String("port", port))
if err := server.ListenAndServe(); err != nil && err != http.ErrServerClosed {
logger.Fatal("Failed", zap.Error(err))
}
}()
quit := make(chan os.Signal, 1)
signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM)
<-quit
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
server.Shutdown(ctx)
logger.Info("Translate Service stopped")
}