This library provides a way to manage and chain HTTP middlewares using a hash table with separate chaining for Go's net/http
package. It allows you to easily add, retrieve, and chain middlewares for different routes in a thread-safe manner.
- Efficient and thread-safe middleware management.
- Supports chaining of multiple middlewares for different routes.
- Easy to integrate with existing Go
net/http
applications.
To install the library, you can use go get
:
go get github.com/0x0000F1/chainware
Importing the Package
import (
"github.com/0x0000F1/chainware"
"net/http"
)
Here's a complete example demonstrating how to use the middleware chaining library:
package main
import (
"fmt"
middleware "github.com/0x0000F1/chainware"
"net/http"
)
func main() {
// Create a new middleware table
mt := middleware.NewMiddlewareTable()
// Define some example middlewares
loggingMiddleware := func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fmt.Println("Logging middleware")
next.ServeHTTP(w, r)
})
}
authMiddleware := func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fmt.Println("Auth middleware")
next.ServeHTTP(w, r)
})
}
// Add middlewares to the table for a specific route
mt.AddMiddleware("/example", loggingMiddleware)
mt.AddMiddleware("/example", authMiddleware)
// Define the final handler
finalHandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintln(w, "Hello, World!")
})
// Create the server
http.HandleFunc("/example", func(w http.ResponseWriter, r *http.Request) {
chain := mt.GetChain("/example")
if chain != nil {
handler := chain.ChainMiddlewares(finalHandler)
handler.ServeHTTP(w, r)
} else {
finalHandler.ServeHTTP(w, r)
}
})
http.ListenAndServe(":8080", nil)
}
- Middleware Type: A Middleware is a function that wraps an
http.Handler
and returns a newhttp.Handler
. - Chain: A Chain represents a sequence of middlewares. It supports adding middlewares and chaining them to create a final
http.Handler
. - MiddlewareTable: A MiddlewareTable manages multiple Chain instances, each associated with a different key (e.g., route path).
- Adding Middlewares: Use
AddMiddleware
to add a middleware to a chain associated with a specific key. - Chaining Middlewares: Use
ChainMiddlewares
to wrap the finalhttp.Handler
with the middlewares in the chain.
pkg/chain.go
-
Middleware: A type definition for middleware functions.
-
Chain: A struct representing a chain of middlewares.
NewChain(capacity int) *Chain
: Creates a newChain
with a given initial capacity.AddMiddleware(m Middleware)
: Adds a middleware to the chain.GetMiddlewares() []Middleware
: Returns the list of middlewares in the chain.ChainMiddlewares(finalHandler http.Handler) http.Handler
: Chains the middlewares and returns the finalhttp.Handler
.
-
MiddlewareTable: A struct that holds multiple chains of middlewares.
NewMiddlewareTable() *MiddlewareTable
: Creates a newMiddlewareTable
.AddMiddleware(key string, m Middleware)
: Adds a middleware to the chain associated with the given key.GetChain(key string) *Chain
: Returns the chain of middlewares associated with the given key.
Contributions are welcome! Please feel free to submit a pull request or open an issue on GitHub.
This project is licensed under the MIT License.