| package middleware
|
|
|
| import (
|
| "github.com/gin-gonic/gin"
|
|
|
| "github.com/looplj/axonhub/internal/contexts"
|
| "github.com/looplj/axonhub/internal/log"
|
| "github.com/looplj/axonhub/internal/server/biz"
|
| "github.com/looplj/axonhub/internal/tracing"
|
| )
|
|
|
|
|
|
|
| func WithThread(config tracing.Config, threadService *biz.ThreadService) gin.HandlerFunc {
|
|
|
| threadHeader := config.ThreadHeader
|
| if threadHeader == "" {
|
| threadHeader = "AH-Thread-Id"
|
| }
|
|
|
| return func(c *gin.Context) {
|
| threadID := c.GetHeader(threadHeader)
|
| if threadID == "" {
|
| c.Next()
|
| return
|
| }
|
|
|
|
|
| projectID, ok := contexts.GetProjectID(c.Request.Context())
|
| if !ok {
|
| c.Next()
|
| return
|
| }
|
|
|
|
|
| thread, err := threadService.GetOrCreateThread(c.Request.Context(), projectID, threadID)
|
| if err != nil {
|
| log.Warn(c.Request.Context(), "failed to get or create thread", log.String("thread_id", threadID), log.Int("project_id", projectID), log.Cause(err))
|
| c.Next()
|
|
|
| return
|
| }
|
|
|
|
|
| ctx := contexts.WithThread(c.Request.Context(), thread)
|
| c.Request = c.Request.WithContext(ctx)
|
|
|
| c.Next()
|
| }
|
| }
|
|
|