add initial application structure with configuration, logging, and health check endpoints
This commit is contained in:
49
internal/infra/http/http.go
Normal file
49
internal/infra/http/http.go
Normal file
@@ -0,0 +1,49 @@
|
||||
package http
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"github.com/labstack/echo/v4"
|
||||
"go.uber.org/fx"
|
||||
"go.uber.org/zap"
|
||||
|
||||
"epic-ent/internal/config"
|
||||
)
|
||||
|
||||
func NewEcho() *echo.Echo {
|
||||
return echo.New()
|
||||
}
|
||||
|
||||
func StartServer(lc fx.Lifecycle, cfg *config.Config, e *echo.Echo, logger *zap.Logger) {
|
||||
addr := fmt.Sprintf(":%d", cfg.Server.Port)
|
||||
|
||||
if cfg.Server.ReadTimeout != "" {
|
||||
if d, err := time.ParseDuration(cfg.Server.ReadTimeout); err == nil {
|
||||
e.Server.ReadTimeout = d
|
||||
}
|
||||
}
|
||||
if cfg.Server.WriteTimeout != "" {
|
||||
if d, err := time.ParseDuration(cfg.Server.WriteTimeout); err == nil {
|
||||
e.Server.WriteTimeout = d
|
||||
}
|
||||
}
|
||||
|
||||
lc.Append(fx.Hook{
|
||||
OnStart: func(ctx context.Context) error {
|
||||
logger.Info("http server starting", zap.String("addr", addr))
|
||||
go func() {
|
||||
if err := e.Start(addr); err != nil && err != http.ErrServerClosed {
|
||||
logger.Error("http server stopped", zap.Error(err))
|
||||
}
|
||||
}()
|
||||
return nil
|
||||
},
|
||||
OnStop: func(ctx context.Context) error {
|
||||
logger.Info("http server stopping")
|
||||
return e.Shutdown(ctx)
|
||||
},
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user