From b78abcfe5214cc517f12abafba03a0989b0b5da6 Mon Sep 17 00:00:00 2001 From: amangupta Date: Wed, 26 Nov 2025 02:48:36 +0530 Subject: [PATCH] added the graceful shutdown and configured the env file --- cmd/camp/main.go | 41 ++++++++++++++++++++++++++++++++++++++++- config/app-config.go | 27 +++++++++++++++++++++++++++ go.mod | 2 ++ go.sum | 2 ++ 4 files changed, 71 insertions(+), 1 deletion(-) create mode 100644 config/app-config.go diff --git a/cmd/camp/main.go b/cmd/camp/main.go index 8c02566..fd46cf8 100644 --- a/cmd/camp/main.go +++ b/cmd/camp/main.go @@ -1,14 +1,26 @@ package main import ( + "encoding/json" "log" "net/http" + "os" + "os/signal" + "syscall" + "github.com/codersgyan/camp/config" "github.com/codersgyan/camp/internal/contact" "github.com/codersgyan/camp/internal/database" ) func main() { + + cfg, err := config.SetupENV() + + if err != nil { + log.Fatal(err) + } + db, err := database.Connect("./camp_data/camp.db") if err != nil { log.Fatal(err) @@ -24,6 +36,33 @@ func main() { contactHandler := contact.NewHandler(contactRepository) http.HandleFunc("POST /api/contacts", contactHandler.Create) + http.HandleFunc("/health", HealthCheckHandler) + + // Graceful shutdown + + go func() { + if err := http.ListenAndServe(cfg.HttpPort, nil); err != nil { + log.Fatalf("server error: %v", err) + } + log.Printf("server running on this port: %v", cfg.HttpPort) + }() + + // Wait for signal (Ctrl+C/kill) + quit := make(chan os.Signal, 1) + signal.Notify(quit, os.Interrupt, syscall.SIGTERM) + <-quit + + log.Println("shutting down gracefully...") + +} + +func HealthCheckHandler(w http.ResponseWriter, r *http.Request) { + resp := map[string]string{ + "status": "ok", + } + + w.Header().Set("Content-Type", "application/json") + w.WriteHeader(http.StatusOK) - log.Fatal(http.ListenAndServe(":8080", nil)) + json.NewEncoder(w).Encode(resp) } diff --git a/config/app-config.go b/config/app-config.go new file mode 100644 index 0000000..17b2818 --- /dev/null +++ b/config/app-config.go @@ -0,0 +1,27 @@ +package config + +import ( + "errors" + "os" + + "github.com/joho/godotenv" +) + +type AppConfig struct { + HttpPort string +} + +func SetupENV() (cfg *AppConfig, err error) { + + godotenv.Load() + + httpPort := os.Getenv("HTTP_PORT") + + if len(httpPort) < 1 { + return &AppConfig{}, errors.New("env file is not configured properly") + } + + return &AppConfig{ + HttpPort: httpPort, + }, nil +} diff --git a/go.mod b/go.mod index 0aed561..6415cf3 100644 --- a/go.mod +++ b/go.mod @@ -3,3 +3,5 @@ module github.com/codersgyan/camp go 1.25.2 require github.com/mattn/go-sqlite3 v1.14.32 + +require github.com/joho/godotenv v1.5.1 // indirect diff --git a/go.sum b/go.sum index 66f7516..19413a2 100644 --- a/go.sum +++ b/go.sum @@ -1,2 +1,4 @@ +github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0= +github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4= github.com/mattn/go-sqlite3 v1.14.32 h1:JD12Ag3oLy1zQA+BNn74xRgaBbdhbNIDYvQUEuuErjs= github.com/mattn/go-sqlite3 v1.14.32/go.mod h1:Uh1q+B4BYcTPb+yiD3kU8Ct7aC0hY9fxUwlHK0RXw+Y=