This repository has been archived by the owner on Jul 29, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
update cors and deeptrain oauth link
- Loading branch information
1 parent
cdde135
commit 6d7f921
Showing
3 changed files
with
44 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package middleware | ||
|
||
import ( | ||
"github.com/gin-gonic/gin" | ||
"net/http" | ||
) | ||
|
||
var allowedOrigins = []string{ | ||
"https://fystart.cn", | ||
"https://www.fystart.cn", | ||
"https://40code.com", | ||
"https://www.40code.com", | ||
} | ||
|
||
func Contains[T comparable](value T, slice []T) bool { | ||
for _, item := range slice { | ||
if item == value { | ||
return true | ||
} | ||
} | ||
return false | ||
} | ||
|
||
func CORSMiddleware() gin.HandlerFunc { | ||
return func(c *gin.Context) { | ||
origin := c.Request.Header.Get("Origin") | ||
if Contains(origin, allowedOrigins) { | ||
c.Writer.Header().Set("Access-Control-Allow-Origin", origin) | ||
c.Writer.Header().Set("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS") | ||
c.Writer.Header().Set("Access-Control-Allow-Headers", "Origin, Content-Type, Content-Length, Accept-Encoding, X-CSRF-Token, Authorization") | ||
|
||
if c.Request.Method == "OPTIONS" { | ||
c.Writer.Header().Set("Access-Control-Max-Age", "3600") | ||
c.AbortWithStatus(http.StatusOK) | ||
return | ||
} | ||
} | ||
|
||
c.Next() | ||
} | ||
} |