46 lines
790 B
Go
46 lines
790 B
Go
package httpx
|
|
|
|
import (
|
|
"context"
|
|
"log/slog"
|
|
"net/http"
|
|
"time"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
type Server struct {
|
|
log *slog.Logger
|
|
http *http.Server
|
|
}
|
|
|
|
func New(log *slog.Logger, addr string) *Server {
|
|
gin.SetMode(gin.ReleaseMode)
|
|
r := gin.New()
|
|
r.Use(gin.Recovery())
|
|
r.GET("/healthz", func(c *gin.Context) {
|
|
c.JSON(http.StatusOK, gin.H{"status": "ok"})
|
|
})
|
|
return &Server{
|
|
log: log,
|
|
http: &http.Server{
|
|
Addr: addr,
|
|
Handler: r,
|
|
ReadHeaderTimeout: 5 * time.Second,
|
|
},
|
|
}
|
|
}
|
|
|
|
func (s *Server) Start() error {
|
|
s.log.Info("http listening", "addr", s.http.Addr)
|
|
err := s.http.ListenAndServe()
|
|
if err == http.ErrServerClosed {
|
|
return nil
|
|
}
|
|
return err
|
|
}
|
|
|
|
func (s *Server) Stop(ctx context.Context) error {
|
|
return s.http.Shutdown(ctx)
|
|
}
|