Skip to content

Commit

Permalink
feat: cors
Browse files Browse the repository at this point in the history
  • Loading branch information
idreaminteractive committed Jun 21, 2024
1 parent fd675c1 commit 6f68faa
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
18 changes: 18 additions & 0 deletions internal/hotreload/cors.go
Original file line number Diff line number Diff line change
@@ -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)
})
}
2 changes: 1 addition & 1 deletion internal/hotreload/hotreload.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down

0 comments on commit 6f68faa

Please sign in to comment.