This repository has been archived on 2025-08-09. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
web-app-template/api/middleware/cors.go

29 lines
643 B
Go

package middleware
import (
"log"
"net/http"
"os"
)
func EnableCors(next http.Handler) http.Handler {
var frontent_url = os.Getenv("FRONTEND_URL")
if frontent_url == "" {
log.Fatal("FRONTEND_URL is not set")
}
log.Println("FRONTEND_URL is", frontent_url)
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Access-Control-Allow-Origin", frontent_url)
w.Header().Set("Access-Control-Allow-Methods", "GET, POST, DELETE")
w.Header().Set("Access-Control-Allow-Headers", "Authorization")
if r.Method == "OPTIONS" {
w.WriteHeader(http.StatusOK)
return
}
next.ServeHTTP(w, r)
})
}