-
Notifications
You must be signed in to change notification settings - Fork 152
/
guard.go
58 lines (44 loc) · 1.26 KB
/
guard.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
package server
import (
"bytes"
"github.com/ArtisanCloud/PowerLibs/v2/http/response"
logger2 "github.com/ArtisanCloud/PowerLibs/v2/logger"
"github.com/ArtisanCloud/PowerWeChat/v2/src/kernel"
"io/ioutil"
"net/http"
)
type Guard struct {
*kernel.ServerGuard
}
func NewGuard(app *kernel.ApplicationInterface) *Guard {
//config := (*app).GetContainer().GetConfig()
guard := &Guard{
kernel.NewServerGuard(app),
}
guard.OverrideShouldReturnRawResponse()
return guard
}
func (guard *Guard) VerifyURL(request *http.Request) (httpRS *response.HttpResponse, err error) {
logger := (*guard.App).GetComponent("Logger").(*logger2.Logger)
_, err = guard.Validate(request)
if err != nil {
return nil, err
}
rs := &http.Response{
Body: ioutil.NopCloser(bytes.NewBufferString(request.URL.Query().Get("echostr"))),
StatusCode: http.StatusOK,
}
httpRS = response.NewHttpResponse(http.StatusOK)
httpRS.Response = rs
logger.Info("Server response created:", "content", httpRS.GetBody())
return httpRS, err
}
// Override Validate
func (guard *Guard) OverrideShouldReturnRawResponse() {
guard.ShouldReturnRawResponse = func(request *http.Request) bool {
if request == nil || request.URL.Query().Get("echostr") == "" {
return false
}
return true
}
}