backend for xcvr appview
2
fork

Configure Feed

Select the types of activity you want to include in your feed.

better support CORS for development

+16 -8
+16 -8
server/cmd/main.go
··· 32 32 createChannel(channel{Band: "general", Sign: "this is the general channel"}, false) 33 33 createChannel(channel{Band: "sneep", Sign: "snirp"}, true) 34 34 fmt.Println("hello world") 35 - http.HandleFunc("GET /xrpc/getChannels", withCORS(getChannels)) 36 - http.HandleFunc("POST /xrpc/initChannel", initChannel) 37 - http.ListenAndServe(":8080", nil) 35 + mux := http.NewServeMux() 36 + mux.HandleFunc("GET /xrpc/getChannels", getChannels) 37 + mux.HandleFunc("POST /xrpc/initChannel", initChannel) 38 + 39 + http.ListenAndServe(":8080", withCORSAll(mux)) 38 40 } 39 41 40 42 func initChannel(w http.ResponseWriter, r *http.Request) { ··· 72 74 iePort 73 75 ) 74 76 77 + //TODO: can changes to bandToServer after unlock create data race? 75 78 func isValidInit(c channel) initError { 76 79 if c.Band == "" { 77 80 return ieNoBand ··· 79 82 if len(c.Band) > 31 { 80 83 return ieLongBand 81 84 } 85 + channelsMu.Lock() 82 86 _, ok := bandToServer[c.Band] 87 + channelsMu.Unlock() 83 88 if ok { 84 89 return ieCollision 85 90 } ··· 166 171 return (nl.Addr().(*net.TCPAddr)).Port, nil 167 172 } 168 173 169 - func withCORS(h http.HandlerFunc) http.HandlerFunc { 170 - return func(w http.ResponseWriter, r *http.Request) { 174 + 175 + func withCORSAll(h http.Handler) http.Handler { 176 + return http.HandlerFunc(func (w http.ResponseWriter, r *http.Request) { 177 + fmt.Println("incoming request:", r.Method, r.URL.Path) 171 178 w.Header().Set("Access-Control-Allow-Origin", "http://localhost:5173") 172 179 w.Header().Set("Access-Control-Allow-Methods", "GET, POST, OPTIONS") 173 180 w.Header().Set("Access-Control-Allow-Headers", "Content-Type") 174 - if r.Method == "OPTIONSONS" { 181 + if r.Method == "OPTIONS" { 175 182 w.WriteHeader(http.StatusNoContent) 183 + return 176 184 } 177 - h(w, r) 178 - } 185 + h.ServeHTTP(w, r) 186 + }) 179 187 }