sendOK refactored, moved to own file

This commit is contained in:
0ceanSlim 2024-07-24 17:00:59 -04:00
parent 1249c6cb82
commit 0dd29c5bcb
2 changed files with 17 additions and 8 deletions

View File

@ -45,7 +45,7 @@ func HandleEvent(ws *websocket.Conn, message []interface{}) {
func HandleKind(ctx context.Context, evt relay.Event, ws *websocket.Conn) { func HandleKind(ctx context.Context, evt relay.Event, ws *websocket.Conn) {
if !utils.CheckSignature(evt) { if !utils.CheckSignature(evt) {
OKResponse(ws, evt.ID, false, "invalid: signature verification failed") sendOK(ws, evt.ID, false, "invalid: signature verification failed")
return return
} }
@ -77,15 +77,11 @@ func HandleKind(ctx context.Context, evt relay.Event, ws *websocket.Conn) {
} }
if err != nil { if err != nil {
OKResponse(ws, evt.ID, false, fmt.Sprintf("error: %v", err)) sendOK(ws, evt.ID, false, fmt.Sprintf("error: %v", err))
return return
} }
OKResponse(ws, evt.ID, true, "") sendOK(ws, evt.ID, true, "")
} }
func OKResponse(ws *websocket.Conn, eventID string, status bool, message string) {
response := []interface{}{"OK", eventID, status, message}
responseBytes, _ := json.Marshal(response)
websocket.Message.Send(ws, string(responseBytes))
}

13
relay/handlers/ok.go Normal file
View File

@ -0,0 +1,13 @@
package handlers
import (
"encoding/json"
"golang.org/x/net/websocket"
)
func sendOK(ws *websocket.Conn, eventID string, status bool, message string) {
response := []interface{}{"OK", eventID, status, message}
responseBytes, _ := json.Marshal(response)
websocket.Message.Send(ws, string(responseBytes))
}