mux

package module
v0.0.3 Latest Latest
Warning

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

Go to latest
Published: Feb 1, 2021 License: MIT Imports: 6 Imported by: 10

README

mux

PkgGoDev Build Status codecov Go Report Card LICENSE

Package mux implements an HTTP request multiplexer.

Features

  • Middleware
  • Group
  • Path matching and routing
  • Fully compatible with the http.HandlerFunc
  • Not found
  • Recovery
  • HTTP Handler

Get started

Install
go get github.com/hslam/mux
Import
import "github.com/hslam/mux"
Usage
Simple Example
package main

import (
	"github.com/hslam/mux"
	"log"
	"net/http"
)

func main() {
	m := mux.New()
	m.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
		w.Write([]byte("Hello World"))
	})
	log.Fatal(http.ListenAndServe(":8080", m))
}
Example
package main

import (
	"fmt"
	"github.com/hslam/mux"
	"log"
	"net/http"
)

func main() {
	m := mux.New()
	m.Recovery(mux.Recovery)
	m.NotFound(func(w http.ResponseWriter, r *http.Request) {
		http.Error(w, "Not Found : "+r.URL.String(), http.StatusNotFound)
	})
	m.Use(func(w http.ResponseWriter, r *http.Request) {
		fmt.Printf("Host:%s Path:%s Method:%s\n", r.Host, r.URL.Path, r.Method)
	})
	m.HandleFunc("/hello", func(w http.ResponseWriter, r *http.Request) {
		w.Write([]byte(fmt.Sprintf("hello world Method:%s\n", r.Method)))
	}).All()
	m.HandleFunc("/hello/:key/world/:value", func(w http.ResponseWriter, r *http.Request) {
		params := m.Params(r)
		w.Write([]byte(fmt.Sprintf("hello key:%s value:%s\n", params["key"], params["value"])))
	}).GET().POST().PUT().DELETE()
	m.Group("/group", func(m *mux.Mux) {
		m.HandleFunc("/foo/:id", func(w http.ResponseWriter, r *http.Request) {
			params := m.Params(r)
			w.Write([]byte(fmt.Sprintf("group/foo id:%s\n", params["id"])))
		}).GET()
		m.HandleFunc("/bar/:id", func(w http.ResponseWriter, r *http.Request) {
			params := m.Params(r)
			w.Write([]byte(fmt.Sprintf("group/bar id:%s\n", params["id"])))
		}).GET()
	})
	log.Fatal(http.ListenAndServe(":8080", m))
}

curl -XGET http://localhost:8080/favicon.ico

Not Found : /favicon.ico

curl -XGET http://localhost:8080/hello

hello world Method:GET

curl -XPOST http://localhost:8080/hello

hello world Method:POST

curl -I http://localhost:8080/hello

HTTP/1.1 200 OK
Date: Tue, 01 Oct 2019 20:28:42 GMT
Content-Length: 24
Content-Type: text/plain; charset=utf-8

curl -XPATCH http://localhost:8080/hello

hello world Method:PATCH

curl -XOPTIONS http://localhost:8080/hello

hello world Method:OPTIONS

curl http://localhost:8080/hello/123/world/456

hello key:123 value:456

curl http://localhost:8080/group/foo/1

group/foo id:1

curl http://localhost:8080/group/bar/2

group/bar id:2
License

This package is licensed under a MIT license (Copyright (c) 2019 Meng Huang)

Author

mux was written by Meng Huang.

Documentation

Overview

Package mux implements an HTTP request multiplexer.

Index

Constants

This section is empty.

Variables

View Source
var ErrGroupExisted = errors.New("Group Existed")

ErrGroupExisted is the error returned by Group when registers a existed group.

View Source
var ErrParamsKeyEmpty = errors.New("Params key must be not empty")

ErrParamsKeyEmpty is the error returned by HandleFunc when the params key is empty.

View Source
var RecoveryContextKey = &contextKey{"recovery"}

RecoveryContextKey is a context key.

Functions

func Recovery added in v0.0.2

func Recovery(w http.ResponseWriter, r *http.Request)

Recovery returns a recovery handler function that recovers from any panics and writes a 500 status code.

Types

type Entry

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

Entry represents an HTTP HandlerFunc entry.

func (*Entry) All

func (entry *Entry) All()

All adds all HTTP method to the entry.

func (*Entry) CONNECT

func (entry *Entry) CONNECT() *Entry

CONNECT adds a CONNECT HTTP method to the entry.

func (*Entry) DELETE

func (entry *Entry) DELETE() *Entry

DELETE adds a DELETE HTTP method to the entry.

func (*Entry) GET

func (entry *Entry) GET() *Entry

GET adds a GET HTTP method to the entry.

func (*Entry) HEAD

func (entry *Entry) HEAD() *Entry

HEAD adds a HEAD HTTP method to the entry.

func (*Entry) OPTIONS

func (entry *Entry) OPTIONS() *Entry

OPTIONS adds a OPTIONS HTTP method to the entry.

func (*Entry) PATCH

func (entry *Entry) PATCH() *Entry

PATCH adds a PATCH HTTP method to the entry.

func (*Entry) POST

func (entry *Entry) POST() *Entry

POST adds a POST HTTP method to the entry.

func (*Entry) PUT

func (entry *Entry) PUT() *Entry

PUT adds a PUT HTTP method to the entry.

func (*Entry) TRACE

func (entry *Entry) TRACE() *Entry

TRACE adds a TRACE HTTP method to the entry.

type Mux

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

Mux is an HTTP request multiplexer.

func New

func New() *Mux

New returns a new Mux.

func (*Mux) Group

func (m *Mux) Group(group string, f func(m *Mux))

Group registers a group with the given pattern to the Mux.

func (*Mux) Handle

func (m *Mux) Handle(pattern string, handler http.Handler) *Entry

Handle registers a handler with the given pattern to the Mux.

func (*Mux) HandleFunc

func (m *Mux) HandleFunc(pattern string, handler func(http.ResponseWriter, *http.Request)) *Entry

HandleFunc registers a handler function with the given pattern to the Mux.

func (*Mux) NotFound

func (m *Mux) NotFound(handler http.HandlerFunc)

NotFound registers a not found handler function to the Mux.

func (*Mux) Params

func (m *Mux) Params(r *http.Request) map[string]string

Params returns http request params.

func (*Mux) Recovery added in v0.0.2

func (m *Mux) Recovery(handler http.HandlerFunc)

Recovery registers a recovery handler function to the Mux.

func (*Mux) ServeHTTP

func (m *Mux) ServeHTTP(w http.ResponseWriter, r *http.Request)

ServeHTTP dispatches the request to the handler whose pattern most closely matches the request URL.

func (*Mux) Use

func (m *Mux) Use(handler http.HandlerFunc)

Use uses middleware.

Jump to

Keyboard shortcuts

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