github.com/go-playground/webhooks/v6@v6.3.0/_examples/multiple-handlers/main.go (about)

     1  package main
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"net/http"
     7  
     8  	"github.com/go-playground/webhooks/v6/github"
     9  )
    10  
    11  const (
    12  	path1 = "/webhooks1"
    13  	path2 = "/webhooks2"
    14  )
    15  
    16  func main() {
    17  	hook1, _ := github.New(github.Options.Secret("MyGitHubSuperSecretSecrect...?"))
    18  	hook2, _ := github.New(github.Options.Secret("MyGitHubSuperSecretSecrect2...?"))
    19  
    20  	http.HandleFunc(path1, func(w http.ResponseWriter, r *http.Request) {
    21  		payload, err := hook1.Parse(r, github.ReleaseEvent, github.PullRequestEvent)
    22  		if err != nil {
    23  			if err == github.ErrEventNotFound {
    24  				// ok event wasn;t one of the ones asked to be parsed
    25  			}
    26  		}
    27  		switch payload.(type) {
    28  
    29  		case github.ReleasePayload:
    30  			release := payload.(github.ReleasePayload)
    31  			// Do whatever you want from here...
    32  			fmt.Printf("%+v", release)
    33  
    34  		case github.PullRequestPayload:
    35  			pullRequest := payload.(github.PullRequestPayload)
    36  			// Do whatever you want from here...
    37  			fmt.Printf("%+v", pullRequest)
    38  		}
    39  	})
    40  
    41  	http.HandleFunc(path2, func(w http.ResponseWriter, r *http.Request) {
    42  		payload, err := hook2.Parse(r, github.ReleaseEvent, github.PullRequestEvent)
    43  		if err != nil {
    44  			if err == github.ErrEventNotFound {
    45  				// ok event wasn;t one of the ones asked to be parsed
    46  			}
    47  		}
    48  		switch payload.(type) {
    49  
    50  		case github.ReleasePayload:
    51  			release := payload.(github.ReleasePayload)
    52  			// Do whatever you want from here...
    53  			fmt.Printf("%+v", release)
    54  
    55  		case github.PullRequestPayload:
    56  			pullRequest := payload.(github.PullRequestPayload)
    57  			// Do whatever you want from here...
    58  			fmt.Printf("%+v", pullRequest)
    59  		}
    60  	})
    61  	http.ListenAndServe(":3000", nil)
    62  }