This commit is contained in:
vipg
2026-02-09 16:09:14 +08:00
parent 4541b322b3
commit a8b04bca12
8 changed files with 203 additions and 0 deletions

View File

@@ -0,0 +1,7 @@
module country
go 1.25.7
require common v0.0.0
replace common => ../../common

View File

@@ -0,0 +1,58 @@
package main
import (
"context"
"fmt"
"net/http"
"os"
"os/signal"
"syscall"
"time"
"common/logger"
"common/utils"
)
func main() {
port := utils.GetEnv("PORT", "8081")
srv := &http.Server{
Addr: ":" + port,
Handler: routes(),
}
logger.L().Printf("country service starting on :%s", port)
go func() {
if err := srv.ListenAndServe(); err != nil && err != http.ErrServerClosed {
logger.L().Fatalf("listen: %v", err)
}
}()
quit := make(chan os.Signal, 1)
signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM)
<-quit
logger.L().Printf("country service shutting down...")
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
if err := srv.Shutdown(ctx); err != nil {
logger.L().Printf("server shutdown: %v", err)
}
logger.L().Printf("country service exited")
}
func routes() http.Handler {
mux := http.NewServeMux()
mux.HandleFunc("/healthz", func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
w.Write([]byte("ok"))
})
mux.HandleFunc("/version", func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
w.Write([]byte("country-service v0.1.0"))
})
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "hello from country-service")
})
return mux
}