yagm

package module
Version: v0.0.0-...-3a6185e Latest Latest
Warning

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

Go to latest
Published: Oct 4, 2016 License: MIT Imports: 3 Imported by: 1

README

yagm Build Status GoDoc Go Report Card

yagm is an acronym of Yet Another Go Mux.

YagmMux uses regular expressions to match an URL. If you're familiar with Django, the patterns match is almost the same.

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

Install

go get github.com/alexandrevicenzi/yagm

Usage

You can register your handlers as in http.ServeMux.

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

You can get the value of named groups with

params, ok := yagm.Params(request)
value, ok := params["group_name"]

or

value, ok := yagm.Param(request, "group_name")

Full Example

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)
}

License

MIT

Documentation

Overview

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

Constants

This section is empty.

Variables

This section is empty.

Functions

func Param

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

Param return the value of a route variable.

func Params

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

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

Types

type YagmMux

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

func New() *YagmMux

New allocates and returns a new YagmMux.

func (*YagmMux) Handle

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

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

HandleFunc registers the handler function for the given pattern.

func (*YagmMux) ServeHTTP

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

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

Source Files

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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