use gin-contrib/cors middleware

This commit is contained in:
Michael Yang
2023-07-21 18:01:24 -07:00
parent 65d93a86b2
commit 8609db77ea
3 changed files with 61 additions and 1 deletions

View File

@@ -14,6 +14,7 @@ import (
"time"
"dario.cat/mergo"
"github.com/gin-contrib/cors"
"github.com/gin-gonic/gin"
"github.com/jmorganca/ollama/api"
@@ -228,7 +229,22 @@ func ListModelsHandler(c *gin.Context) {
}
func Serve(ln net.Listener) error {
config := cors.DefaultConfig()
config.AllowWildcard = true
// only allow http/https from localhost
config.AllowOrigins = []string{
"http://localhost",
"http://localhost:*",
"https://localhost",
"https://localhost:*",
"http://127.0.0.1",
"http://127.0.0.1:*",
"https://127.0.0.1",
"https://127.0.0.1:*",
}
r := gin.Default()
r.Use(cors.New(config))
r.GET("/", func(c *gin.Context) {
c.String(http.StatusOK, "Ollama is running")