multiplexer

package module
v1.4.1 Latest Latest
Warning

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

Go to latest
Published: Jun 15, 2024 License: BSD-3-Clause Imports: 4 Imported by: 0

README

multiplexer - A Versatile HTTP Request Router🚀

The multiplexer library provides a robust and customizable HTTP request router built on top of the standard net/http package in Go. It empowers developers to efficiently manage incoming requests, directing them to appropriate handlers based on URL patterns and methods.

Installation🛠

Prerequisites: Go 1.22 or above

  go get -u github.com/iarsham/multiplexer

Usage/Examples💡

func main() {
	mux := multiplexer.New(http.NewServeMux(), "/api")
	mux.NotFound = http.HandlerFunc(notfound)
	mux.MethodNotAllowed = http.HandlerFunc(allowed)
	dynamic := multiplexer.NewChain(logMiddleware)
	mux.Handle("GET /root", dynamic.WrapFunc(root))
	authGroup := mux.Group("/user")
	protected := dynamic.Append(authMiddleware)
	authGroup.Handle("GET /home", protected.WrapFunc(home))
	log.Fatal(http.ListenAndServe(":8000", mux))
}

Features🪜

  • Clear and Consistent Routing: Define routes using URL patterns that may include named capture groups (e.g., /users/:id).
  • Base Path Support: Configure a base path to be prepended to all route patterns, simplifying organization within nested routing scenarios.
  • Flexible Handler Registration: Register handlers using HandleFunc, Handle, or custom handler functions.
  • Customizable Not Found and Method Not Allowed Handlers: Provide tailored responses for unmatched requests or unsupported methods.
  • Sub-Routing with Grouping: Create nested routing hierarchies using the Group function, promoting better code organization.

Contributing 🤝

Contributions are always welcome!

  • Create a fork of the repository.

  • Make your changes in a separate branch.

  • Ensure your code adheres to Go's formatting and style conventions.

  • Add unit tests for your changes.

  • Submit a well-structured pull request with a clear description of your changes. ❤️‍🔥

Contributors 👨🏻‍💻👩🏼‍💻

License ⚠️

MIT

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Chain added in v0.4.0

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

Chain represents a chain of middlewares.

func NewChain added in v0.4.0

func NewChain(mws ...Middleware) Chain

NewChain creates a new Chain with the provided middlewares.

func (Chain) Append added in v0.4.0

func (c Chain) Append(mws ...Middleware) Chain

Append appends the provided middlewares to the chain.

func (Chain) Wrap added in v0.4.0

func (c Chain) Wrap(h http.Handler) http.Handler

Wrap wraps the provided http.Handler with the chain of middlewares.

func (Chain) WrapFunc added in v0.4.0

func (c Chain) WrapFunc(h http.HandlerFunc) http.Handler

WrapFunc wraps the provided http.HandlerFunc with the chain of middlewares.

type Middleware added in v0.4.0

type Middleware func(http.Handler) http.Handler

Middleware is a function that takes a http.Handler and returns a http.Handler

type Router

type Router struct {
	NotFound         http.Handler
	MethodNotAllowed http.Handler
	// contains filtered or unexported fields
}

Router represents a multiplexer that routes incoming HTTP requests.

func New

func New(mux *http.ServeMux, basePath string) *Router

New creates a new instance of the Router struct. It initializes the mux field with a new instance of http.ServeMux. Returns a pointer to the newly created Router.

func (*Router) Group added in v0.5.0

func (r *Router) Group(subPath string) *Router

Group creates a new sub-router with the given path appended to the base path of the parent router.

func (*Router) Handle added in v0.3.0

func (r *Router) Handle(pattern string, handler http.Handler)

Handle adds a new route with the given pattern and handler.

func (*Router) HandleFunc added in v0.3.0

func (r *Router) HandleFunc(pattern string, handler http.HandlerFunc)

HandleFunc adds a new route with the given pattern and handler function.

func (*Router) ServeHTTP added in v0.2.0

func (r *Router) ServeHTTP(w http.ResponseWriter, req *http.Request)

ServeHTTP implements the http.Handler interface. It calls the ServeHTTP method of the underlying http.ServeMux. It also handles the custom NotFound and MethodNotAllowed handlers.

Jump to

Keyboard shortcuts

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