first commit
This commit is contained in:
44
cmd/app/main.go
Normal file
44
cmd/app/main.go
Normal file
@@ -0,0 +1,44 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/felipecoppola/go-boilerplate/internal/config"
|
||||
"github.com/felipecoppola/go-boilerplate/internal/infrastructure/os"
|
||||
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
// Globals that are set at build time.
|
||||
var (
|
||||
appName = ""
|
||||
appVersion = ""
|
||||
)
|
||||
|
||||
func main() {
|
||||
defer func() { _ = zap.L().Sync() }()
|
||||
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
defer cancel()
|
||||
|
||||
logger := zap.L().With(
|
||||
zap.String("app_name", appName),
|
||||
zap.String("app_version", appVersion),
|
||||
)
|
||||
|
||||
cfg, err := config.NewConfig()
|
||||
if err != nil {
|
||||
logger.Error("error creating config: %w", zap.Error(err))
|
||||
return
|
||||
}
|
||||
|
||||
logger.Info("starting app")
|
||||
os.SignalListener(logger, cancel)
|
||||
|
||||
if err = run(ctx, cfg); err != nil {
|
||||
logger.Error("command failed: %w", zap.Error(err))
|
||||
return
|
||||
}
|
||||
|
||||
logger.Info("command completed")
|
||||
}
|
||||
29
cmd/app/service.go
Normal file
29
cmd/app/service.go
Normal file
@@ -0,0 +1,29 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/kjannette/go-http-server/cmd/app/handler/httpjson"
|
||||
"github.com/kjannnette/go-http-server/internal/config"
|
||||
"github.com/kjannette/go-http-server/pkg/logger"
|
||||
|
||||
"go.uber.org/zap"
|
||||
"golang.org/x/sync/errgroup"
|
||||
)
|
||||
|
||||
func run(ctx context.Context, cfg *config.Config) error {
|
||||
lg := zap.L().With(zap.String("app_name", appName), zap.String("app_version", appVersion))
|
||||
|
||||
eg, ctx := errgroup.WithContext(ctx)
|
||||
app, h := httpjson.New(lg)
|
||||
|
||||
eg.Go(func(l logger.Logger) func() error {
|
||||
return func() error {
|
||||
l.Info("starting server")
|
||||
defer l.Info("terminated server")
|
||||
return h.RunServer(ctx, cfg, app)
|
||||
}
|
||||
}(lg.Named("run_server")))
|
||||
|
||||
return eg.Wait()
|
||||
}
|
||||
56
cmd/handler/httpjson/handler.go
Normal file
56
cmd/handler/httpjson/handler.go
Normal 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
|
||||
}
|
||||
15
cmd/handler/httpjson/health.go
Normal file
15
cmd/handler/httpjson/health.go
Normal 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")
|
||||
}
|
||||
17
cmd/main.go
Normal file
17
cmd/main.go
Normal file
@@ -0,0 +1,17 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"github.com/kjannette/go-server/internal/config"
|
||||
"github.com/kjannette/go-server/internal/infrastructure/os"
|
||||
"go.uber.org/zap"
|
||||
)
|
||||
|
||||
var (
|
||||
appName = ""
|
||||
appVarsion = ""
|
||||
)
|
||||
|
||||
func main() {
|
||||
defer func() {_ = zap.L().Sync() }()
|
||||
}
|
||||
0
cmd/service.go
Normal file
0
cmd/service.go
Normal file
Reference in New Issue
Block a user