All checks were successful
Build and Push Docker Image / build-and-push (push) Successful in 3m38s
44 lines
906 B
Go
44 lines
906 B
Go
package http
|
|
|
|
import (
|
|
"context"
|
|
"net/http"
|
|
|
|
"git.kanopo.dev/rhythm/rhythm-backend/internal/config"
|
|
"github.com/gin-gonic/gin"
|
|
"go.uber.org/fx"
|
|
"go.uber.org/zap"
|
|
)
|
|
|
|
func NewServer(lc fx.Lifecycle, cfg *config.Config, log *zap.SugaredLogger) *gin.Engine {
|
|
if cfg.AppEnv == "production" {
|
|
gin.SetMode(gin.ReleaseMode)
|
|
}
|
|
|
|
r := gin.New()
|
|
r.Use(gin.Recovery())
|
|
|
|
server := &http.Server{
|
|
Addr: ":" + cfg.ServerPort,
|
|
Handler: r,
|
|
}
|
|
|
|
lc.Append(fx.Hook{
|
|
OnStart: func(ctx context.Context) error {
|
|
log.Infof("Starting HTTP server on port %v", cfg.ServerPort)
|
|
go func() {
|
|
if err := server.ListenAndServe(); err != nil && err != http.ErrServerClosed {
|
|
log.Fatalf("error starting http server %v", err.Error())
|
|
}
|
|
}()
|
|
return nil
|
|
},
|
|
OnStop: func(ctx context.Context) error {
|
|
log.Info("Shutting down HTTP server\n")
|
|
return server.Shutdown(ctx)
|
|
},
|
|
})
|
|
|
|
return r
|
|
}
|