mux

package module
v0.0.3 Latest Latest
Warning

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

Go to latest
Published: Sep 19, 2019 License: MIT Imports: 3 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()

	m.Get("/head/:param1/middle/:param2/tail", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		params := mux.GetParams(r)
		w.Write([]byte(fmt.Sprintf("param1=%s, param2=%s", params["param1"], params["param2"])))
	}))

	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 GetParams added in v0.0.3

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

GetParams extracts route parameters from request

Types

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)

Add registers handler for specified method and matched pattern

func (*Mux) Del

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

Del registers handler for DELETE method

func (*Mux) Get

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

Get registers handler for GET method

func (*Mux) Head

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

Head registers handler for HEAD method

func (*Mux) Options

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

Options registers handler for OPTIONS method

func (*Mux) Patch

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

Patch registers handler for PATCH method

func (*Mux) Post

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

Post registers handler for POST method

func (*Mux) Put

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

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