Skip to content

Commit 74548e9

Browse files
observability added
1 parent 79caf8a commit 74548e9

40 files changed

+1048
-196
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ help: ## Show this help message
3737
cleanup-enterprise: ## Clean up enterprise directories if present
3838
@echo "$(GREEN)Cleaning up enterprise...$(NC)"
3939
@if [ -d "ui/app/enterprise" ]; then rm -rf ui/app/enterprise; fi
40-
@if [ -d "ui/app/_fallbacks/enterprise" ]; then rm -rf ui/app/_fallbacks/enterprise; fi
40+
@if [ -d "ui/app/enterprise" ]; then rm -rf ui/app/enterprise; fi
4141
@echo "$(GREEN)Enterprise cleaned up$(NC)"
4242

4343
install-ui: cleanup-enterprise

transports/bifrost-http/handlers/cache.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ func NewCacheHandler(plugin schemas.Plugin, logger schemas.Logger) *CacheHandler
2424
}
2525
}
2626

27-
func (h *CacheHandler) RegisterRoutes(r *router.Router, middlewares ...fasthttp.RequestHandler) {
27+
func (h *CacheHandler) RegisterRoutes(r *router.Router, middlewares ...BifrostHTTPMiddleware) {
2828
r.DELETE("/api/cache/clear/{requestId}", ChainMiddlewares(h.clearCache, middlewares...))
2929
r.DELETE("/api/cache/clear-by-key/{cacheKey}", ChainMiddlewares(h.clearCacheByKey, middlewares...))
3030
}

transports/bifrost-http/handlers/completions.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,7 @@ func (h *CompletionHandler) validateAudioFile(fileHeader *multipart.FileHeader)
264264
}
265265

266266
// RegisterRoutes registers all completion-related routes
267-
func (h *CompletionHandler) RegisterRoutes(r *router.Router, middlewares ...fasthttp.RequestHandler) {
267+
func (h *CompletionHandler) RegisterRoutes(r *router.Router, middlewares ...BifrostHTTPMiddleware) {
268268
// Completion endpoints
269269
r.POST("/v1/text/completions", ChainMiddlewares(h.textCompletion, middlewares...))
270270
r.POST("/v1/chat/completions", ChainMiddlewares(h.chatCompletion, middlewares...))

transports/bifrost-http/handlers/config.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ func NewConfigHandler(client *bifrost.Bifrost, logger schemas.Logger, store *lib
3333

3434
// RegisterRoutes registers the configuration-related routes.
3535
// It adds the `PUT /api/config` endpoint.
36-
func (h *ConfigHandler) RegisterRoutes(r *router.Router, middlewares ...fasthttp.RequestHandler) {
36+
func (h *ConfigHandler) RegisterRoutes(r *router.Router, middlewares ...BifrostHTTPMiddleware) {
3737
r.GET("/api/config", ChainMiddlewares(h.getConfig, middlewares...))
3838
r.PUT("/api/config", ChainMiddlewares(h.updateConfig, middlewares...))
3939
r.GET("/api/version", ChainMiddlewares(h.getVersion, middlewares...))

transports/bifrost-http/handlers/governance.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ type UpdateCustomerRequest struct {
120120
}
121121

122122
// RegisterRoutes registers all governance-related routes for the new hierarchical system
123-
func (h *GovernanceHandler) RegisterRoutes(r *router.Router, middlewares ...fasthttp.RequestHandler) {
123+
func (h *GovernanceHandler) RegisterRoutes(r *router.Router, middlewares ...BifrostHTTPMiddleware) {
124124
// Virtual Key CRUD operations
125125
r.GET("/api/governance/virtual-keys", ChainMiddlewares(h.getVirtualKeys, middlewares...))
126126
r.POST("/api/governance/virtual-keys", ChainMiddlewares(h.createVirtualKey, middlewares...))

transports/bifrost-http/handlers/logging.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ func NewLoggingHandler(logManager logging.LogManager, logger schemas.Logger) *Lo
3030
}
3131

3232
// RegisterRoutes registers all logging-related routes
33-
func (h *LoggingHandler) RegisterRoutes(r *router.Router, middlewares ...fasthttp.RequestHandler) {
33+
func (h *LoggingHandler) RegisterRoutes(r *router.Router, middlewares ...BifrostHTTPMiddleware) {
3434
// Log retrieval with filtering, search, and pagination
3535
r.GET("/api/logs", ChainMiddlewares(h.getLogs, middlewares...))
3636
r.GET("/api/logs/dropped", ChainMiddlewares(h.getDroppedRequests, middlewares...))

transports/bifrost-http/handlers/mcp.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ func NewMCPHandler(client *bifrost.Bifrost, logger schemas.Logger, store *lib.Co
3030
}
3131

3232
// RegisterRoutes registers all MCP-related routes
33-
func (h *MCPHandler) RegisterRoutes(r *router.Router, middlewares ...fasthttp.RequestHandler) {
33+
func (h *MCPHandler) RegisterRoutes(r *router.Router, middlewares ...BifrostHTTPMiddleware) {
3434
// MCP tool execution endpoint
3535
r.POST("/v1/mcp/tool/execute", ChainMiddlewares(h.executeTool, middlewares...))
3636
r.GET("/api/mcp/clients", ChainMiddlewares(h.getMCPClients, middlewares...))

transports/bifrost-http/handlers/middlewares.go

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,49 @@
11
package handlers
22

33
import (
4+
"github.com/maximhq/bifrost/transports/bifrost-http/lib"
45
"github.com/valyala/fasthttp"
56
)
67

8+
// BifrostHTTPMiddleware is a middleware function for the Bifrost HTTP transport
9+
type BifrostHTTPMiddleware func(ctx *fasthttp.RequestCtx)
10+
11+
// CorsMiddleware handles CORS headers for localhost and configured allowed origins
12+
func CorsMiddleware(config *lib.Config, next fasthttp.RequestHandler) fasthttp.RequestHandler {
13+
return func(ctx *fasthttp.RequestCtx) {
14+
origin := string(ctx.Request.Header.Peek("Origin"))
15+
// Check if origin is allowed (localhost always allowed + configured origins)
16+
if IsOriginAllowed(origin, config.ClientConfig.AllowedOrigins) {
17+
ctx.Response.Header.Set("Access-Control-Allow-Origin", origin)
18+
}
19+
// Setting headers
20+
ctx.Response.Header.Set("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS")
21+
ctx.Response.Header.Set("Access-Control-Allow-Headers", "Content-Type, Authorization, X-Requested-With")
22+
ctx.Response.Header.Set("Access-Control-Allow-Credentials", "true")
23+
ctx.Response.Header.Set("Access-Control-Max-Age", "86400")
24+
// Handle preflight OPTIONS requests
25+
if string(ctx.Method()) == "OPTIONS" {
26+
ctx.SetStatusCode(fasthttp.StatusOK)
27+
return
28+
}
29+
next(ctx)
30+
}
31+
}
32+
733
// ChainMiddlewares chains multiple middlewares together
8-
func ChainMiddlewares(handler fasthttp.RequestHandler, middlewares ...fasthttp.RequestHandler) fasthttp.RequestHandler {
34+
func ChainMiddlewares(handler fasthttp.RequestHandler, middlewares ...BifrostHTTPMiddleware) fasthttp.RequestHandler {
935
// If no middlewares, return the original handler
1036
if len(middlewares) == 0 {
1137
return handler
1238
}
13-
1439
return func(ctx *fasthttp.RequestCtx) {
1540
// Execute all middlewares in order
1641
for _, middleware := range middlewares {
1742
middleware(ctx)
43+
// Check if the response is set
44+
if ctx.Response.StatusCode() != 0 {
45+
return
46+
}
1847
}
1948
// Execute the handler last
2049
handler(ctx)

transports/bifrost-http/handlers/plugins.go

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,37 +11,43 @@ import (
1111
"gorm.io/gorm"
1212
)
1313

14+
// PluginsHandler is the handler for the plugins API
1415
type PluginsHandler struct {
1516
logger schemas.Logger
1617
configStore configstore.ConfigStore
1718
}
1819

20+
// NewPluginsHandler creates a new PluginsHandler
1921
func NewPluginsHandler(configStore configstore.ConfigStore, logger schemas.Logger) *PluginsHandler {
2022
return &PluginsHandler{
2123
configStore: configStore,
2224
logger: logger,
2325
}
2426
}
2527

28+
// CreatePluginRequest is the request body for creating a plugin
2629
type CreatePluginRequest struct {
27-
Name string `json:"name"`
28-
Enabled bool `json:"enabled"`
29-
Config map[string]interface{} `json:"config"`
30+
Name string `json:"name"`
31+
Enabled bool `json:"enabled"`
32+
Config map[string]any `json:"config"`
3033
}
3134

35+
// UpdatePluginRequest is the request body for updating a plugin
3236
type UpdatePluginRequest struct {
33-
Enabled bool `json:"enabled"`
34-
Config map[string]interface{} `json:"config"`
37+
Enabled bool `json:"enabled"`
38+
Config map[string]any `json:"config"`
3539
}
3640

37-
func (h *PluginsHandler) RegisterRoutes(r *router.Router, middlewares ...fasthttp.RequestHandler) {
41+
// RegisterRoutes registers the routes for the PluginsHandler
42+
func (h *PluginsHandler) RegisterRoutes(r *router.Router, middlewares ...BifrostHTTPMiddleware) {
3843
r.GET("/api/plugins", ChainMiddlewares(h.getPlugins, middlewares...))
3944
r.GET("/api/plugins/{name}", ChainMiddlewares(h.getPlugin, middlewares...))
4045
r.POST("/api/plugins", ChainMiddlewares(h.createPlugin, middlewares...))
4146
r.PUT("/api/plugins/{name}", ChainMiddlewares(h.updatePlugin, middlewares...))
4247
r.DELETE("/api/plugins/{name}", ChainMiddlewares(h.deletePlugin, middlewares...))
4348
}
4449

50+
// getPlugins gets all plugins
4551
func (h *PluginsHandler) getPlugins(ctx *fasthttp.RequestCtx) {
4652
plugins, err := h.configStore.GetPlugins()
4753
if err != nil {
@@ -50,12 +56,13 @@ func (h *PluginsHandler) getPlugins(ctx *fasthttp.RequestCtx) {
5056
return
5157
}
5258

53-
SendJSON(ctx, map[string]interface{}{
59+
SendJSON(ctx, map[string]any{
5460
"plugins": plugins,
5561
"count": len(plugins),
5662
}, h.logger)
5763
}
5864

65+
// getPlugin gets a plugin by name
5966
func (h *PluginsHandler) getPlugin(ctx *fasthttp.RequestCtx) {
6067
// Safely validate the "name" parameter
6168
nameValue := ctx.UserValue("name")
@@ -91,6 +98,7 @@ func (h *PluginsHandler) getPlugin(ctx *fasthttp.RequestCtx) {
9198
SendJSON(ctx, plugin, h.logger)
9299
}
93100

101+
// createPlugin creates a new plugin
94102
func (h *PluginsHandler) createPlugin(ctx *fasthttp.RequestCtx) {
95103
var request CreatePluginRequest
96104
if err := json.Unmarshal(ctx.PostBody(), &request); err != nil {
@@ -130,12 +138,13 @@ func (h *PluginsHandler) createPlugin(ctx *fasthttp.RequestCtx) {
130138
}
131139

132140
ctx.SetStatusCode(fasthttp.StatusCreated)
133-
SendJSON(ctx, map[string]interface{}{
141+
SendJSON(ctx, map[string]any{
134142
"message": "Plugin created successfully",
135143
"plugin": plugin,
136144
}, h.logger)
137145
}
138146

147+
// updatePlugin updates an existing plugin
139148
func (h *PluginsHandler) updatePlugin(ctx *fasthttp.RequestCtx) {
140149
// Safely validate the "name" parameter
141150
nameValue := ctx.UserValue("name")
@@ -160,9 +169,16 @@ func (h *PluginsHandler) updatePlugin(ctx *fasthttp.RequestCtx) {
160169

161170
// Check if plugin exists
162171
if _, err := h.configStore.GetPlugin(name); err != nil {
163-
h.logger.Warn("plugin not found for update: %s", name)
164-
SendError(ctx, fasthttp.StatusNotFound, "Plugin not found", h.logger)
165-
return
172+
// If doesn't exist, create it
173+
if err := h.configStore.CreatePlugin(&configstore.TablePlugin{
174+
Name: name,
175+
Enabled: false,
176+
Config: map[string]any{},
177+
}); err != nil {
178+
h.logger.Error("failed to create plugin: %v", err)
179+
SendError(ctx, 500, "Failed to create plugin", h.logger)
180+
return
181+
}
166182
}
167183

168184
var request UpdatePluginRequest
@@ -199,6 +215,7 @@ func (h *PluginsHandler) updatePlugin(ctx *fasthttp.RequestCtx) {
199215
}, h.logger)
200216
}
201217

218+
// deletePlugin deletes an existing plugin
202219
func (h *PluginsHandler) deletePlugin(ctx *fasthttp.RequestCtx) {
203220
// Safely validate the "name" parameter
204221
nameValue := ctx.UserValue("name")

transports/bifrost-http/handlers/providers.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ type ErrorResponse struct {
5959
}
6060

6161
// RegisterRoutes registers all provider management routes
62-
func (h *ProviderHandler) RegisterRoutes(r *router.Router, middlewares ...fasthttp.RequestHandler) {
62+
func (h *ProviderHandler) RegisterRoutes(r *router.Router, middlewares ...BifrostHTTPMiddleware) {
6363
// Provider CRUD operations
6464
r.GET("/api/providers", ChainMiddlewares(h.listProviders, middlewares...))
6565
r.GET("/api/providers/{provider}", ChainMiddlewares(h.getProvider, middlewares...))

0 commit comments

Comments
 (0)