Refactor CORS handling and remove response's CORS headers
This commit is contained in:
32
cors.go
32
cors.go
@@ -1,28 +1,20 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"log"
|
||||||
|
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
)
|
)
|
||||||
|
|
||||||
// this function is aborded
|
func sendCORSHeaders(c *gin.Context) {
|
||||||
func corsMiddleware() gin.HandlerFunc {
|
log.Println("sendCORSHeaders")
|
||||||
return func(c *gin.Context) {
|
if c.Writer.Header().Get("Access-Control-Allow-Origin") == "" {
|
||||||
// set cors header
|
c.Header("Access-Control-Allow-Origin", "*")
|
||||||
header := c.Request.Header
|
}
|
||||||
if header.Get("Access-Control-Allow-Origin") == "" {
|
if c.Writer.Header().Get("Access-Control-Allow-Methods") == "" {
|
||||||
c.Header("Access-Control-Allow-Origin", "*")
|
c.Header("Access-Control-Allow-Methods", "POST, GET, OPTIONS, PUT, DELETE, PATCH")
|
||||||
}
|
}
|
||||||
if header.Get("Access-Control-Allow-Methods") == "" {
|
if c.Writer.Header().Get("Access-Control-Allow-Headers") == "" {
|
||||||
c.Header("Access-Control-Allow-Methods", "POST, GET, OPTIONS, PUT, DELETE, PATCH")
|
c.Header("Access-Control-Allow-Headers", "Origin, Authorization, Content-Type")
|
||||||
}
|
|
||||||
if header.Get("Access-Control-Allow-Headers") == "" {
|
|
||||||
c.Header("Access-Control-Allow-Headers", "Origin, Authorization, Content-Type")
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func sendCORSHeaders(c *gin.Context) {
|
|
||||||
c.Header("Access-Control-Allow-Origin", "*")
|
|
||||||
c.Header("Access-Control-Allow-Methods", "POST, GET, OPTIONS, PUT, DELETE, PATCH")
|
|
||||||
c.Header("Access-Control-Allow-Headers", "Origin, Authorization, Content-Type")
|
|
||||||
}
|
|
||||||
|
|||||||
1
main.go
1
main.go
@@ -13,6 +13,7 @@ import (
|
|||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
"github.com/penglongli/gin-metrics/ginmetrics"
|
"github.com/penglongli/gin-metrics/ginmetrics"
|
||||||
"gorm.io/driver/postgres"
|
"gorm.io/driver/postgres"
|
||||||
|
|
||||||
"gorm.io/driver/sqlite"
|
"gorm.io/driver/sqlite"
|
||||||
"gorm.io/gorm"
|
"gorm.io/gorm"
|
||||||
)
|
)
|
||||||
|
|||||||
19
process.go
19
process.go
@@ -136,16 +136,13 @@ func processRequest(c *gin.Context, upstream *OPENAI_UPSTREAM, record *Record, s
|
|||||||
record.ResponseTime = time.Since(record.CreatedAt)
|
record.ResponseTime = time.Since(record.CreatedAt)
|
||||||
record.Status = r.StatusCode
|
record.Status = r.StatusCode
|
||||||
|
|
||||||
// handle reverse proxy cors header if upstream do not set that
|
// remove response's cors headers
|
||||||
if r.Header.Get("Access-Control-Allow-Origin") == "" {
|
r.Header.Del("Access-Control-Allow-Origin")
|
||||||
c.Header("Access-Control-Allow-Origin", "*")
|
r.Header.Del("Access-Control-Allow-Methods")
|
||||||
}
|
r.Header.Del("Access-Control-Allow-Headers")
|
||||||
if r.Header.Get("Access-Control-Allow-Methods") == "" {
|
r.Header.Del("access-control-allow-origin")
|
||||||
c.Header("Access-Control-Allow-Methods", "POST, GET, OPTIONS, PUT, DELETE, PATCH")
|
r.Header.Del("access-control-allow-methods")
|
||||||
}
|
r.Header.Del("access-control-allow-headers")
|
||||||
if r.Header.Get("Access-Control-Allow-Headers") == "" {
|
|
||||||
c.Header("Access-Control-Allow-Headers", "Origin, Authorization, Content-Type")
|
|
||||||
}
|
|
||||||
|
|
||||||
if !shouldResponse && r.StatusCode != 200 {
|
if !shouldResponse && r.StatusCode != 200 {
|
||||||
log.Println("[proxy.modifyResponse]: upstream return not 200 and should not response", r.StatusCode)
|
log.Println("[proxy.modifyResponse]: upstream return not 200 and should not response", r.StatusCode)
|
||||||
@@ -163,6 +160,8 @@ func processRequest(c *gin.Context, upstream *OPENAI_UPSTREAM, record *Record, s
|
|||||||
record.Status = r.StatusCode
|
record.Status = r.StatusCode
|
||||||
return errRet
|
return errRet
|
||||||
}
|
}
|
||||||
|
// handle reverse proxy cors header if upstream do not set that
|
||||||
|
sendCORSHeaders(c)
|
||||||
// count success
|
// count success
|
||||||
r.Body = io.NopCloser(io.TeeReader(r.Body, &buf))
|
r.Body = io.NopCloser(io.TeeReader(r.Body, &buf))
|
||||||
contentType = r.Header.Get("content-type")
|
contentType = r.Header.Get("content-type")
|
||||||
|
|||||||
Reference in New Issue
Block a user