mirror of
https://github.com/Dvorinka/SendMail.git
synced 2026-06-03 16:42:57 +00:00
cors
This commit is contained in:
@@ -67,26 +67,12 @@ func sendHandler(w http.ResponseWriter, r *http.Request) {
|
|||||||
|
|
||||||
func enableCors(handler http.HandlerFunc) http.HandlerFunc {
|
func enableCors(handler http.HandlerFunc) http.HandlerFunc {
|
||||||
return func(w http.ResponseWriter, r *http.Request) {
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
// Allow all origins
|
||||||
origin := r.Header.Get("Origin")
|
origin := r.Header.Get("Origin")
|
||||||
|
if origin != "" {
|
||||||
// Allow localhost development URLs
|
w.Header().Set("Access-Control-Allow-Origin", "*")
|
||||||
if strings.HasPrefix(origin, "http://localhost:") ||
|
w.Header().Set("Access-Control-Allow-Methods", "POST, GET, OPTIONS")
|
||||||
strings.HasPrefix(origin, "http://127.0.0.1:") {
|
w.Header().Set("Access-Control-Allow-Headers", "Accept, Content-Type, Content-Length, Accept-Encoding, Authorization")
|
||||||
w.Header().Set("Access-Control-Allow-Origin", origin)
|
|
||||||
w.Header().Set("Access-Control-Allow-Methods", "POST, OPTIONS")
|
|
||||||
w.Header().Set("Access-Control-Allow-Headers", "Content-Type")
|
|
||||||
} else {
|
|
||||||
// Check production domains
|
|
||||||
cleanOrigin := strings.TrimPrefix(strings.TrimPrefix(origin, "http://"), "https://")
|
|
||||||
cleanOrigin = strings.Split(cleanOrigin, "/")[0]
|
|
||||||
|
|
||||||
// Check if the origin's domain is in our domain map
|
|
||||||
_, allowed := domainMap[cleanOrigin]
|
|
||||||
if allowed {
|
|
||||||
w.Header().Set("Access-Control-Allow-Origin", origin)
|
|
||||||
w.Header().Set("Access-Control-Allow-Methods", "POST, OPTIONS")
|
|
||||||
w.Header().Set("Access-Control-Allow-Headers", "Content-Type")
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Handle preflight requests
|
// Handle preflight requests
|
||||||
|
|||||||
Reference in New Issue
Block a user