github.com/volts-dev/volts@v0.0.0-20240120094013-5e9c65924106/broker/http/handler.go (about)

     1  package http
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/volts-dev/volts/broker"
     7  	"github.com/volts-dev/volts/router"
     8  )
     9  
    10  func (h *httpBroker) Handler(ctx *router.THttpContext) {
    11  	var m broker.Message
    12  	err := ctx.Body().Decode(&m)
    13  	if err != nil {
    14  
    15  	}
    16  
    17  	topic := m.Header["v-topic"]
    18  	delete(m.Header, "v-topic")
    19  
    20  	if len(topic) == 0 {
    21  		//errr := merr.InternalServerError("go.micro.broker", "Topic not found")
    22  		err := fmt.Errorf("Error parsing request body: %v", err)
    23  
    24  		ctx.WriteHeader(500)
    25  		ctx.Write([]byte(err.Error()))
    26  		return
    27  	}
    28  
    29  	event := &event{
    30  		m: &m,
    31  		t: topic,
    32  	}
    33  	id := ctx.MethodParams().FieldByName("id").AsString()
    34  
    35  	//nolint:prealloc
    36  	var subs []broker.Handler
    37  
    38  	ctx.Router()
    39  	h.RLock()
    40  	for _, subscriber := range h.subscribers[topic] {
    41  		if id != subscriber.id {
    42  			continue
    43  		}
    44  		subs = append(subs, subscriber.fn)
    45  	}
    46  	h.RUnlock()
    47  
    48  	// execute the handler
    49  	for _, fn := range subs {
    50  		event.err = fn(event)
    51  	}
    52  }