first commit

This commit is contained in:
KS Jannette
2026-05-18 21:20:44 -04:00
commit 4b39726647
14 changed files with 355 additions and 0 deletions

View File

@@ -0,0 +1,56 @@
package httpjson
import (
"context"
"fmt"
"net/http"
"time"
"github.com/kjannette/go-http-server/internal/config"
"github.com/labstack/echo/v4"
"github.com/pkg/errors"
"go.uber.org/zap"
)
// Handler has services that are required by the handler.
type Handler struct {
logger *zap.Logger
}
// New creates a new Handler instance.
func New(logger *zap.Logger) (*echo.Echo, Handler) {
h := Handler{
logger: logger,
}
e := echo.New()
h.setupHealthCheckRoutes(e)
return e, h
}
// RunServer runs http server and listens until context is canceled.
func (h *Handler) RunServer(ctx context.Context, conf *config.Config, e *echo.Echo) error {
go func() {
<-ctx.Done()
timeout, cancel := context.WithTimeout(context.Background(), time.Duration(conf.API.ReadTimeout)*time.Second)
defer cancel()
_ = e.Shutdown(timeout) // try gracefully first
_ = e.Close() // immediate termination
}()
server := &http.Server{
Addr: fmt.Sprintf(":%d", conf.API.Port),
ReadTimeout: time.Duration(conf.API.ReadTimeout) * time.Second,
WriteTimeout: time.Duration(conf.API.WriteTimeout) * time.Second,
IdleTimeout: time.Duration(conf.API.IdleTimeout) * time.Second,
}
if err := e.StartServer(server); !errors.Is(err, http.ErrServerClosed) {
return errors.Wrap(err, "server error")
}
return nil
}

View File

@@ -0,0 +1,15 @@
package httpjson
import (
"net/http"
"github.com/labstack/echo/v4"
)
func (h *Handler) setupHealthCheckRoutes(app *echo.Echo) {
app.GET("/health", h.healthCheck)
}
func (h *Handler) healthCheck(c echo.Context) error {
return c.String(http.StatusOK, "OK")
}