Skip to content

Commit d68809c

Browse files
aryanmehrotraHeerakKashyapHeerakKashyapcoolwednesdayccoVeille
authored
Release/v1.43.0 (#2137)
* Fix/pubsub migration get last version (#2119) * add chaining of next migrator in getLastVersion method of pubsub.go * fix linter * feat: add OnStart hook for synchronous startup jobs (#2079) * feat: improve OnStart hook with DI context, update docs, and ensure code quality * refactor: improve OnStart hook context, move logic to method, and test DI access * feat(onstart): implement all reviewer feedback - Move OnStart logic to a dedicated, testable method. - Fix all linter issues (deep-exit and cyclomatic complexity). - Add unit tests for success and error cases. - Add comprehensive documentation with a realistic example. - Update the example app to demonstrate a real-world use case. * fix: improve startup error handling for OnStart hooks (graceful shutdown on context cancel) * fix: address linter errors in OnStart tests - Fix dynamic error definition - Fix unused parameters with underscore - Use t.Context() instead of context.Background() - Use assert.NoError instead of assert.Nil * fix: remove os.Exit call to resolve deep-exit linter error * fix: resolve remaining linter errors - Fix cuddled return statement in newContextForHooks - Fix cuddled assignment in OnStart test - Remove os.Exit call to resolve deep-exit error * refactor: remove newContextForHooks and reuse existing newContext function * fix: resolve linting errors and refactor Run() method for better maintainability - Fix err113: Replace dynamic error creation with static error in TestApp_OnStart - Fix gocyclo: Refactor Run() method by extracting helper methods to reduce cyclomatic complexity - Fix ws1: Correct whitespace issues in gofr_test.go and run.go - Add helper methods: handleStartupHooks, startShutdownHandler, startTelemetryIfEnabled, startAllServers - Improve code organization and readability while maintaining functionality * fix: resolve remaining linting issues in TestApp_OnStart - Fix err113: Move error variable to package level to avoid dynamic error creation - Fix error-naming: Rename hookFailedErr to errHookFailed following Go naming conventions - Fix whitespace: Remove unnecessary blank line before app assignment - All linting issues in modified files are now resolved * fix: resolve godot and wsl linter errors in run.go and gofr_test.go * Update go.work.sum * fix: restore comments as requested by reviewer * Update run.go * Update run.go * fix: address reviewer comments - add godoc comment to OnStart method and move startup-hooks.md to proper directory structure * fix: restore metrics server comments and ensure OnStart hook runs before route registration * fix: implement proper context usage in runOnStartHooks and fix example API usage * Update go.work.sum * fix: correct spelling of 'canceled' in comment * minor documentation fix * revert go.sum changes * Update pkg/gofr/gofr_test.go Co-authored-by: ccoVeille <[email protected]> * revert go.work.sum changes --------- Co-authored-by: HeerakKashyap <[email protected]> Co-authored-by: Umang Mundhra <[email protected]> Co-authored-by: coolwednesday <[email protected]> Co-authored-by: Divya Darshana <[email protected]> Co-authored-by: ccoVeille <[email protected]> * build(deps): bump github.com/prometheus/client_golang Bumps [github.com/prometheus/client_golang](https://github.com/prometheus/client_golang) from 1.22.0 to 1.23.0. - [Release notes](https://github.com/prometheus/client_golang/releases) - [Changelog](https://github.com/prometheus/client_golang/blob/main/CHANGELOG.md) - [Commits](prometheus/client_golang@v1.22.0...v1.23.0) --- updated-dependencies: - dependency-name: github.com/prometheus/client_golang dependency-version: 1.23.0 dependency-type: direct:production update-type: version-update:semver-minor ... Signed-off-by: dependabot[bot] <[email protected]> * build(deps): bump go.opentelemetry.io/otel/exporters/prometheus Bumps [go.opentelemetry.io/otel/exporters/prometheus](https://github.com/open-telemetry/opentelemetry-go) from 0.59.0 to 0.59.1. - [Release notes](https://github.com/open-telemetry/opentelemetry-go/releases) - [Changelog](https://github.com/open-telemetry/opentelemetry-go/blob/main/CHANGELOG.md) - [Commits](open-telemetry/opentelemetry-go@exporters/prometheus/v0.59.0...exporters/prometheus/v0.59.1) --- updated-dependencies: - dependency-name: go.opentelemetry.io/otel/exporters/prometheus dependency-version: 0.59.1 dependency-type: direct:production update-type: version-update:semver-patch ... Signed-off-by: dependabot[bot] <[email protected]> * build(deps): bump gofr.dev in /examples/using-add-filestore Bumps [gofr.dev](https://github.com/gofr-dev/gofr) from 1.42.4 to 1.42.5. - [Release notes](https://github.com/gofr-dev/gofr/releases) - [Commits](v1.42.4...v1.42.5) --- updated-dependencies: - dependency-name: gofr.dev dependency-version: 1.42.5 dependency-type: direct:production update-type: version-update:semver-patch ... Signed-off-by: dependabot[bot] <[email protected]> * build(deps): bump github.com/ClickHouse/clickhouse-go/v2 Bumps [github.com/ClickHouse/clickhouse-go/v2](https://github.com/ClickHouse/clickhouse-go) from 2.39.0 to 2.40.1. - [Release notes](https://github.com/ClickHouse/clickhouse-go/releases) - [Changelog](https://github.com/ClickHouse/clickhouse-go/blob/main/CHANGELOG.md) - [Commits](ClickHouse/clickhouse-go@v2.39.0...v2.40.1) --- updated-dependencies: - dependency-name: github.com/ClickHouse/clickhouse-go/v2 dependency-version: 2.40.1 dependency-type: direct:production update-type: version-update:semver-minor ... Signed-off-by: dependabot[bot] <[email protected]> * build(deps): bump crate-ci/typos in the actions group Bumps the actions group with 1 update: [crate-ci/typos](https://github.com/crate-ci/typos). Updates `crate-ci/typos` from 1.34.0 to 1.35.1 - [Release notes](https://github.com/crate-ci/typos/releases) - [Changelog](https://github.com/crate-ci/typos/blob/master/CHANGELOG.md) - [Commits](crate-ci/typos@v1.34.0...v1.35.1) --- updated-dependencies: - dependency-name: crate-ci/typos dependency-version: 1.35.1 dependency-type: direct:production update-type: version-update:semver-minor dependency-group: actions ... Signed-off-by: dependabot[bot] <[email protected]> * build(deps): bump github.com/prometheus/client_golang Bumps [github.com/prometheus/client_golang](https://github.com/prometheus/client_golang) from 1.22.0 to 1.23.0. - [Release notes](https://github.com/prometheus/client_golang/releases) - [Changelog](https://github.com/prometheus/client_golang/blob/main/CHANGELOG.md) - [Commits](prometheus/client_golang@v1.22.0...v1.23.0) --- updated-dependencies: - dependency-name: github.com/prometheus/client_golang dependency-version: 1.23.0 dependency-type: direct:production update-type: version-update:semver-minor ... Signed-off-by: dependabot[bot] <[email protected]> * build(deps): bump google.golang.org/api from 0.243.0 to 0.244.0 Bumps [google.golang.org/api](https://github.com/googleapis/google-api-go-client) from 0.243.0 to 0.244.0. - [Release notes](https://github.com/googleapis/google-api-go-client/releases) - [Changelog](https://github.com/googleapis/google-api-go-client/blob/main/CHANGES.md) - [Commits](googleapis/google-api-go-client@v0.243.0...v0.244.0) --- updated-dependencies: - dependency-name: google.golang.org/api dependency-version: 0.244.0 dependency-type: direct:production update-type: version-update:semver-minor ... Signed-off-by: dependabot[bot] <[email protected]> * build(deps): bump modernc.org/sqlite from 1.38.1 to 1.38.2 Bumps [modernc.org/sqlite](https://gitlab.com/cznic/sqlite) from 1.38.1 to 1.38.2. - [Commits](https://gitlab.com/cznic/sqlite/compare/v1.38.1...v1.38.2) --- updated-dependencies: - dependency-name: modernc.org/sqlite dependency-version: 1.38.2 dependency-type: direct:production update-type: version-update:semver-patch ... Signed-off-by: dependabot[bot] <[email protected]> * build(deps): bump github.com/golang-jwt/jwt/v5 from 5.2.3 to 5.3.0 Bumps [github.com/golang-jwt/jwt/v5](https://github.com/golang-jwt/jwt) from 5.2.3 to 5.3.0. - [Release notes](https://github.com/golang-jwt/jwt/releases) - [Changelog](https://github.com/golang-jwt/jwt/blob/main/VERSION_HISTORY.md) - [Commits](golang-jwt/jwt@v5.2.3...v5.3.0) --- updated-dependencies: - dependency-name: github.com/golang-jwt/jwt/v5 dependency-version: 5.3.0 dependency-type: direct:production update-type: version-update:semver-minor ... Signed-off-by: dependabot[bot] <[email protected]> * Merge pull request #2135 from gofr-dev/fix-docs * update release version to v1.43.0 --------- Signed-off-by: dependabot[bot] <[email protected]> Co-authored-by: HootingConjuror <[email protected]> Co-authored-by: HeerakKashyap <[email protected]> Co-authored-by: coolwednesday <[email protected]> Co-authored-by: Divya Darshana <[email protected]> Co-authored-by: ccoVeille <[email protected]> Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2 parents fa6c7c1 + ffba79f commit d68809c

File tree

18 files changed

+378
-134
lines changed

18 files changed

+378
-134
lines changed

.github/workflows/typos.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,4 @@ jobs:
1111
- name: Checkout Code
1212
uses: actions/checkout@v4
1313
- name: typos-action
14-
uses: crate-ci/typos@v1.34.0
14+
uses: crate-ci/typos@v1.35.1
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
# Startup Hooks
2+
3+
GoFr provides a way to run synchronous jobs when your application starts, before any servers begin handling requests. This is useful for tasks like seeding a database, warming up a cache, or performing other critical setup procedures.
4+
5+
## OnStart
6+
7+
You can register a startup hook using the `a.OnStart()` method on your `app` instance.
8+
9+
## Usage
10+
11+
The method accepts a function with the signature:
12+
13+
The method accepts a function with the signature `func(ctx *gofr.Context) error`.
14+
15+
- The `*gofr.Context` passed to the hook is fully initialized and provides access to all dependency-injection-managed services (e.g., `ctx.Container.SQL`, `ctx.Container.Redis`).
16+
- If any `OnStart` hook returns an error, the application will log the error and refuse to start.
17+
18+
19+
### Example: Warming up a Cache
20+
21+
Here is an example of using `OnStart` to set an initial value in a Redis cache when the application starts.
22+
23+
```go
24+
package main
25+
26+
import (
27+
"gofr.dev/pkg/gofr"
28+
)
29+
30+
func main() {
31+
a := gofr.New()
32+
33+
// Register an OnStart hook to warm up a cache.
34+
a.OnStart(func(ctx *gofr.Context) error {
35+
ctx.Logger.Info("Warming up the cache...")
36+
37+
// In a real app, this might come from a database or another service.
38+
cacheKey := "initial-data"
39+
cacheValue := "This is some data cached at startup."
40+
41+
err := ctx.Redis.Set(ctx, cacheKey, cacheValue, 0).Err()
42+
if err != nil {
43+
ctx.Logger.Errorf("Failed to warm up cache: %v", err)
44+
return err // Return the error to halt startup if caching fails.
45+
}
46+
47+
ctx.Logger.Info("Cache warmed up successfully!")
48+
49+
return nil
50+
})
51+
52+
// ... register your routes
53+
54+
a.Run()
55+
}
56+
```
57+
58+
This ensures that critical startup tasks are completed successfully before the application begins accepting traffic.
59+

docs/navigation.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,11 @@ export const navigation = [
122122
href: '/docs/advanced-guide/swagger-documentation',
123123
desc: "Learn how to automatically render SwaggerUI documentation for your GoFr APIs, improving discoverability and usability."
124124
},
125+
{
126+
title: 'Adding Synchronous Startup Hooks',
127+
href: '/docs/advanced-guide/startup-hooks',
128+
desc: "Learn how to seed a database, warm up a cache, or perform other critical setup procedures, synchronously before starting your application."
129+
},
125130
{
126131
title: 'Error Handling',
127132
href: '/docs/advanced-guide/gofr-errors',

examples/http-server-using-redis/main.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,27 @@ func main() {
1717
app.POST("/redis", RedisSetHandler)
1818
app.GET("/redis-pipeline", RedisPipelineHandler)
1919

20+
// Register an OnStart hook to warm up a cache.
21+
// This runs before route registration as intended.
22+
app.OnStart(func(ctx *gofr.Context) error {
23+
ctx.Logger.Info("Warming up the cache...")
24+
25+
// Example: Fetch some data and store it in Redis.
26+
// In a real app, this might come from a database or another service.
27+
cacheKey := "initial-data"
28+
cacheValue := "This is some data cached at startup."
29+
30+
err := ctx.Redis.Set(ctx, cacheKey, cacheValue, 0).Err()
31+
if err != nil {
32+
ctx.Logger.Errorf("Failed to warm up cache: %v", err)
33+
return err // Return the error to halt startup if caching fails.
34+
}
35+
36+
ctx.Logger.Info("Cache warmed up successfully!")
37+
38+
return nil
39+
})
40+
2041
// Run the application
2142
app.Run()
2243
}

examples/using-add-filestore/go.mod

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,13 @@ toolchain go1.24.1
77
require (
88
github.com/stretchr/testify v1.10.0
99
go.uber.org/mock v0.5.2
10-
gofr.dev v1.42.4
10+
gofr.dev v1.42.5
1111
gofr.dev/pkg/gofr/datasource/file/ftp v0.2.1
1212
)
1313

1414
require (
1515
cloud.google.com/go v0.120.0 // indirect
16-
cloud.google.com/go/auth v0.16.2 // indirect
16+
cloud.google.com/go/auth v0.16.3 // indirect
1717
cloud.google.com/go/auth/oauth2adapt v0.2.8 // indirect
1818
cloud.google.com/go/compute/metadata v0.7.0 // indirect
1919
cloud.google.com/go/iam v1.5.2 // indirect
@@ -34,11 +34,11 @@ require (
3434
github.com/go-logr/stdr v1.2.2 // indirect
3535
github.com/go-sql-driver/mysql v1.9.3 // indirect
3636
github.com/gogo/protobuf v1.3.2 // indirect
37-
github.com/golang-jwt/jwt/v5 v5.2.2 // indirect
37+
github.com/golang-jwt/jwt/v5 v5.2.3 // indirect
3838
github.com/google/s2a-go v0.1.9 // indirect
3939
github.com/google/uuid v1.6.0 // indirect
4040
github.com/googleapis/enterprise-certificate-proxy v0.3.6 // indirect
41-
github.com/googleapis/gax-go/v2 v2.14.2 // indirect
41+
github.com/googleapis/gax-go/v2 v2.15.0 // indirect
4242
github.com/gorilla/mux v1.8.1 // indirect
4343
github.com/gorilla/websocket v1.5.3 // indirect
4444
github.com/grpc-ecosystem/go-grpc-middleware v1.4.0 // indirect
@@ -83,24 +83,24 @@ require (
8383
go.opentelemetry.io/otel/sdk/metric v1.37.0 // indirect
8484
go.opentelemetry.io/otel/trace v1.37.0 // indirect
8585
go.opentelemetry.io/proto/otlp v1.7.0 // indirect
86-
golang.org/x/crypto v0.39.0 // indirect
87-
golang.org/x/exp v0.0.0-20250408133849-7e4ce0ab07d0 // indirect
88-
golang.org/x/net v0.41.0 // indirect
86+
golang.org/x/crypto v0.40.0 // indirect
87+
golang.org/x/exp v0.0.0-20250620022241-b7579e27df2b // indirect
88+
golang.org/x/net v0.42.0 // indirect
8989
golang.org/x/oauth2 v0.30.0 // indirect
9090
golang.org/x/sync v0.16.0 // indirect
9191
golang.org/x/sys v0.34.0 // indirect
9292
golang.org/x/term v0.33.0 // indirect
9393
golang.org/x/text v0.27.0 // indirect
9494
golang.org/x/time v0.12.0 // indirect
95-
google.golang.org/api v0.241.0 // indirect
96-
google.golang.org/genproto v0.0.0-20250505200425-f936aa4a68b2 // indirect
95+
google.golang.org/api v0.243.0 // indirect
96+
google.golang.org/genproto v0.0.0-20250603155806-513f23925822 // indirect
9797
google.golang.org/genproto/googleapis/api v0.0.0-20250603155806-513f23925822 // indirect
98-
google.golang.org/genproto/googleapis/rpc v0.0.0-20250603155806-513f23925822 // indirect
99-
google.golang.org/grpc v1.73.0 // indirect
98+
google.golang.org/genproto/googleapis/rpc v0.0.0-20250715232539-7130f93afb79 // indirect
99+
google.golang.org/grpc v1.74.2 // indirect
100100
google.golang.org/protobuf v1.36.6 // indirect
101101
gopkg.in/yaml.v3 v3.0.1 // indirect
102-
modernc.org/libc v1.65.10 // indirect
102+
modernc.org/libc v1.66.3 // indirect
103103
modernc.org/mathutil v1.7.1 // indirect
104104
modernc.org/memory v1.11.0 // indirect
105-
modernc.org/sqlite v1.38.0 // indirect
105+
modernc.org/sqlite v1.38.1 // indirect
106106
)

0 commit comments

Comments
 (0)