Skip to content

Commit 0cb726b

Browse files
author
HeerakKashyap
committed
refactor: improve OnStart hook context, move logic to method, and test DI access
1 parent 8202ed1 commit 0cb726b

File tree

3 files changed

+27
-27
lines changed

3 files changed

+27
-27
lines changed

examples/http-server/main.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,11 @@ func main() {
2828
a.GET("/trace", TraceHandler)
2929
a.GET("/mysql", MysqlHandler)
3030

31-
// Register an OnStart Hook
3231
a.OnStart(func(ctx *gofr.Context) error {
33-
// Try accessing a DI-managed service, e.g., DB or HTTPService
32+
3433
fmt.Println("OnStart hook executed!")
35-
fmt.Printf("DB: %#v\n", ctx.Container.SQL)
34+
fmt.Printf("SQL: %#v\n", ctx.Container.SQL)
35+
fmt.Printf("Redis: %#v\n", ctx.Container.Redis)
3636
return nil
3737
})
3838

pkg/gofr/gofr.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,26 @@ type App struct {
4949
onStartHooks []func(ctx *Context) error
5050
}
5151

52+
func (a *App) newContextForHooks(ctx context.Context) *Context {
53+
logger := logging.NewContextLogger(ctx, a.container.Logger)
54+
return &Context{
55+
Context: ctx,
56+
Container: a.container,
57+
Request: noopRequest{},
58+
ContextLogger: *logger,
59+
}
60+
}
61+
62+
func (a *App) runOnStartHooks(ctx context.Context) {
63+
gofrCtx := a.newContextForHooks(ctx)
64+
for _, hook := range a.onStartHooks {
65+
if err := hook(gofrCtx); err != nil {
66+
a.Logger().Errorf("OnStart hook failed: %v", err)
67+
os.Exit(1)
68+
}
69+
}
70+
}
71+
5272
// Shutdown stops the service(s) and close the application.
5373
// It shuts down the HTTP, gRPC, Metrics servers and closes the container's active connections to datasources.
5474
func (a *App) Shutdown(ctx context.Context) error {

pkg/gofr/run.go

Lines changed: 4 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,6 @@ import (
88
"sync"
99
"syscall"
1010

11-
"go.opentelemetry.io/otel"
12-
13-
"gofr.dev/pkg/gofr/logging"
1411
)
1512

1613
// Run starts the application. If it is an HTTP server, it will start the server.
@@ -19,30 +16,13 @@ func (a *App) Run() {
1916
a.cmd.Run(a.container)
2017
}
2118

22-
// Create a context that is canceled on receiving termination signals
19+
2320
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGINT, syscall.SIGTERM)
2421
defer stop()
2522

26-
// --- Add this block for OnStart hooks ---
27-
onStartCtx, span := otel.GetTracerProvider().Tracer("gofr-onstart").Start(context.Background(), "OnStart")
28-
defer span.End()
29-
30-
logger := logging.NewContextLogger(onStartCtx, a.container.Logger)
31-
32-
gofrCtx := &Context{
33-
Context: onStartCtx,
34-
Container: a.container,
35-
Request: noopRequest{}, // Make sure noopRequest{} is available
36-
ContextLogger: *logger,
37-
}
38-
39-
for _, hook := range a.onStartHooks {
40-
if err := hook(gofrCtx); err != nil {
41-
a.Logger().Errorf("OnStart hook failed: %v", err)
42-
os.Exit(1)
43-
}
44-
}
45-
// --- End OnStart block ---
23+
onStartCtx := context.WithoutCancel(ctx)
24+
a.runOnStartHooks(onStartCtx)
25+
4626

4727
timeout, err := getShutdownTimeoutFromConfig(a.Config)
4828
if err != nil {

0 commit comments

Comments
 (0)