29 lines
561 B
Go
29 lines
561 B
Go
package middleware
|
|
|
|
import (
|
|
"log"
|
|
"log/slog"
|
|
"net/http"
|
|
"os"
|
|
)
|
|
|
|
func EnableCors(next http.Handler) http.Handler {
|
|
var base_url = os.Getenv("BASE_URL")
|
|
if base_url == "" {
|
|
log.Fatal("BASE_URL is not set")
|
|
}
|
|
slog.Info("BASE_URL is " + base_url)
|
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
w.Header().Set("Access-Control-Allow-Origin", base_url)
|
|
w.Header().Set("Access-Control-Allow-Methods", "GET, POST, DELETE")
|
|
|
|
if r.Method == "OPTIONS" {
|
|
w.WriteHeader(http.StatusOK)
|
|
return
|
|
}
|
|
|
|
next.ServeHTTP(w, r)
|
|
})
|
|
}
|