Skip to content

Commit 68453d9

Browse files
committed
Updated contexts
1 parent cad63d4 commit 68453d9

File tree

3 files changed

+15
-16
lines changed

3 files changed

+15
-16
lines changed

pkg/httprouter/httprouter.go

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -63,18 +63,20 @@ func (r *router) Run(ctx context.Context) error {
6363
// HTTP ROUTER
6464

6565
// Register a function to handle a URL path
66-
func (r *router) HandleFunc(ctx context.Context, prefix string, fn http.HandlerFunc) {
66+
func (r *router) HandleFunc(parent context.Context, prefix string, fn http.HandlerFunc) {
6767
// Wrap the function with middleware
6868
for _, middleware := range r.middleware {
6969
fn = middleware.HandleFunc(fn)
7070
}
7171

7272
// Apply middleware, set context
73-
ref.Log(ctx).Debug(ctx, "Register route: ", types.JoinPath(r.prefix, prefix))
73+
ref.Log(parent).Debug(parent, "Register route: ", types.JoinPath(r.prefix, prefix))
7474
r.ServeMux.HandleFunc(types.JoinPath(r.prefix, prefix), func(w http.ResponseWriter, r *http.Request) {
75-
r = r.WithContext(ref.WithLog(r.Context(), ref.Log(ctx)))
76-
// TODO: Add Log into the r context, but don't replace the original
77-
fn(w, r)
75+
// Add provider to context
76+
ctx := ref.WithProvider(r.Context(), ref.Provider(parent))
77+
78+
// Serve the request
79+
fn(w, r.WithContext(ctx))
7880
})
7981
}
8082

@@ -84,7 +86,7 @@ func (r *router) Origin() string {
8486
}
8587

8688
// Register serving of static files from a filesystem
87-
func (r *router) HandleFS(ctx context.Context, prefix string, fs fs.FS) {
89+
func (r *router) HandleFS(parent context.Context, prefix string, fs fs.FS) {
8890
// Create the file server
8991
fn := http.StripPrefix(types.JoinPath(r.prefix, prefix), http.FileServer(http.FS(fs))).ServeHTTP
9092

@@ -94,12 +96,12 @@ func (r *router) HandleFS(ctx context.Context, prefix string, fs fs.FS) {
9496
}
9597

9698
// Apply middleware
97-
ref.Log(ctx).Debug(ctx, "Register route: ", types.JoinPath(r.prefix, prefix))
99+
ref.Log(parent).Debug(parent, "Register static: ", types.JoinPath(r.prefix, prefix))
98100
r.ServeMux.HandleFunc(types.JoinPath(r.prefix, prefix), func(w http.ResponseWriter, req *http.Request) {
99101
// Set CORS headers
100102
httpresponse.Cors(w, req, r.origin, http.MethodGet)
101103

102104
// Call the file server
103-
fn(w, req.WithContext(ref.WithLog(ctx, ref.Log(ctx))))
105+
fn(w, req.WithContext(ref.WithProvider(parent, ref.Provider(parent))))
104106
})
105107
}

pkg/pgqueue/config/task.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ func (task *task) Run(parent context.Context) error {
6161

6262
// Create a cancelable context
6363
ctx, cancel := context.WithCancel(context.Background())
64-
ctx = ref.WithPath(ref.WithLog(ctx, ref.Log(parent)), ref.Path(parent)...)
64+
ctx = ref.WithPath(ref.WithProvider(ctx, ref.Provider(parent)), ref.Path(parent)...)
6565

6666
// Ticker loop
6767
tickerch := make(chan *schema.Ticker)
@@ -260,7 +260,8 @@ func (t *task) tryTask(ctx context.Context, taskpool *pgqueue.TaskPool, task *sc
260260
taskpool.RunTask(ctx, task, t.getTaskCallback(task), func(err error) {
261261
var status string
262262
delta := time.Since(now).Truncate(time.Millisecond)
263-
if _, err_ := t.manager.ReleaseTask(context.TODO(), task.Id, err == nil, err, &status); err_ != nil {
263+
child := ref.WithPath(ref.WithProvider(context.TODO(), ref.Provider(ctx)), ref.Path(ctx)...)
264+
if _, err_ := t.manager.ReleaseTask(child, task.Id, err == nil, err, &status); err_ != nil {
264265
err = errors.Join(err, err_)
265266
}
266267
switch {

pkg/ref/context.go

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,10 @@ func Label(ctx context.Context) string {
5454
}
5555

5656
func Log(ctx context.Context) server.Logger {
57-
if value := ctx.Value(ctxLogger); value == nil {
57+
if provider := ctx.Value(ctxProvider); provider == nil {
5858
return nil
5959
} else {
60-
return value.(server.Logger)
60+
return provider.(server.Logger)
6161
}
6262
}
6363

@@ -93,10 +93,6 @@ func Task(ctx context.Context) *pgqueue.Task {
9393
}
9494
}
9595

96-
func WithLog(ctx context.Context, logger server.Logger) context.Context {
97-
return context.WithValue(ctx, ctxLogger, logger)
98-
}
99-
10096
func WithAuth(ctx context.Context, auth server.Auth) context.Context {
10197
return context.WithValue(ctx, ctxAuth, auth)
10298
}

0 commit comments

Comments
 (0)