Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/fakundo/go-middleware
Middleware for Go event handlers
https://github.com/fakundo/go-middleware
event-handler golang middleware
Last synced: 30 days ago
JSON representation
Middleware for Go event handlers
- Host: GitHub
- URL: https://github.com/fakundo/go-middleware
- Owner: fakundo
- License: mit
- Created: 2021-03-10T22:18:49.000Z (almost 4 years ago)
- Default Branch: master
- Last Pushed: 2021-03-25T10:17:36.000Z (over 3 years ago)
- Last Synced: 2024-04-15T01:10:35.587Z (8 months ago)
- Topics: event-handler, golang, middleware
- Language: Go
- Homepage:
- Size: 3.91 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
## go-middleware
Middleware for Go functions.
Works perfectly with `socket.io` event handlers –
https://github.com/googollee/go-socket.io### Installation
`go get github.com/fakundo/go-middleware`
### Usage
Create middleware
```go
import (
middleware "github.com/fakundo/go-middleware"
socketio "github.com/googollee/go-socket.io"
)var requireAuth = middleware.Create(func(s socketio.Conn, next func()) {
if authorized(s) {
next()
} else {
s.emit("error", AuthError)
}
})
```Use it
```go
import socketio "github.com/googollee/go-socket.io"io, _ = socketio.NewServer(nil)
io.OnEvent("/", "some-event", requireAuth(func(s socketio.Conn) {
// some event handler code
}))io.OnEvent("/", "another-event", requireAuth(func(s socketio.Conn, arg string) {
// another event handler code
}))
```### Middleware composition
```go
import middleware "github.com/fakundo/go-middleware"...
io.OnEvent("/", "event", middleware.Use(someMiddleware, requireAuth, func(s socketio.Conn) {
// event handler code
}))
```### About middleware behaviour
- *currently* the decorated function type must be `interface` (exactly like `socket.io` event handlers)
- the origin function and middleware can have different number of arguments, middleware will only take required arguments, and the last argument is always `next`
- `next()` calls the origin function
- the origin function may return something, so `next()` will return that too### License
MIT