Skip to content
Open
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
32 changes: 29 additions & 3 deletions cmd/camp/main.go
Original file line number Diff line number Diff line change
@@ -1,29 +1,55 @@
package main

import (
"context"
"log"
"net/http"
"os"
"time"

"github.com/codersgyan/camp/internal/contact"
"github.com/codersgyan/camp/internal/database"
graceful "github.com/codersgyan/camp/internal/shutdown"
)

func main() {
db, err := database.Connect("./camp_data/camp.db")
if err != nil {
log.Fatal(err)
}
defer db.Close()

// run the migration
if err := database.RunMigration(db); err != nil {
log.Fatal(err)
}

shutdown := graceful.New(10 * time.Second)

contactRepository := contact.NewRepository(db)
contactHandler := contact.NewHandler(contactRepository)

srv := &http.Server{
Addr: ":8080",
}

http.HandleFunc("POST /api/contacts", contactHandler.Create)

log.Fatal(http.ListenAndServe(":8080", nil))
// Register cleanup
shutdown.Register(srv.Shutdown)
shutdown.Register(db.Close)

// Start server in goroutine
go func() {
log.Println("Server running on :8080")
if err := srv.ListenAndServe(); err != nil && err != http.ErrServerClosed {
log.Fatal(err)
}
}()

// Wait for signal
exitCode, err := shutdown.Wait(context.Background())
if err != nil {
log.Fatal(err)
}
os.Exit(exitCode)

}
2 changes: 2 additions & 0 deletions internal/contact/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/json"
"fmt"
"net/http"
"time"
)

type Handler struct {
Expand All @@ -25,6 +26,7 @@ func (h *Handler) Create(w http.ResponseWriter, r *http.Request) {
return
}

time.Sleep(time.Second * 10)
w.Header().Set("Content-Type", "application/json")

createdId, err := h.repo.CreateContactOrUpsertTags(&contactBody)
Expand Down
63 changes: 63 additions & 0 deletions internal/shutdown/graceful.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package shutdown

import (
"context"
"os"
"os/signal"
"syscall"
"time"
)

type GracefulShutdown struct {
timeout time.Duration
cleanup []func(context.Context) error
}

func New(timeout time.Duration) *GracefulShutdown {
return &GracefulShutdown{
timeout: timeout,
}
}

// Register accepts BOTH:
//
// func() error
// func(context.Context) error
func (gs *GracefulShutdown) Register(fn any) {
switch f := fn.(type) {

case func(context.Context) error:
gs.cleanup = append(gs.cleanup, f)

case func() error:
wrapped := func(ctx context.Context) error {
return f()
}
gs.cleanup = append(gs.cleanup, wrapped)

default:
panic("Register: invalid function type (must be func() error or func(context.Context) error)")
}
}

func (gs *GracefulShutdown) Wait(ctx context.Context) (int, error) {
sigChan := make(chan os.Signal, 1)

signal.Notify(sigChan, syscall.SIGINT, syscall.SIGTERM)

sig := <-sigChan

// Convert to UNIX exit code
exitCode := 128 + int(sig.(syscall.Signal))

// cleanup with timeout
cleanupCtx, cancel := context.WithTimeout(ctx, gs.timeout)
defer cancel()

for _, fn := range gs.cleanup {
if err := fn(cleanupCtx); err != nil {
return exitCode, err
}
}
return exitCode, nil
}