diff --git a/internal/hotreload/cors.go b/internal/hotreload/cors.go new file mode 100644 index 0000000..be15286 --- /dev/null +++ b/internal/hotreload/cors.go @@ -0,0 +1,18 @@ +package hotreload + +import "net/http" + +func enableCORS(next http.Handler) http.Handler { + return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + w.Header().Set("Access-Control-Allow-Origin", "*") + w.Header().Set("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS") + w.Header().Set("Access-Control-Allow-Headers", "Content-Type") + + if r.Method == http.MethodOptions { + w.WriteHeader(http.StatusOK) + return + } + + next.ServeHTTP(w, r) + }) +} diff --git a/internal/hotreload/hotreload.go b/internal/hotreload/hotreload.go index 80c0374..87f7611 100644 --- a/internal/hotreload/hotreload.go +++ b/internal/hotreload/hotreload.go @@ -69,7 +69,7 @@ func WithLogger(w *io.Writer) func(*HotReloadServer) { func InitHotReloadServer(port int, options ...func(*HotReloadServer)) *HotReloadServer { // set a default logger - h := newHandler() + h := enableCORS(newHandler()) hrs := &HotReloadServer{ logwriter: &io.Discard, server: &http.Server{