Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
pablor21 committed Dec 3, 2024
0 parents commit 221019a
Show file tree
Hide file tree
Showing 14 changed files with 622 additions and 0 deletions.
Empty file added .gitignore
Empty file.
15 changes: 15 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Launch Package",
"type": "go",
"request": "launch",
"mode": "auto",
"program": "${fileDirname}"
}
]
}
21 changes: 21 additions & 0 deletions LICENSE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2024 Pablo Ramirez <[email protected]>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
91 changes: 91 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
# 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:

```sh
go get github.com/pablor21/eventrouter
```

## Usage

### Creating an EventRouter

To create a new EventRouter, use the NewEventRouter function:

```go
router := eventrouter.NewEventRouter()
```

### Binding Handlers

You can bind handlers to routes using the Bind method:

```go
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:

```go
router.Unbind(handler)
```

### Binding Hooks

You can bind hooks to handlers using the BindHook method:

```go
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:

```go
router.UnbindHook(hook)
```

### Handling Events

To handle an event, use the Handle method:

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

## Testing

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

```sh
go test ./...
```

## License

This project is licensed under the MIT License. See the LICENSE file for details.
22 changes: 22 additions & 0 deletions event.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package eventrouter

type IEvent interface {
Name() string
}

type Event struct {
name string
}

func NewEvent(name string) *Event {
return &Event{
name: name,
}
}

func (e *Event) Name() string {
if e.name == "" {
e.name = RandomString(10)
}
return e.name
}
28 changes: 28 additions & 0 deletions event_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package eventrouter_test

import (
"testing"

"github.com/pablor21/eventrouter"
)

func TestNewEvent(t *testing.T) {
event := eventrouter.NewEvent("testEvent")
if event == nil {
t.Fatal("Expected new event to be created")
}
if event.Name() != "testEvent" {
t.Fatalf("Expected event name to be 'testEvent', got %s", event.Name())
}
}

func TestEventName(t *testing.T) {
event := eventrouter.NewEvent("")
name := event.Name()
if name == "" {
t.Fatal("Expected event name to be generated, got empty string")
}
if len(name) != 10 {
t.Fatalf("Expected event name length to be 10, got %d", len(name))
}
}
62 changes: 62 additions & 0 deletions examples/basic/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package main

import (
"github.com/pablor21/eventrouter"
)

type testHandler struct {
eventrouter.Handler
}

func (h *testHandler) Route() string {
return "/test.*"
}

func (h *testHandler) Handle(e eventrouter.IEvent) error {
println("handler called")
return nil
}

type testHook struct {
name string
}

func newTestHook(name string) *testHook {
return &testHook{name: name}
}

func (h *testHook) Handle(next eventrouter.HandlerFunc) eventrouter.HandlerFunc {
return func(e eventrouter.IEvent) error {
println("pre run called", h.name)
err := next(e)
if err != nil {
return err
}
println("hook called", h.name)
return nil
}
}

func main() {
eb := eventrouter.NewEventRouter()

h := &testHandler{}
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
}
})
h.BindHook(newTestHook("test"), hook)

eb.Bind(h)

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

eb.Handle(e)
}
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module github.com/pablor21/eventrouter

go 1.23.2
Empty file added go.sum
Empty file.
82 changes: 82 additions & 0 deletions handler.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package eventrouter

type HandlerFunc func(e IEvent) error
type HookFunc func(next HandlerFunc) HandlerFunc

type IHook interface {
Handle(next HandlerFunc) HandlerFunc
}

type Hook struct {
fn HookFunc
}

func NewHook(fn HookFunc) *Hook {
return &Hook{
fn: fn,
}
}

func (h *Hook) Handle(next HandlerFunc) HandlerFunc {
return h.fn(next)
}

type IHandler interface {
Route() string
Handle(e IEvent) error
Hooks() []IHook
BindHook(hook ...IHook) IHandler
UnbindHook(hook IHook) IHandler
BindHookFunc(hook ...HookFunc) IHandler
}

type Handler struct {
route string
fn HandlerFunc
hooks []IHook
}

func NewHandler(route string, fn HandlerFunc, hooks ...IHook) *Handler {
return &Handler{
route: route,
fn: fn,
hooks: hooks,
}
}

func (h *Handler) Route() string {
return h.route
}

func (h *Handler) Handle(e IEvent) error {
if h.fn == nil {
return nil
}
return h.fn(e)
}

func (h *Handler) Hooks() []IHook {
return h.hooks
}

func (h *Handler) BindHook(hook ...IHook) IHandler {
h.hooks = append(h.hooks, hook...)
return h
}

func (h *Handler) UnbindHook(hook IHook) IHandler {
for i, ho := range h.hooks {
if ho == hook {
h.hooks = append(h.hooks[:i], h.hooks[i+1:]...)
return h
}
}
return h
}

func (h *Handler) BindHookFunc(hook ...HookFunc) IHandler {
for _, fn := range hook {
h.hooks = append(h.hooks, NewHook(fn))
}
return h
}
Loading

0 comments on commit 221019a

Please sign in to comment.