From 2c3e3e27f7b122a7193e19501b56403df512b531 Mon Sep 17 00:00:00 2001 From: Benson Wong Date: Fri, 31 Jan 2025 10:09:00 -0800 Subject: [PATCH] Support OPTIONS requests (#42) Add middleware that responds with permissive OPTIONS headers for all request paths. --- proxy/proxymanager.go | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/proxy/proxymanager.go b/proxy/proxymanager.go index 684dd37..edad17a 100644 --- a/proxy/proxymanager.go +++ b/proxy/proxymanager.go @@ -69,6 +69,19 @@ func New(config *Config) *ProxyManager { }) } + // see: https://github.com/mostlygeek/llama-swap/issues/42 + // respond with permissive OPTIONS for any endpoint + pm.ginEngine.Use(func(c *gin.Context) { + if c.Request.Method == "OPTIONS" { + c.Header("Access-Control-Allow-Origin", "*") + c.Header("Access-Control-Allow-Methods", "GET, POST, OPTIONS") + c.Header("Access-Control-Allow-Headers", "Content-Type, Authorization") + c.AbortWithStatus(204) + return + } + c.Next() + }) + // Set up routes using the Gin engine pm.ginEngine.POST("/v1/chat/completions", pm.proxyOAIHandler) // Support legacy /v1/completions api, see issue #12