Skip to content

Commit 7c50b52

Browse files
committed
use ifnotnil/daemon
1 parent 3d0078a commit 7c50b52

File tree

6 files changed

+17
-277
lines changed

6 files changed

+17
-277
lines changed

cmd/goboilerplate/main.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import (
66
"log/slog"
77
"os"
88

9-
"github.com/moukoublen/goboilerplate/internal"
9+
"github.com/ifnotnil/daemon"
1010
"github.com/moukoublen/goboilerplate/internal/config"
1111
"github.com/moukoublen/goboilerplate/internal/httpx"
1212
"github.com/moukoublen/goboilerplate/internal/logx"
@@ -44,33 +44,33 @@ func main() {
4444

4545
logger = logx.InitSLog(logx.ParseConfig(cnf))
4646

47-
daemon, ctx := internal.NewDaemon(
47+
dmn := daemon.Start(
4848
context.Background(),
49-
logger,
50-
internal.SetShutdownTimeout(cnf.Duration("shutdown_timeout")),
49+
daemon.WithLogger(logger),
50+
daemon.WithShutdownGraceDuration(cnf.Duration("shutdown_timeout")),
5151
)
5252

5353
httpConf := httpx.ParseConfig(cnf)
54-
router := httpx.NewDefaultRouter(ctx, httpConf, logger)
54+
router := httpx.NewDefaultRouter(dmn.CTX(), httpConf, logger)
5555

5656
// init services / application
5757
server := httpx.StartListenAndServe(
5858
fmt.Sprintf("%s:%d", httpConf.IP, httpConf.Port),
5959
router,
6060
httpConf.ReadHeaderTimeout,
61-
daemon.FatalErrorsChannel(),
61+
dmn.FatalErrorsChannel(),
6262
)
63-
logger.InfoContext(ctx, "service started", slog.String("bind", fmt.Sprintf("%s:%d", httpConf.IP, httpConf.Port)))
63+
logger.InfoContext(dmn.CTX(), "service started", slog.String("bind", fmt.Sprintf("%s:%d", httpConf.IP, httpConf.Port)))
6464

6565
// set onShutdown for other components/services.
66-
daemon.OnShutDown(
66+
dmn.OnShutDown(
6767
func(ctx context.Context) {
68-
logger.InfoContext(ctx, "shuting down http server")
68+
logger.InfoContext(ctx, "shuting down http server")
6969
if err := server.Shutdown(ctx); err != nil {
7070
logger.Warn("error during http server shutdown", logx.Error(err))
7171
}
7272
},
7373
)
7474

75-
daemon.Wait()
75+
dmn.Wait()
7676
}

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ go 1.24.2
44

55
require (
66
github.com/go-chi/chi/v5 v5.2.1
7+
github.com/ifnotnil/daemon v0.0.1
78
github.com/ifnotnil/x/http v0.0.2
89
github.com/knadh/koanf/parsers/dotenv v1.1.0
910
github.com/knadh/koanf/parsers/yaml v1.0.0

go.sum

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ github.com/go-chi/chi/v5 v5.2.1 h1:KOIHODQj58PmL80G2Eak4WdvUzjSJSm0vG72crDCqb8=
66
github.com/go-chi/chi/v5 v5.2.1/go.mod h1:L2yAIGWB3H+phAw1NxKwWM+7eUH/lU8pOMm5hHcoops=
77
github.com/go-viper/mapstructure/v2 v2.2.1 h1:ZAaOCxANMuZx5RCeg0mBdEZk7DZasvvZIxtHqx8aGss=
88
github.com/go-viper/mapstructure/v2 v2.2.1/go.mod h1:oJDH3BJKyqBA2TXFhDsKDGDTlndYOZ6rGS0BRZIxGhM=
9+
github.com/ifnotnil/daemon v0.0.1 h1:L3xmpvfhJbS/br1pYsSRnfqY/LkowETJKq1lrSpw0N0=
10+
github.com/ifnotnil/daemon v0.0.1/go.mod h1:1MdZgPWrq+D4jlxqENo54Q7AkwCEIF03mZEYu79dpyY=
911
github.com/ifnotnil/x/http v0.0.2 h1:b4D76UAXWpWgB718Ai4NAkwvxuSnEW5hXRSp1APm1n8=
1012
github.com/ifnotnil/x/http v0.0.2/go.mod h1:ypXtxLtlMet+XudeN1YlIYWAT3vwfIuUfSALBnwvP8s=
1113
github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0=
@@ -40,6 +42,8 @@ github.com/stretchr/objx v0.5.2 h1:xuMeJ0Sdp5ZMRXx/aWO6RZxdr3beISkG5/G/aIRr3pY=
4042
github.com/stretchr/objx v0.5.2/go.mod h1:FRsXN1f5AsAjCGJKqEizvkpNtU+EGNCLh3NxZ/8L+MA=
4143
github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA=
4244
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
45+
go.uber.org/goleak v1.3.0 h1:2K3zAYmnTNqV73imy9J1T3WC+gmCePx2hEGkimedGto=
46+
go.uber.org/goleak v1.3.0/go.mod h1:CoHD4mav9JJNrW/WLlf7HGZPjdw8EucARQHekz1X6bE=
4347
golang.org/x/sys v0.33.0 h1:q3i8TbbEz+JRD9ywIRlyRAQbM0qF7hu24q3teo2hbuw=
4448
golang.org/x/sys v0.33.0/go.mod h1:BJP2sWEmIv4KK5OTEluFJCKSidICx8ciO85XgH3Ak8k=
4549
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=

internal/daemon.go

Lines changed: 0 additions & 201 deletions
This file was deleted.

internal/httpx/mock_http_client_test.go

Lines changed: 2 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/mock_daemon_config_option_test.go

Lines changed: 0 additions & 65 deletions
This file was deleted.

0 commit comments

Comments
 (0)