Skip to content

Commit 5c2916f

Browse files
authored
Add an API middleware to check for updates on API calls (#1067)
Signed-off-by: lujunsan <[email protected]>
1 parent 78654da commit 5c2916f

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

pkg/api/server.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ import (
3232
"github.com/stacklok/toolhive/pkg/container"
3333
"github.com/stacklok/toolhive/pkg/logger"
3434
"github.com/stacklok/toolhive/pkg/registry"
35+
"github.com/stacklok/toolhive/pkg/updates"
3536
"github.com/stacklok/toolhive/pkg/workloads"
3637
)
3738

@@ -88,6 +89,28 @@ func headersMiddleware(next http.Handler) http.Handler {
8889
})
8990
}
9091

92+
// updateCheckMiddleware triggers update checks for API usage
93+
func updateCheckMiddleware() func(next http.Handler) http.Handler {
94+
return func(next http.Handler) http.Handler {
95+
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
96+
go func() {
97+
versionClient := updates.NewVersionClient()
98+
updateChecker, err := updates.NewUpdateChecker(versionClient)
99+
if err != nil {
100+
logger.Warnf("unable to create update client for API: %s", err)
101+
return
102+
}
103+
104+
err = updateChecker.CheckLatestVersion()
105+
if err != nil {
106+
logger.Warnf("could not check for updates for API: %s", err)
107+
}
108+
}()
109+
next.ServeHTTP(w, r)
110+
})
111+
}
112+
}
113+
91114
// Serve starts the server on the given address and serves the API.
92115
// It is assumed that the caller sets up appropriate signal handling.
93116
// If isUnixSocket is true, address is treated as a UNIX socket path.
@@ -108,6 +131,9 @@ func Serve(
108131
headersMiddleware,
109132
)
110133

134+
// Add update check middleware
135+
r.Use(updateCheckMiddleware())
136+
111137
// Add authentication middleware
112138
authMiddleware, err := auth.GetAuthenticationMiddleware(ctx, oidcConfig, false)
113139
if err != nil {

0 commit comments

Comments
 (0)