mux

package module
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Jul 12, 2025 License: Apache-2.0 Imports: 5 Imported by: 2

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

Prerequisites

go version 1.23

Running

Basic Example

package main

import (
	"fmt"
	"net/http"

	"github.com/devilcove/mux"
)

func main() {
	r := mux.NewRouter()

	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)
}

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
	// 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(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) 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) Patch

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

Delete 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) 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) 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