Add Access-Control-Allow-Origin CORS header to /v1/models endpoint

- match behavior of llama.cpp where the Origin in request is used
- add test for listModelsHandler
This commit is contained in:
Benson Wong
2024-12-03 15:53:59 -08:00
parent da2326bdc7
commit 18c134624d
2 changed files with 73 additions and 0 deletions

View File

@@ -98,6 +98,10 @@ func (pm *ProxyManager) listModelsHandler(c *gin.Context) {
// Set the Content-Type header to application/json
c.Header("Content-Type", "application/json")
if origin := c.Request.Header.Get("Origin"); origin != "" {
c.Header("Access-Control-Allow-Origin", origin)
}
// Encode the data as JSON and write it to the response writer
if err := json.NewEncoder(c.Writer).Encode(map[string]interface{}{"data": data}); err != nil {
c.AbortWithError(http.StatusInternalServerError, fmt.Errorf("error encoding JSON"))