(cpm CustomProvidersManager, rm routeManager, pm PoliciesManager, a authenticator, prod, private bool, log *zap.Logger, pub publisher, prefix string, ac accessCache, uac userAccessCache, client http.Client, scanner Scanner, cd CustomPolicyDetector, um userManager, removeUserAgent bool)
| 170 | } |
| 171 | |
| 172 | func getMiddleware(cpm CustomProvidersManager, rm routeManager, pm PoliciesManager, a authenticator, prod, private bool, log *zap.Logger, pub publisher, prefix string, ac accessCache, uac userAccessCache, client http.Client, scanner Scanner, cd CustomPolicyDetector, um userManager, removeUserAgent bool) gin.HandlerFunc { |
| 173 | return func(c *gin.Context) { |
| 174 | if c == nil || c.Request == nil { |
| 175 | JSON(c, http.StatusInternalServerError, "[BricksLLM] request is empty") |
| 176 | c.Abort() |
| 177 | return |
| 178 | } |
| 179 | |
| 180 | if c.FullPath() == "/api/health" { |
| 181 | c.Abort() |
| 182 | return |
| 183 | } |
| 184 | |
| 185 | if removeUserAgent { |
| 186 | c.Set("removeUserAgent", removeUserAgent) |
| 187 | } |
| 188 | |
| 189 | blw := &responseWriter{body: bytes.NewBufferString(""), ResponseWriter: c.Writer} |
| 190 | c.Writer = blw |
| 191 | |
| 192 | cid := util.NewUuid() |
| 193 | c.Set(util.STRING_CORRELATION_ID, cid) |
| 194 | logWithCid := log.With(zap.String(util.STRING_CORRELATION_ID, cid)) |
| 195 | util.SetLogToCtx(c, logWithCid) |
| 196 | |
| 197 | start := time.Now() |
| 198 | c.Set("startTime", start) |
| 199 | |
| 200 | enrichedEvent := &event.EventWithRequestAndContent{} |
| 201 | requestBytes := []byte(`{}`) |
| 202 | responseBytes := []byte(`{}`) |
| 203 | userId := "" |
| 204 | |
| 205 | var policyInput any = nil |
| 206 | |
| 207 | customId := c.Request.Header.Get("X-CUSTOM-EVENT-ID") |
| 208 | |
| 209 | metadataBytes := []byte(`{}`) |
| 210 | metadata := c.Request.Header.Get("X-METADATA") |
| 211 | |
| 212 | defer func() { |
| 213 | dur := time.Since(start) |
| 214 | latency := int(dur.Milliseconds()) |
| 215 | |
| 216 | if !prod { |
| 217 | logWithCid.Sugar().Infof("%s | %d | %s | %s | %dms", prefix, c.Writer.Status(), c.Request.Method, c.FullPath(), latency) |
| 218 | } |
| 219 | |
| 220 | keyId := "" |
| 221 | tags := []string{} |
| 222 | |
| 223 | if enrichedEvent.Key != nil { |
| 224 | keyId = enrichedEvent.Key.KeyId |
| 225 | tags = enrichedEvent.Key.Tags |
| 226 | } |
| 227 | |
| 228 | if len(metadata) != 0 { |
| 229 | data, err := json.Marshal(metadata) |
no test coverage detected