11package redirects
22
33import (
4+ "context"
45 "encoding/json"
56 "fmt"
67 "io"
@@ -48,15 +49,15 @@ func WithConfig(cfg config.RedirectsStoreConfig) RedirectStoreOption {
4849 }
4950}
5051
51- func queryRenkuApi (host url.URL , endpoint string ) ([]byte , error ) {
52+ func queryRenkuApi (ctx context. Context , host url.URL , endpoint string ) ([]byte , error ) {
5253
5354 rel , err := url .Parse ("/api/data" )
5455 if err != nil {
5556 return nil , fmt .Errorf ("error parsing endpoint: %w" , err )
5657 }
5758 rel = rel .JoinPath (endpoint )
5859 fullUrl := host .ResolveReference (rel ).String ()
59- req , err := http .NewRequest ( "GET" , fullUrl , nil )
60+ req , err := http .NewRequestWithContext ( ctx , "GET" , fullUrl , nil )
6061 if err != nil {
6162 return nil , fmt .Errorf ("error creating request: %w" , err )
6263 }
@@ -78,9 +79,9 @@ func queryRenkuApi(host url.URL, endpoint string) ([]byte, error) {
7879 return body , nil
7980}
8081
81- func retrieveRedirectTargetForSource (host url.URL , source string ) (* PlatformRedirectConfig , error ) {
82+ func retrieveRedirectTargetForSource (ctx context. Context , host url.URL , source string ) (* PlatformRedirectConfig , error ) {
8283 // Query the Renku API to get the redirect for the given source URL
83- body , err := queryRenkuApi (host , fmt .Sprintf ("/platform/redirects/%s" , source ))
84+ body , err := queryRenkuApi (ctx , host , fmt .Sprintf ("/platform/redirects/%s" , source ))
8485 if err != nil {
8586 return nil , fmt .Errorf ("error querying Renku API: %w" , err )
8687 }
@@ -112,7 +113,7 @@ func (rs *RedirectStore) urlToKey(redirectUrl url.URL) (string, error) {
112113 return urlToCheck , nil
113114}
114115
115- func (rs * RedirectStore ) GetRedirectEntry (url url.URL ) (* RedirectStoreRedirectEntry , error ) {
116+ func (rs * RedirectStore ) GetRedirectEntry (ctx context. Context , url url.URL ) (* RedirectStoreRedirectEntry , error ) {
116117 key , err := rs .urlToKey (url )
117118 if err != nil {
118119 return nil , fmt .Errorf ("error converting url to key: %w" , err )
@@ -128,7 +129,7 @@ func (rs *RedirectStore) GetRedirectEntry(url url.URL) (*RedirectStoreRedirectEn
128129 // Re-check after acquiring the lock, since it might have been updated meanwhile
129130 entry , ok = rs .RedirectMap [key ]
130131 if ! ok || entry .UpdatedAt .Add (rs .EntryTtl ).Before (time .Now ()) {
131- updatedEntry , err := retrieveRedirectTargetForSource (* rs .Config .Gitlab .RenkuBaseURL , key )
132+ updatedEntry , err := retrieveRedirectTargetForSource (ctx , * rs .Config .Gitlab .RenkuBaseURL , key )
132133 if err != nil {
133134 return nil , fmt .Errorf ("error retrieving redirect for url %s: %w" , key , err )
134135 }
@@ -153,8 +154,9 @@ func (rs *RedirectStore) Middleware() echo.MiddlewareFunc {
153154 if redirectUrl == nil {
154155 return next (c )
155156 }
157+ ctx := c .Request ().Context ()
156158 // check for redirects for this URL
157- entry , err := rs .GetRedirectEntry (* redirectUrl )
159+ entry , err := rs .GetRedirectEntry (ctx , * redirectUrl )
158160
159161 if err != nil {
160162 slog .Debug (
0 commit comments