Skip to content

Commit db771b4

Browse files
committed
added compatibility middleware
added Notification Payload Aliases to improve Compatibility with multiple Apps
1 parent 6a6f954 commit db771b4

File tree

2 files changed

+131
-1
lines changed

2 files changed

+131
-1
lines changed
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
package middlewares
2+
3+
import (
4+
"bytes"
5+
"encoding/json"
6+
"io"
7+
"net/http"
8+
"strconv"
9+
10+
log "github.com/codeshelldev/secured-signal-api/utils/logger"
11+
)
12+
13+
type MessageAlias struct {
14+
Alias string
15+
Priority int
16+
}
17+
18+
type BodyMiddleware struct {
19+
Next http.Handler
20+
MessageAliases []MessageAlias
21+
}
22+
23+
func (data BodyMiddleware) Use() http.Handler {
24+
next := data.Next
25+
messageAliases := data.MessageAliases
26+
27+
return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
28+
if req.Body != nil {
29+
bodyBytes, err := io.ReadAll(req.Body)
30+
31+
if err != nil {
32+
log.Error("Could not read Body: ", err.Error())
33+
http.Error(w, "Internal Error", http.StatusInternalServerError)
34+
return
35+
}
36+
37+
req.Body.Close()
38+
39+
var modifiedBodyData map[string]interface{}
40+
41+
err = json.Unmarshal(bodyBytes, &modifiedBodyData)
42+
43+
if err != nil {
44+
log.Error("Could not decode Body: ", err.Error())
45+
http.Error(w, "Internal Error", http.StatusInternalServerError)
46+
return
47+
}
48+
49+
content, ok := modifiedBodyData["message"]
50+
51+
if !ok || content == "" {
52+
best := 0
53+
54+
for _, alias := range messageAliases {
55+
aliasKey := alias.Alias
56+
priority := alias.Priority
57+
58+
value, ok := modifiedBodyData[aliasKey]
59+
60+
if ok && value != "" && priority > best {
61+
content = modifiedBodyData[aliasKey]
62+
}
63+
64+
modifiedBodyData[aliasKey] = nil
65+
}
66+
67+
modifiedBodyData["message"] = content
68+
69+
bodyBytes, err = json.Marshal(modifiedBodyData)
70+
71+
if err != nil {
72+
log.Error("Could not encode Body: ", err.Error())
73+
http.Error(w, "Internal Error", http.StatusInternalServerError)
74+
return
75+
}
76+
}
77+
78+
modifiedBody := string(bodyBytes)
79+
80+
req.Body = io.NopCloser(bytes.NewReader(bodyBytes))
81+
82+
req.ContentLength = int64(len(modifiedBody))
83+
req.Header.Set("Content-Length", strconv.Itoa(len(modifiedBody)))
84+
}
85+
86+
next.ServeHTTP(w, req)
87+
})
88+
}

main.go

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,50 @@ func main() {
2626

2727
initHandler = proxy.Create(ENV.API_URL)
2828

29+
body_m4 := BodyMiddleware{
30+
Next: initHandler,
31+
MessageAliases: []MessageAlias{
32+
{
33+
Alias: "msg",
34+
Priority: 100,
35+
},
36+
{
37+
Alias: "content",
38+
Priority: 99,
39+
},
40+
{
41+
Alias: "description",
42+
Priority: 98,
43+
},
44+
{
45+
Alias: "text",
46+
Priority: 20,
47+
},
48+
{
49+
Alias: "body",
50+
Priority: 15,
51+
},
52+
{
53+
Alias: "summary",
54+
Priority: 10,
55+
},
56+
{
57+
Alias: "details",
58+
Priority: 9,
59+
},
60+
{
61+
Alias: "payload",
62+
Priority: 2,
63+
},
64+
{
65+
Alias: "data",
66+
Priority: 1,
67+
},
68+
},
69+
}
70+
2971
temp_m3 := TemplateMiddleware{
30-
Next: initHandler,
72+
Next: body_m4.Use(),
3173
Variables: ENV.VARIABLES,
3274
}
3375

0 commit comments

Comments
 (0)