-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathminify.go
49 lines (39 loc) · 922 Bytes
/
minify.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package router
import (
"encoding/json"
"github.com/oklog/ulid/v2"
"github.com/tdewolff/minify/v2"
jsonMIME "github.com/tdewolff/minify/v2/json"
"github.com/mirror520/pubsub-forwarder/pubsub"
)
type MIME string
const (
JSON MIME = "application/json"
XML MIME = "application/xml"
)
func (m MIME) Type() string {
return string(m)
}
func MinifyMiddleware(mime ...MIME) pubsub.MessageMiddleware {
m := minify.New()
if len(mime) == 0 {
m.AddFunc(JSON.Type(), jsonMIME.Minify)
} else {
for _, t := range mime {
switch t {
case JSON:
m.AddFunc(JSON.Type(), jsonMIME.Minify)
}
}
}
return func(next pubsub.MessageHandler) pubsub.MessageHandler {
return func(topic string, payload json.RawMessage, id ulid.ULID) {
minifiedPayload, err := m.Bytes("application/json", payload)
if err != nil {
next(topic, payload, id)
return
}
next(topic, minifiedPayload, id)
}
}
}