keel/examples/graceful/main.go
Kevin Franklin Kim 07f0c394d5
feat: add GoRoutine service
moves all services into the service package
2023-09-08 12:17:23 +02:00

61 lines
1.0 KiB
Go

package main
import (
"context"
"net/http"
"sync"
"time"
"github.com/foomo/keel/service"
"go.uber.org/zap"
"github.com/foomo/keel"
"github.com/foomo/keel/log"
)
func main() {
svr := keel.NewServer(
keel.WithHTTPZapService(true),
keel.WithHTTPViperService(true),
keel.WithHTTPPrometheusService(true),
keel.WithHTTPHealthzService(true),
)
l := svr.Logger()
go waitGroup(svr.CancelContext(), l.With(log.FServiceName("waitGroup")))
// create demo service
svs := http.NewServeMux()
svs.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
_, _ = w.Write([]byte("OK"))
})
svr.AddService(
service.NewHTTP(l, "demo", "localhost:8080", svs),
)
svr.Run()
}
func waitGroup(ctx context.Context, l *zap.Logger) {
var wg sync.WaitGroup
wg.Add(1)
go func() {
defer wg.Done()
for {
select {
case <-ctx.Done():
l.Info("Break the loop")
return
case <-time.After(3 * time.Second):
l.Info("Hello in a loop")
}
}
}()
wg.Wait()
}