debian-forge-composer/internal/common/operation_id.go
Lukas Zapletal 5ce8f65a58 cloudapi: propagate operation/external id
Signed-off-by: Lukas Zapletal <lzap+git@redhat.com>
2024-06-25 13:58:53 +02:00

33 lines
745 B
Go

package common
import (
"context"
"github.com/labstack/echo/v4"
"github.com/segmentio/ksuid"
)
type ctxKey string
const OperationIDKey string = "operationID"
const operationIDKeyCtx ctxKey = ctxKey(OperationIDKey)
// Adds a time-sortable globally unique identifier to an echo.Context if not already set
func OperationIDMiddleware(next echo.HandlerFunc) echo.HandlerFunc {
return func(c echo.Context) error {
if c.Get(OperationIDKey) == nil {
oid := GenerateOperationID()
c.Set(OperationIDKey, oid)
ctx := c.Request().Context()
ctx = context.WithValue(ctx, operationIDKeyCtx, oid)
c.SetRequest(c.Request().WithContext(ctx))
}
return next(c)
}
}
func GenerateOperationID() string {
return ksuid.New().String()
}