-
Notifications
You must be signed in to change notification settings - Fork 0
/
webhook.go
106 lines (102 loc) · 3.14 KB
/
webhook.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
package gdnotify
import (
"io"
"net/http"
"net/http/httputil"
"net/url"
"strings"
logx "github.com/mashiike/go-logx"
)
func (app *App) ServeHTTP(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
channelID := r.Header.Get("X-Goog-Channel-Id")
state := r.Header.Get("X-Goog-Resource-State")
userAgent := r.Header.Get("User-Agent")
resourceID := r.Header.Get("X-Goog-Resource-Id")
logx.Printf(ctx, "[info] method:%s uri:%s user_agent:%s channel_id:%s resource_id:%s resource_state:%s message_number:%s forwarded_for:%s channel_expiration:%s",
coalesce(r.Method, "-"),
coalesce(r.URL.String(), "-"),
url.QueryEscape(coalesce(userAgent, "-")),
coalesce(channelID, "-"),
coalesce(resourceID, "-"),
coalesce(state, "-"),
coalesce(r.Header.Get("X-Goog-Message-Number"), "-"),
coalesce(r.Header.Get("X-Forwarded-For"), "-"),
coalesce(r.Header.Get("X-Goog-Channel-Expiration"), "-"),
)
defer r.Body.Close()
if d, err := httputil.DumpRequest(r, true); err == nil {
logx.Println(ctx, "[debug] receive request\n", string(d))
}
if !strings.HasPrefix(userAgent, "APIs-Google;") {
logx.Printf(ctx, "[warn] user-agent unexpected return 404: `%s`", userAgent)
w.WriteHeader(http.StatusNotFound)
io.WriteString(w, http.StatusText(http.StatusNotFound))
return
}
if state == "sync" {
logx.Printf(ctx, "[info] sync accepted channel_id:%s resource_id:%s",
coalesce(channelID, "-"),
coalesce(resourceID, "-"),
)
w.WriteHeader(http.StatusOK)
io.WriteString(w, http.StatusText(http.StatusOK))
return
}
if state != "change" {
logx.Printf(ctx, "[warn] unknown state:%s channel_id:%s resource_id:%s",
coalesce(state, "-"),
coalesce(channelID, "-"),
coalesce(resourceID, "-"),
)
w.WriteHeader(http.StatusOK)
io.WriteString(w, http.StatusText(http.StatusOK))
return
}
logx.Printf(ctx, "[info] change accepted channel_id:%s resource_id:%s",
coalesce(channelID, "-"),
coalesce(resourceID, "-"),
)
changes, item, err := app.ChangesList(ctx, channelID)
if err != nil {
logx.Printf(ctx, "[error] get changes list failed channel_id:%s resource_id:%s err:%s",
coalesce(channelID, "-"),
coalesce(resourceID, "-"),
err.Error(),
)
w.WriteHeader(http.StatusInternalServerError)
io.WriteString(w, http.StatusText(http.StatusInternalServerError))
return
}
if len(changes) > 0 {
logx.Printf(ctx, "[debug] send changes channel_id:%s resource_id:%s",
coalesce(channelID, "-"),
coalesce(resourceID, "-"),
)
if err := app.SendNotification(ctx, item, changes); err != nil {
logx.Printf(ctx, "[error] send changes failed channel_id:%s resource_id:%s err:%s",
coalesce(channelID, "-"),
coalesce(resourceID, "-"),
err.Error(),
)
w.WriteHeader(http.StatusInternalServerError)
io.WriteString(w, http.StatusText(http.StatusInternalServerError))
return
}
} else {
logx.Printf(ctx, "[debug] no changes channel_id:%s resource_id:%s",
coalesce(channelID, "-"),
coalesce(resourceID, "-"),
)
}
w.WriteHeader(http.StatusOK)
io.WriteString(w, http.StatusText(http.StatusOK))
}
func coalesce(strs ...string) string {
for _, str := range strs {
if str != "" {
return str
}
}
return ""
}