Package SeaMUX provides a lightweight and flexible HTTP router for Go. It is designed to be simple to use, yet powerful enough to handle complex routing requirements.
Creates and returns a new RouteMux.
mux := seamux.New()Adds a new route to the RouteMux.
mux.HandleFunc("/hello/:name", func(w http.ResponseWriter, r *http.Request) {
name := r.URL.Query().Get("name")
fmt.Fprintf(w, "Hello, %s!", name)
})- pattern : the URL pattern to match. Any sections that start with : are considered parameters and are replaced with a regular expression.
- handler : the function to execute when the pattern is matched.
- Returns a pointer to the route object that was created.
Adds a middleware function to the RouteMux.
mux.Middleware(func(w http.ResponseWriter, r *http.Request) {
// Do something before the request is handled by the route's handler.
})- filter : the middleware function to add.
Adds a new route to the RouteMux using the specified pattern and handler. This method is called by HandleFunc.
route := mux.AddRoute("/hello/:name", func(w http.ResponseWriter, r *http.Request) {
name := r.URL.Query().Get("name")
fmt.Fprintf(w, "Hello, %s!", name)
})- pattern : the URL pattern to match. Any sections that start with : are considered parameters and are replaced with a regular expression.
- handler : the function to execute when the pattern is matched.
- Returns a pointer to the route object that was created.
Handles incoming HTTP requests and routes them to the appropriate handler.
http.ListenAndServe(":8080", mux)- w : the http.ResponseWriter to use for the response.
- r : the incoming http.Request to route.
type route struct {
methods []string
regex *regexp.Regexp
params map[int]string
handler http.HandlerFunc
}The route type represents an HTTP route, consisting of a set of HTTP methods, a regular expression for matching the URL pattern, a set of named parameters extracted from the URL pattern, and an HTTP request handler function.
type RouteMux struct {
routes []*route
middleware []http.HandlerFunc
}The RouteMux type is the main HTTP multiplexer type. It stores a list of routes and middleware functions.
func New() *RouteMuxNew returns a new RouteMux.
func (m *RouteMux) HandleFuncHandleFunc mimics the HandleFunc in the standard http library
func (m *RouteMux) HandleFunc(pattern string, handler http.HandlerFunc) *routeHandleFunc registers an HTTP request handler function for the given URL pattern. It returns a route that can be used to further configure the route.
func (r *route) Methods(methods ...string)Methods sets the HTTP methods that this route should match.
func (m *RouteMux) Middleware(filter http.HandlerFunc)Middleware registers an HTTP middleware function to be executed for all routes.
func (m *RouteMux) AddRoute(pattern string, handler http.HandlerFunc) *routeAddRoute registers a new HTTP route for the given URL pattern and request handler function. It returns a route that can be used to further configure the route.
func (m *RouteMux) ServeHTTP(w http.ResponseWriter, r *http.Request)ServeHTTP is the HTTP request handler function for the RouteMux type. It matches incoming HTTP requests to registered routes, executes middleware functions, and invokes the appropriate request handler function.
func containsMethod(s []string, str string) boolcontainsMethod returns true if the given HTTP method is in the list of allowed methods for a route, false otherwise.