Skip to content

Commit

Permalink
feat: add func to publish event in socket (#46)
Browse files Browse the repository at this point in the history
  • Loading branch information
CorrectRoadH authored Feb 23, 2024
1 parent 83f6b55 commit 5ee3ad9
Showing 1 changed file with 36 additions and 0 deletions.
36 changes: 36 additions & 0 deletions external/message_bus.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
package external

import (
"bytes"
"context"
"encoding/json"
"fmt"
"net"
"net/http"
"os"
"path/filepath"
"strings"
Expand Down Expand Up @@ -64,3 +69,34 @@ func GetMessageBusAddress(runtimePath string) (string, error) {

return strings.TrimRight(address, "/") + APIMessageBus, nil
}
func PublishEventInSocket(ctx context.Context, eventType EventType, properties map[string]string) (*http.Response, error) {
socketPath := "/tmp/message-bus.sock"
httpClient := &http.Client{
Transport: &http.Transport{
DialContext: func(ctx context.Context, _, _ string) (net.Conn, error) {
return net.Dial("unix", socketPath)
},
},
}

body, err := json.Marshal(properties)
if err != nil {
return nil, err
}

req, err := http.NewRequest("POST",
fmt.Sprintf("http://unix/v2/message_bus/event/%s/%s", eventType.SourceID, eventType.Name),
bytes.NewBuffer(body),
)
if err != nil {
return nil, err
}

req.Header.Set("Content-Type", "application/json")
resp, err := httpClient.Do(req)
if err != nil {
return resp, err
}
defer resp.Body.Close()
return resp, nil
}

0 comments on commit 5ee3ad9

Please sign in to comment.