mirror of
https://github.com/stulzq/azure-openai-proxy.git
synced 2025-12-18 23:04:19 +01:00
feat: support cors (#92)
This commit is contained in:
28
cmd/main.go
28
cmd/main.go
@@ -33,6 +33,13 @@ func main() {
|
||||
|
||||
gin.SetMode(gin.ReleaseMode)
|
||||
r := gin.Default()
|
||||
|
||||
// if viper get cors is true, then apply corsMiddleware
|
||||
if viper.GetBool("cors") {
|
||||
log.Printf("CORS supported! \n")
|
||||
r.Use(corsMiddleware())
|
||||
}
|
||||
|
||||
registerRoute(r)
|
||||
|
||||
srv := &http.Server{
|
||||
@@ -43,6 +50,26 @@ func main() {
|
||||
runServer(srv)
|
||||
}
|
||||
|
||||
// corsMiddleware sets up the CORS headers for all responses
|
||||
func corsMiddleware() gin.HandlerFunc {
|
||||
return func(c *gin.Context) {
|
||||
// Clear any previously set headers
|
||||
if c.Request.Method != "POST" {
|
||||
c.Writer.Header().Set("Access-Control-Allow-Origin", "*")
|
||||
}
|
||||
c.Writer.Header().Set("Access-Control-Allow-Credentials", "true")
|
||||
c.Writer.Header().Set("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept, Authorization, X-Stainless-OS, X-STAINLESS-LANG, X-STAINLESS-PACKAGE-VERSION, X-STAINLESS-RUNTIME, X-STAINLESS-RUNTIME-VERSION, X-STAINLESS-ARCH")
|
||||
c.Writer.Header().Set("Access-Control-Allow-Methods", "GET, POST, PUT, PATCH, DELETE, OPTIONS")
|
||||
|
||||
// Handle preflight requests
|
||||
if c.Request.Method == "OPTIONS" {
|
||||
c.AbortWithStatus(200)
|
||||
return
|
||||
}
|
||||
c.Next()
|
||||
}
|
||||
}
|
||||
|
||||
func runServer(srv *http.Server) {
|
||||
go func() {
|
||||
log.Printf("Server listening at %s\n", srv.Addr)
|
||||
@@ -66,6 +93,7 @@ func parseFlag() {
|
||||
pflag.StringP("configFile", "c", "config.yaml", "config file")
|
||||
pflag.StringP("listen", "l", ":8080", "listen address")
|
||||
pflag.BoolP("version", "v", false, "version information")
|
||||
pflag.BoolP("cors", "s", false, "cors support")
|
||||
pflag.Parse()
|
||||
if err := viper.BindPFlags(pflag.CommandLine); err != nil {
|
||||
panic(err)
|
||||
|
||||
Reference in New Issue
Block a user