MCPcopy Create free account
hub / github.com/bricks-cloud/BricksLLM / getMiddleware

Function getMiddleware

internal/server/web/proxy/middleware.go:172–1277  ·  view source on GitHub ↗
(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)

Source from the content-addressed store, hash-verified

170}
171
172func 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)

Callers 1

NewProxyServerFunction · 0.85

Calls 15

NewUuidFunction · 0.92
SetLogToCtxFunction · 0.92
IncrFunction · 0.92
TimingFunction · 0.92
JSONFunction · 0.85
getProviderFunction · 0.85
logCompletionRequestFunction · 0.85
logCreateMessageRequestFunction · 0.85
logEmbeddingRequestFunction · 0.85
logRequestFunction · 0.85

Tested by

no test coverage detected