Add CORS headers so bus can be accessed cross-origin

This commit is contained in:
James Mills 2022-03-31 12:45:18 +10:00
parent 7181b6df1b
commit a5ef82800e
No known key found for this signature in database
GPG Key ID: AC4C014F1440EBD6
2 changed files with 10 additions and 2 deletions

View File

@ -83,6 +83,14 @@ func parseArgs() error {
return nil
}
func corsMiddleware(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-Headers", "*")
next.ServeHTTP(w, r)
})
}
func main() {
parseArgs()
@ -109,7 +117,7 @@ func main() {
}
mb := msgbus.New(&opts)
http.Handle("/", mb)
http.Handle("/", corsMiddleware(mb))
http.Handle("/metrics", mb.Metrics().Handler())
log.Infof("msgbusd %s listening on %s", msgbus.FullVersion(), bind)
log.Fatal(http.ListenAndServe(bind, nil))

2
go.mod
View File

@ -3,7 +3,7 @@ module git.mills.io/prologic/msgbus
go 1.17
require (
github.com/gorilla/websocket v1.5.0 // indirect
github.com/gorilla/websocket v1.5.0
github.com/jpillora/backoff v1.0.0
github.com/mitchellh/go-homedir v1.1.0
github.com/mmcloughlin/professor v0.0.0-20170922221822-6b97112ab8b3