mux

package module
v0.0.5 Latest Latest
Warning

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

Go to latest
Published: Sep 22, 2019 License: MIT Imports: 4 Imported by: 0

README

Simple HTTP muxer with greedy params

This is a simple HTTP muxer. It works like pat with important difference - all url params greedy, see above.

Why another router???

I just need a simple router that can work on nested routes /head/elem1/elem2/.../elemN/tail. That's all!

Routers like pat, chi, echo are awesome, but they cannot cover this particular use case.

Match example

PatternPathMatch?Params
/simple/simple/testYes{}
/simple/s1mp1eNo{}
/one/:param1/three/one/two/threeYes{param1:"two"}
/one/:param1/five/one/two/three/four/fiveYes{param1:"two/three/four"}
/1/:param1/5/:param2/10/:param3/1/2/3/4/5/6/7/8/9/10/11/12Yes{param1:"2/3/4", param2:"6/7/8/9", param3:"11/12"}

Params

func(w http.ResponseWriter, r *http.Request) {
	params := mux.GetParams(r)
...

Example

package main

import (
	"fmt"
	"log"
	"net/http"

	"github.com/neonxp/mux"
)

func main() {
	m := mux.New()

	middleware := func(next http.Handler) http.Handler {
		return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
			w.Header().Set("X-Middleware", "hello!")
			next.ServeHTTP(w, r)
		})
	}

	m.Get("/head/:param1/middle/:param2/tail", func(writer http.ResponseWriter, reader *http.Request) {
		params := mux.GetParams(reader)
		mux.Error(mux.Plain(fmt.Sprintf("param1=%s, param2=%s", params["param1"], params["param2"]), writer), writer)
	}, middleware)

	http.Handle("/", m)
	err := http.ListenAndServe(":8080", nil)
	if err != nil {
		log.Fatal(err)
	}
}

Documentation

Overview

Package mux implements simple HTTP with greedy patterns

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Error added in v0.0.5

func Error(err error, w http.ResponseWriter)

Error renders error (if present) to ResponseWriter

func GetParams added in v0.0.3

func GetParams(r *http.Request) map[string]string

GetParams extracts route parameters from request

func JSON added in v0.0.5

func JSON(v interface{}, w http.ResponseWriter) error

JSON renders object to ResponseWriter as JSON

func Plain added in v0.0.5

func Plain(text string, w http.ResponseWriter) error

Plain renders text to ResponseWriter

Types

type HTTPError added in v0.0.5

type HTTPError struct {
	Code int
	// contains filtered or unexported fields
}

type Mux

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

Mux is a simple HTTP muxer

func New

func New() *Mux

New returns new Mux instance

func (*Mux) Add

func (m *Mux) Add(method string, pattern string, handler http.Handler, middlewares ...func(next http.Handler) http.Handler) *Mux

Add registers handler for specified method and matched pattern

func (*Mux) Del

func (m *Mux) Del(pattern string, handler http.HandlerFunc, middlewares ...func(next http.Handler) http.Handler) *Mux

Del registers handler for DELETE method

func (*Mux) Get

func (m *Mux) Get(pattern string, handler http.HandlerFunc, middlewares ...func(next http.Handler) http.Handler) *Mux

Get registers handler for GET method

func (*Mux) Head

func (m *Mux) Head(pattern string, handler http.HandlerFunc, middlewares ...func(next http.Handler) http.Handler) *Mux

Head registers handler for HEAD method

func (*Mux) Options

func (m *Mux) Options(pattern string, handler http.HandlerFunc, middlewares ...func(next http.Handler) http.Handler) *Mux

Options registers handler for OPTIONS method

func (*Mux) Patch

func (m *Mux) Patch(pattern string, handler http.HandlerFunc, middlewares ...func(next http.Handler) http.Handler) *Mux

Patch registers handler for PATCH method

func (*Mux) Post

func (m *Mux) Post(pattern string, handler http.HandlerFunc, middlewares ...func(next http.Handler) http.Handler) *Mux

Post registers handler for POST method

func (*Mux) Put

func (m *Mux) Put(pattern string, handler http.HandlerFunc, middlewares ...func(next http.Handler) http.Handler) *Mux

Put registers handler for PUT method

func (*Mux) ServeHTTP

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

Jump to

Keyboard shortcuts

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