yagm: github.com/alexandrevicenzi/yagm Index | Files | Directories

package yagm

import "github.com/alexandrevicenzi/yagm"

Package yagm implements a simple regular expression pattern match mux.

Regular Expressions patterns

YagmMux uses regular expressions to match an URL. If you're familiar with Django, the patterns match is almost the same. If you're not, take a look in the pattern match explanation below.

Remember, the declared order can affect the match. For example, if you place "^/[a-z]+" before "^/index" it will never match "^/index", as the regular expression of "^/[a-z]+" can match "/index" too.

The route "^/$" will match only

/

The route "^/files" will match

/files
/files/
/files/myfilename.txt

The route "^/index$" will match

/index

and will not match

/index/
/index/other

If you use named groups like "^/(?P<name>[a-zA-Z]+)$" you can use yagm.Param or yagm.Params function to retrieve the value ot the groups.

Example

YagmMux is very close to http.ServeMux. You can replace your actual http.ServeMux by YagmMux without any problem.

package main

import (
    "fmt"
    "net/http"

    "github.com/alexandrevicenzi/yagm"
)

func homeHandler(w http.ResponseWriter, r *http.Request) {
    fmt.Fprint(w, "Hello World!")
}

func helloHandler(w http.ResponseWriter, r *http.Request) {
    name, ok := yagm.Param(r, "name")

    if !ok {
        name = "Unknown"
    }

    fmt.Fprintf(w, "Hello %s!", name)
}

func main() {
    mux := yagm.New()
    mux.HandleFunc("^/$", homeHandler)
    mux.Handle("^/files", http.StripPrefix("/files", http.FileServer(http.Dir("./"))))
    mux.HandleFunc("^/(?P<name>[a-zA-Z]+)$", helloHandler)
    http.ListenAndServe(":8000", mux)
}

Index

Package Files

doc.go yagm.go

func Param Uses

func Param(r *http.Request, name string) (string, bool)

Param return the value of a route variable.

func Params Uses

func Params(r *http.Request) (map[string]string, bool)

Params returns the route variables for the current request, if any.

type YagmMux Uses

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

YagmMux is an HTTP request multiplexer. It matches the URL of each incoming request against a list of registered patterns and calls the handler for the pattern that matches the URL.

func New Uses

func New() *YagmMux

New allocates and returns a new YagmMux.

func (*YagmMux) Handle Uses

func (mux *YagmMux) Handle(pattern string, handler http.Handler)

Handle registers the handler for the given pattern. If a handler already exists for pattern, Handle panics. If a pattern isn't a valid regular expression, Handle panics.

func (*YagmMux) HandleFunc Uses

func (mux *YagmMux) HandleFunc(pattern string, handler func(http.ResponseWriter, *http.Request))

HandleFunc registers the handler function for the given pattern.

func (*YagmMux) ServeHTTP Uses

func (mux *YagmMux) ServeHTTP(w http.ResponseWriter, r *http.Request)

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

Directories

PathSynopsis
example

Package yagm imports 3 packages (graph) and is imported by 2 packages. Updated 2016-10-04. Refresh now. Tools for package owners.