Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: add graceful Shutdown and healthcheck #21

Merged
merged 1 commit into from
Jan 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ The DIP promotes high-level modules (e.g., use cases) to depend on abstractions
- [x] [Go-Standards](https://github.com/golang-standards/project-layout) Project Layout
- [x] Environment Variable Configuration
- [x] Health-Check and Debug API
- [ ] Graceful Shutdown
- [x] Graceful Shutdown
- Layered architecture
- [x] [SOLID Principle](https://en.wikipedia.org/wiki/SOLID)
- [x] Database Transaction
Expand Down
5 changes: 1 addition & 4 deletions cmd/api/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,5 @@ func main() {
panic(err)
}

app := app.NewApp(context.Background(), cfg)
if err := app.Start(); err != nil {
panic(err)
}
app.NewApp(context.Background(), cfg).Run()
}
24 changes: 22 additions & 2 deletions internal/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ import (
"context"
"fmt"
"os"
"os/signal"
"syscall"
"time"

_ "github.com/go-sql-driver/mysql"

Expand All @@ -13,6 +16,7 @@ import (
"github.com/DoWithLogic/golang-clean-architecture/pkg/otel/zerolog"
"github.com/jmoiron/sqlx"
"github.com/labstack/echo/v4"
"github.com/labstack/gommon/log"
)

type App struct {
Expand All @@ -36,13 +40,29 @@ func NewApp(ctx context.Context, cfg config.Config) *App {
}
}

func (app *App) Start() error {
if err := app.StartService(); err != nil {
func (app *App) Run() error {
if err := app.startService(); err != nil {
app.log.Z().Err(err).Msg("[app]StartService")

return err
}

// Wait for interrupt signal to gracefully shutdown the server with a timeout of 10 seconds.
// Use a buffered channel to avoid missing signals as recommended for signal.Notify
quit := make(chan os.Signal, 1)
signal.Notify(quit, syscall.SIGTERM)
signal.Notify(quit, syscall.SIGINT)

go func() {
<-quit
log.Info("Server is shutting down...")

ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
app.db.Close()
app.echo.Shutdown(ctx)
}()

app.echo.Debug = app.cfg.Server.Debug
app.echo.Use(middleware.AppCors())
app.echo.Use(middleware.CacheWithRevalidation)
Expand Down
8 changes: 7 additions & 1 deletion internal/app/service.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,23 @@
package app

import (
"net/http"

userV1 "github.com/DoWithLogic/golang-clean-architecture/internal/users/delivery/http/v1"
userRepository "github.com/DoWithLogic/golang-clean-architecture/internal/users/repository"
userUseCase "github.com/DoWithLogic/golang-clean-architecture/internal/users/usecase"
"github.com/labstack/echo/v4"
)

func (app *App) StartService() error {
func (app *App) startService() error {
userRepo := userRepository.NewRepository(app.db)
userUC := userUseCase.NewUseCase(userRepo, app.cfg)
userCTRL := userV1.NewHandlers(userUC)

domain := app.echo.Group("/api/v1/users")
domain.GET("/ping", func(c echo.Context) error {
return c.String(http.StatusOK, "Hello Word 👋")
})

userCTRL.UserRoutes(domain, app.cfg)

Expand Down
2 changes: 1 addition & 1 deletion internal/users/delivery/http/v1/route.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
)

func (h *handlers) UserRoutes(domain *echo.Group, cfg config.Config) {
domain.POST("", h.CreateUser)
domain.POST("/", h.CreateUser)
domain.POST("/login", h.Login)
domain.GET("/detail", h.UserDetail, middleware.AuthorizeJWT(cfg))
domain.PATCH("/update", h.UpdateUser, middleware.AuthorizeJWT(cfg))
Expand Down