feat: enable CORS for REST API

This commit is contained in:
Prem Chaitanya Prathi 2024-03-22 12:41:34 +05:30 committed by Arseniy Klempner
parent 32442d72ee
commit 6c4bc43016
No known key found for this signature in database
GPG Key ID: 59967D458EFBF01B
8 changed files with 27 additions and 2 deletions

View File

@ -52,6 +52,7 @@ func NewAdminService(node *node.WakuNode, m *chi.Mux, log *zap.Logger) *AdminSer
}
func (a *AdminService) getV1Peers(w http.ResponseWriter, req *http.Request) {
enableCors(&w)
peers, err := a.node.Peers()
if err != nil {
a.log.Error("failed to fetch peers", zap.Error(err))
@ -89,6 +90,7 @@ func (a *AdminService) getV1Peers(w http.ResponseWriter, req *http.Request) {
}
func (a *AdminService) postV1Peer(w http.ResponseWriter, req *http.Request) {
enableCors(&w)
var pInfo WakuPeerInfo
var topics []string
var protos []protocol.ID

View File

@ -53,6 +53,7 @@ func NewDebugService(node *node.WakuNode, m *chi.Mux) *DebugService {
type VersionResponse string
func (d *DebugService) getV1Info(w http.ResponseWriter, req *http.Request) {
enableCors(&w)
response := new(InfoReply)
response.ENRUri = d.node.ENR().String()
for _, addr := range d.node.ListenAddresses() {
@ -62,6 +63,7 @@ func (d *DebugService) getV1Info(w http.ResponseWriter, req *http.Request) {
}
func (d *DebugService) getV1Version(w http.ResponseWriter, req *http.Request) {
enableCors(&w)
response := VersionResponse(node.GetVersionInfo().String())
writeErrOrResponse(w, nil, response)
}

View File

@ -103,6 +103,7 @@ func convertFilterErrorToHttpStatus(err error) (int, string) {
// 404 when request failed or no suitable peers
// 200 when ping successful
func (s *FilterService) ping(w http.ResponseWriter, req *http.Request) {
enableCors(&w)
requestID := chi.URLParam(req, "requestId")
if requestID == "" {
writeResponse(w, &filterSubscriptionResponse{
@ -155,6 +156,7 @@ type filterSubscriptionResponse struct {
// 200 on single returned successful subscription
// NOTE: subscribe on filter client randomly selects a peer if missing for given pubSubTopic
func (s *FilterService) subscribe(w http.ResponseWriter, req *http.Request) {
enableCors(&w)
message := filterSubscriptionRequest{}
if !s.readBody(w, req, &message) {
return
@ -203,6 +205,7 @@ func (s *FilterService) subscribe(w http.ResponseWriter, req *http.Request) {
// NOTE: unsubscribe on filter client will remove subscription from all peers with matching pubSubTopic, if peerId is not provided
// to match functionality in nwaku, we will randomly select a peer that supports filter protocol.
func (s *FilterService) unsubscribe(w http.ResponseWriter, req *http.Request) {
enableCors(&w)
message := filterSubscriptionRequest{} // as pubSubTopics can also be present
if !s.readBody(w, req, &message) {
return
@ -250,6 +253,7 @@ func (s *FilterService) unsubscribe(w http.ResponseWriter, req *http.Request) {
}
func (s *FilterService) unsubscribeGetMessage(result *filter.WakuFilterPushResult) string {
if result == nil {
return http.StatusText(http.StatusOK)
}
@ -291,6 +295,7 @@ func (s *FilterService) readBody(w http.ResponseWriter, req *http.Request, messa
// 200 on all successful unsubscribe
// unsubscribe all subscriptions for a given peer
func (s *FilterService) unsubscribeAll(w http.ResponseWriter, req *http.Request) {
enableCors(&w)
message := filterUnsubscribeAllRequest{}
if !s.readBody(w, req, &message) {
return
@ -342,6 +347,7 @@ func (s FilterService) getRandomFilterPeer(ctx context.Context, requestId string
}
func (s *FilterService) getMessagesByContentTopic(w http.ResponseWriter, req *http.Request) {
enableCors(&w)
contentTopic := topicFromPath(w, req, "contentTopic", s.log)
if contentTopic == "" {
return
@ -355,6 +361,7 @@ func (s *FilterService) getMessagesByContentTopic(w http.ResponseWriter, req *ht
}
func (s *FilterService) getMessagesByPubsubTopic(w http.ResponseWriter, req *http.Request) {
enableCors(&w)
contentTopic := topicFromPath(w, req, "contentTopic", s.log)
if contentTopic == "" {
return
@ -371,6 +378,7 @@ func (s *FilterService) getMessagesByPubsubTopic(w http.ResponseWriter, req *htt
// 200 on all successful unsubscribe
// unsubscribe all subscriptions for a given peer
func (s *FilterService) getMessages(w http.ResponseWriter, req *http.Request, pubsubTopic, contentTopic string) {
enableCors(&w)
msgs, err := s.cache.getMessages(pubsubTopic, contentTopic)
if err != nil {
writeGetMessageErr(w, err, http.StatusNotFound, s.log)

View File

@ -30,6 +30,7 @@ func NewHealthService(node *node.WakuNode, m *chi.Mux) *HealthService {
type HealthResponse string
func (d *HealthService) getHealth(w http.ResponseWriter, r *http.Request) {
enableCors(&w)
if d.node.RLNRelay() != nil {
isReady, err := d.node.RLNRelay().IsReady(r.Context())
if err != nil {

View File

@ -43,6 +43,7 @@ type lightpushRequest struct {
// handled error codes are 200, 400, 500, 503
func (serv *LightpushService) postMessagev1(w http.ResponseWriter, req *http.Request) {
enableCors(&w)
request := &lightpushRequest{}
decoder := json.NewDecoder(req.Body)
if err := decoder.Decode(request); err != nil {

View File

@ -54,6 +54,7 @@ func NewRelayService(node *node.WakuNode, m *chi.Mux, cacheCapacity uint, log *z
}
func (r *RelayService) deleteV1Subscriptions(w http.ResponseWriter, req *http.Request) {
enableCors(&w)
var topics []string
decoder := json.NewDecoder(req.Body)
if err := decoder.Decode(&topics); err != nil {
@ -75,6 +76,7 @@ func (r *RelayService) deleteV1Subscriptions(w http.ResponseWriter, req *http.Re
}
func (r *RelayService) postV1Subscriptions(w http.ResponseWriter, req *http.Request) {
enableCors(&w)
var topics []string
decoder := json.NewDecoder(req.Body)
if err := decoder.Decode(&topics); err != nil {
@ -114,6 +116,7 @@ func (r *RelayService) postV1Subscriptions(w http.ResponseWriter, req *http.Requ
}
func (r *RelayService) getV1Messages(w http.ResponseWriter, req *http.Request) {
enableCors(&w)
topic := topicFromPath(w, req, "topic", r.log)
if topic == "" {
r.log.Debug("topic is not specified, using default waku topic")
@ -162,6 +165,7 @@ func (r *RelayService) getV1Messages(w http.ResponseWriter, req *http.Request) {
}
func (r *RelayService) postV1Message(w http.ResponseWriter, req *http.Request) {
enableCors(&w)
topic := topicFromPath(w, req, "topic", r.log)
if topic == "" {
r.log.Debug("topic is not specified, using default waku topic")
@ -203,6 +207,7 @@ func (r *RelayService) postV1Message(w http.ResponseWriter, req *http.Request) {
}
func (r *RelayService) deleteV1AutoSubscriptions(w http.ResponseWriter, req *http.Request) {
enableCors(&w)
var cTopics []string
decoder := json.NewDecoder(req.Body)
if err := decoder.Decode(&cTopics); err != nil {
@ -221,6 +226,7 @@ func (r *RelayService) deleteV1AutoSubscriptions(w http.ResponseWriter, req *htt
}
func (r *RelayService) postV1AutoSubscriptions(w http.ResponseWriter, req *http.Request) {
enableCors(&w)
var cTopics []string
decoder := json.NewDecoder(req.Body)
if err := decoder.Decode(&cTopics); err != nil {
@ -247,7 +253,7 @@ func (r *RelayService) postV1AutoSubscriptions(w http.ResponseWriter, req *http.
}
func (r *RelayService) getV1AutoMessages(w http.ResponseWriter, req *http.Request) {
enableCors(&w)
cTopic := topicFromPath(w, req, "contentTopic", r.log)
sub, err := r.node.Relay().GetSubscription(cTopic)
if err != nil {
@ -280,7 +286,7 @@ func (r *RelayService) getV1AutoMessages(w http.ResponseWriter, req *http.Reques
}
func (r *RelayService) postV1AutoMessage(w http.ResponseWriter, req *http.Request) {
enableCors(&w)
var restMessage *RestWakuMessage
decoder := json.NewDecoder(req.Body)
if err := decoder.Decode(&restMessage); err != nil {

View File

@ -187,6 +187,7 @@ func toStoreResponse(result *store.Result) StoreResponse {
}
func (d *StoreService) getV1Messages(w http.ResponseWriter, r *http.Request) {
enableCors(&w)
query, options, err := getStoreParams(r)
if err != nil {
writeStoreError(w, http.StatusBadRequest, err)

View File

@ -81,3 +81,7 @@ func writeGetMessageErr(w http.ResponseWriter, err error, code int, logger *zap.
logger.Error("writing response", zap.Error(err))
}
}
func enableCors(w *http.ResponseWriter) {
(*w).Header().Set("Access-Control-Allow-Origin", "*")
}