Skip to content

pablor21/eventrouter

Repository files navigation

GoDoc GitHub release GitHub license

EventRouter

EventRouter is a simple event routing library for Go. It allows you to bind handlers to routes and hooks to events, making it easy to manage and handle events in your application.

Installation

To install EventRouter, use go get:

go get github.com/pablor21/eventrouter

Usage

Creating an EventRouter

To create a new EventRouter, use the NewEventRouter function:

router := eventrouter.NewEventRouter()

Binding Handlers

You can bind handlers to routes using the Bind method:

handler := eventrouter.NewHandler("/test", func(e eventrouter.IEvent) error {
    return nil
})
router.Bind(handler)

Unbinding Handlers

To unbind a handler from a route, use the Unbind method:

router.Unbind(handler)

Binding Hooks

You can bind hooks to handlers using the BindHook method:

hook:= eventrouter.NewHook(func(next eventrouter.HandlerFunc) eventrouter.HandlerFunc {
		return func(e eventrouter.IEvent) error {
			println("pre run called")
			err := next(e)
			if err != nil {
				return err
			}
			println("hook called")
			return nil
		}
	})
handler.BindHook(hook)

// you can also hook to the whole router
router.BindHook(hook)

Unbinding Hooks

To unbind a hook from an event, use the UnbindHook method:

router.UnbindHook(hook) 

Handling Events

To handle an event, use the Handle method:

e := eventrouter.NewEvent("/test.1")
router.Handle(e)

Testing

To run the tests for this project, use the go test command:

go test ./... 

License

This project is licensed under the MIT License. See the LICENSE file for details.

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages