mux

package module
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Sep 18, 2019 License: MIT Imports: 3 Imported by: 0

README

Simple regexp based HTTP muxer

This is a simple HTTP muxer. All path patterns must be a valid go regexp (https://golang.org/pkg/regexp/syntax/).

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.

Params

If one or more groups is present in the pattern (i.e. /book/([a-z]+)/(\d+)) you can get the corresponding parameters from the context:

func(w http.ResponseWriter, r *http.Request) {
	params := r.Context().Value("params").([]string)
...

Here params[0] contains substring that matches to all regexp, params[1] is the first group, params[N] is the N group. Thus, for regexp /book/([a-z]+)/(\d+) and path /shop/book/golang/123, the parameters will contains: [0] => "/book/golang/123", [1] => "golang", [2] => "123"

Match example

PatternPathParams
^/simple/simple/test[0]=>"/simple"
^/one/(.+?)/three$/one/two/three[0]=>"/one/two/three", [1]=>"two"
^/one/(.+?)/four$/one/two/three/four[0]=>"/one/two/three/four", [1]=>"two/three"

Example

package main

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

	"github.com/neonxp/mux"
)

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

	m.Get("/hello/(.+?)/(.+?)/world", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		params := r.Context().Value("params").([]string)
		w.Write([]byte(fmt.Sprintf("First param: %s, second param: %s. All path: %s", params[1], params[2], params[0])))
	}))

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

Documentation

Overview

Package mux implements simple regexp based http muxer

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Mux

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

Mux is a simple HTTP muxer. All path patterns must be valid go regexp (https://golang.org/pkg/regexp/syntax/).

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