Golang WebHook 使用 chi router

Golang WebHook using chi router

我正在使用 chi router 在 Go 中创建一个服务,我需要为我的一个端点实现一个 WebHook。我正在使用 this library 创建我的 webHook。但是,当我尝试接收并解码时遇到问题。在他们的示例代码中,他们使用了 Gin 框架,而我必须使用 chi。我的 receivedSignature 结果为 nil 并且出现错误:interface conversion: interface {} is nil, not string .

谁能帮我解决这个问题?

这是我的发送函数:

    data := []int{1, 2, 3, 4}
    // String sent in the GoHook that helps identify actions to take with data
    resource := "int-list-example"
    // Secret string that should be common to sender and receiver
    // in order to validate the GoHook signature
    saltSecret := "0014716e-392c-4120-609e-555e295faff5"

    hook := &gohooks.GoHook{}
    hook.Create(data, resource, saltSecret)

    // Will return *http.Response and error
    resp, err := hook.Send("http://localhost:3001/")
    if err != nil {
        fmt.Println("error: ", err)
    }
    fmt.Println("resp: ", resp)

这是我的接收逻辑:

func main(){
    r := chi.NewRouter()
    r.Use(middleware.Logger)
    r.Post("/", receiveWebHook)
    http.ListenAndServe(":3001", r)
}

type MyWebhook struct {
    Resource string `json:"resource"`
    Data []int `json:"data"`
}

func receiveWebHook(w http.ResponseWriter, r *http.Request){
    var request MyWebhook
    err := json.NewDecoder(r.Body).Decode(&request)
    if err !=nil{
        fmt.Println("err: ", err)
    }
    // Shared secret with sender
    saltSecret := "0014716e-392c-4120-609e-555e295faff5"

    receivedSignature := r.Context().Value(gohooks.DefaultSignatureHeader).(string)
    // Verify validity of GoHook
    isValid := gohooks.IsGoHookValid(request, receivedSignature, saltSecret)
    // Decide what to do if GoHook is valid or not.
    if !isValid {
        fmt.Println("Not valid, receivedSignature: ", receivedSignature)
    }else{
        fmt.Println("Valid, receivedSignature: ", receivedSignature)
    }
    w.WriteHeader(200)
}

签名将在 header 中。因此,您还应该从请求的 header.

中阅读它
sig := r.Header.Get(gohooks.DefaultSignatureHeader)