hooks

package module
v0.0.0-...-835eaf4 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 6, 2019 License: MIT Imports: 3 Imported by: 0

README

hooks

Go Doc

hooks is a simple HTTP event dispatch handler.

GitHub webhook server

package main

import (
	"fmt"
	"log"
	"net/http"

	"github.com/hatajoe/hooks"
	"github.com/hatajoe/hooks/github"
)

func main() {
	dispatcher := hooks.NewDispatcher(&github.EventParser{})

	verifier := github.NewVerifyMiddleware("GitHub webhook secret token")

	dispatcher.On("push", verifier.Verify(func(w http.ResponseWriter, r *http.Request) {
		fmt.Println("push event detected")
	}))

	if err := dispatcher.Listen("/webhooks", ":3000"); err != nil {
		log.Fatal(err)
	}
}

Slack event API server

package main

import (
	"fmt"
	"log"
	"net/http"

	"github.com/hatajoe/hooks"
	"github.com/hatajoe/hooks/slack"
)

func main() {
	dispatcher := hooks.NewDispatcher(&slack.EventParser{
		ChallengeEventType: "challenge",
		VerifyToken:        true,
		VerificationToken:  "Slack event API secret token",
	})

	dispatcher.On("challenge", slack.ChallengeHandler)
	dispatcher.On("app_mention", func(w http.ResponseWriter, r *http.Request) {
		fmt.Println("app_mention event detected")
	})

	if err := dispatcher.Listen("/webhooks", ":3000"); err != nil {
		log.Fatal(err)
	}
}

Bot using hooks example

https://github.com/hatajoe/minibot

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Dispatcher

type Dispatcher struct {
	// contains filtered or unexported fields
}

Dispatcher is HTTP server that handles the event of http.Request

func NewDispatcher

func NewDispatcher(p EventParser) *Dispatcher

NewDispatcher returns the event Dispatcher object The argument `p` is that parses event string from *http.Request

func (*Dispatcher) Listen

func (d *Dispatcher) Listen(pattern, addr string) error

Listen starts HTTP server that handling the registered event The first argument `pattern` is the path of the hooks URI (e.g, "/webhooks") The second argument `addr` is a listen address port (e.g, "localhost:3000", ":3000")

func (*Dispatcher) On

func (d *Dispatcher) On(event string, handler http.HandlerFunc)

On adds a handler corresponding the specific event string

func (*Dispatcher) ServeHTTP

func (d *Dispatcher) ServeHTTP(w http.ResponseWriter, r *http.Request)

implements http.Handler

type EventParser

type EventParser interface {
	// Parse parses the event name from http.Request
	Parse(r *http.Request) (string, error)
}

EventParser is the event parser interface of http.Request

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL