mux

package module
v0.2.1 Latest Latest
Warning

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

Go to latest
Published: Dec 27, 2025 License: Apache-2.0 Imports: 8 Imported by: 1

README

Mux

Go Reference

a small idomatic http router and request multiplexer for go.

Overview

provides a clean, expressive way to route HTTP requests in Go applications.

  • Route matching by path, host, query, headers, methods, and schemes
  • Subrouters with shared conditions (prefix, middleware grouping)
  • Middleware support (CORS, auth, logging, etc.)
  • Static file serving and SPA-friendly routing
  • Custom 404 and 405 handlers
  • No dependencies

Prerequisites

go version 1.24

Running

Basic Example

package main

import (
	"fmt"
	"net/http"
	"io"

	"github.com/devilcove/mux"
)

func main() {
	r := mux.NewRouter(nil, middleware).NotFound(notFoundHandler).NotAllowed(notAllowedHandler)

	r.Get("/{$}", homeHandler)
	r.Get("/articles/{id}", articleHandler)

	// Serve static assets
	r.Static("/static", "static")

	r.Run(":8000")
}

func homeHandler(w http.ResponseWriter, r *http.Request) {
	fmt.Fprintln(w, "Welcome to mux-powered app!")
}

func articleHandler(w http.ResponseWriter, r *http.Request) {
	id := r.PathValue("id")
	fmt.Fprintf(w, "Article ID: %s\n", id)
}

func middleware(next http.Handler) http.Handler {
    return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
        w.Header().Set("X-tag", "true")
        next.ServeHTTP(w, r)
    })
}

func notFoundHandler(w http.ResponseWriter, _ *http.Request) {
    w.WriteHeader(http.StatusNotFound)
    io.WriteString(w, "custom not found")
}

func notAllowedHandler(w http.ResponseWriter, _ string, code int) {
    w.WriteHeader(code)
    io.WriteString(w, "Custom Method Not Allowed")
}

Documentation

Overview

Package mux is a small, idomatic http router

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Logger

func Logger(next http.Handler) http.Handler

Logger is a logging middleware that logs useragent, RemoteAddr, Method, Host, Path and response.Status to stdlib log.

Types

type Middleware

type Middleware func(http.Handler) http.Handler

Middleware defines a function that wraps an http.Handler.

type Router

type Router struct {
	*http.ServeMux
	*slog.Logger
	// contains filtered or unexported fields
}

Router provides a chain of middlewares and routes.

func DefaultRouter

func DefaultRouter() *Router

DefaultRouter creates a new Router using the default ServeMux.

func NewRouter

func NewRouter(l *slog.Logger, middleware ...Middleware) *Router

NewRouter creates a new Router with the given middleware applied.

func (*Router) All

func (router *Router) All(pattern string, handler http.HandlerFunc)

All registers the handler for all methods on given pattern.

func (*Router) CustomMethod added in v0.2.0

func (router *Router) CustomMethod(method, pattern string, handle http.HandlerFunc)

CustomMethod registers a handler for a custom method request.

func (*Router) Delete

func (router *Router) Delete(pattern string, handler http.HandlerFunc)

Delete registers the handler for delete requests on given pattern.

func (*Router) Get

func (router *Router) Get(pattern string, handler http.HandlerFunc)

Get registers the handler for get requests on given pattern.

func (*Router) Group

func (router *Router) Group(prefix string, middlewares ...Middleware) *Router

Group creates a sub-router for the given prefix and applies middleware to it.

func (*Router) NotAllowed added in v0.2.0

func (router *Router) NotAllowed(h func(http.ResponseWriter, string, int)) *Router

NotAllowed sets a custom method not allowed error.

func (*Router) NotFound added in v0.2.0

func (router *Router) NotFound(h func(http.ResponseWriter, *http.Request)) *Router

NotFound sets a custome not found handler.

func (*Router) Patch

func (router *Router) Patch(pattern string, handler http.HandlerFunc)

Patch registers the handler for patch requests on given pattern.

func (*Router) Post

func (router *Router) Post(pattern string, handler http.HandlerFunc)

Post registers the handler for post requests on given pattern.

func (*Router) Put

func (router *Router) Put(pattern string, handler http.HandlerFunc)

Put registers the handler for Put requests on given pattern.

func (*Router) Run

func (router *Router) Run(addr string)

Run starts the HTTP server and logs any error that occurs.

func (*Router) ServeFile added in v0.1.1

func (router *Router) ServeFile(pattern, file string)

ServeFile registers a ServeFile handler.

func (*Router) ServeFileFS added in v0.2.1

func (router *Router) ServeFileFS(pattern, file string, fs fs.FS)

ServeFileFS registers a ServeFileFS handler.

func (*Router) ServeHTTP

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

ServeHTTP implements the http.Handler interface.

func (*Router) Static added in v0.1.1

func (router *Router) Static(pattern, dir string)

Static registers the handle to serve static files.

func (*Router) StaticFS added in v0.2.1

func (router *Router) StaticFS(pattern string, fs fs.FS)

StaticFS registers the handle to serve static files from FS filesystem. ex. //go:embded images var content embed.FS router.StaticFS("/images/", content) .

func (*Router) Use

func (router *Router) Use(middlewares ...Middleware)

Use adds a chain of middlewares to the router.

Directories

Path Synopsis
Example programm
Example programm

Jump to

Keyboard shortcuts

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