首页 > 代码库 > router

router

1. router in golang

1). sample code

 1 package main 2  3 import ( 4     "fmt" 5     "net/http" 6 ) 7  8 func main() { 9 10     http.HandleFunc("/handlefunc", func(w http.ResponseWriter, r *http.Request) {11         fmt.Fprintf(w, "http.HandleFunc")12     })13 14     http.Handle("/handle", &myHandler{})15 16     http.ListenAndServe(":8080", nil)17 }18 19 type myHandler struct{}20 21 func (this *myHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {22     fmt.Fprintf(w, "http.Handle")23 }

 

 2). implement

a) top layer:

// Objects implementing the Handler interface can be// registered to serve a particular path or subtree// in the HTTP server.//// ServeHTTP should write reply headers and data to the ResponseWriter// and then return.  Returning signals that the request is finished// and that the HTTP server can move on to the next request on// the connection.type Handler interface {    ServeHTTP(ResponseWriter, *Request)}

 

// The HandlerFunc type is an adapter to allow the use of// ordinary functions as HTTP handlers.  If f is a function// with the appropriate signature, HandlerFunc(f) is a// Handler object that calls f.type HandlerFunc func(ResponseWriter, *Request)// ServeHTTP calls f(w, r).func (f HandlerFunc) ServeHTTP(w ResponseWriter, r *Request) {    f(w, r)

 

// Handle registers the handler for the given pattern// in the DefaultServeMux.// The documentation for ServeMux explains how patterns are matched.func Handle(pattern string, handler Handler) { DefaultServeMux.Handle(pattern, handler) }// HandleFunc registers the handler function for the given pattern// in the DefaultServeMux.// The documentation for ServeMux explains how patterns are matched.func HandleFunc(pattern string, handler func(ResponseWriter, *Request)) {    DefaultServeMux.HandleFunc(pattern, handler)}

Now we know that both of http.HandleFunc and http.Handle invoke function of DefaultServeMux.

b) DefaultServeMux

// NewServeMux allocates and returns a new ServeMux.func NewServeMux() *ServeMux { return &ServeMux{m: make(map[string]muxEntry)} }// DefaultServeMux is the default ServeMux used by Serve.var DefaultServeMux = NewServeMux()

 

// ServeMux 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// most closely matches the URL.//type ServeMux struct {    mu    sync.RWMutex    m     map[string]muxEntry    hosts bool // whether any patterns contain hostnames}type muxEntry struct {    explicit bool    h        Handler    pattern  string}

 

// HandleFunc registers the handler function for the given pattern.func (mux *ServeMux) HandleFunc(pattern string, handler func(ResponseWriter, *Request)) {    mux.Handle(pattern, HandlerFunc(handler))}

 

// Handle registers the handler for the given pattern.// If a handler already exists for pattern, Handle panics.func (mux *ServeMux) Handle(pattern string, handler Handler) {    mux.mu.Lock()    defer mux.mu.Unlock()    if pattern == "" {        panic("http: invalid pattern " + pattern)    }    if handler == nil {        panic("http: nil handler")    }    if mux.m[pattern].explicit {        panic("http: multiple registrations for " + pattern)    }    mux.m[pattern] = muxEntry{explicit: true, h: handler, pattern: pattern}    if pattern[0] != / {        mux.hosts = true    }    // Helpful behavior:    // If pattern is /tree/, insert an implicit permanent redirect for /tree.    // It can be overridden by an explicit registration.    n := len(pattern)    if n > 0 && pattern[n-1] == / && !mux.m[pattern[0:n-1]].explicit {        // If pattern contains a host name, strip it and use remaining        // path for redirect.        path := pattern        if pattern[0] != / {            // In pattern, at least the last character is a ‘/‘, so            // strings.Index can‘t be -1.            path = pattern[strings.Index(pattern, "/"):]        }        mux.m[pattern[0:n-1]] = muxEntry{h: RedirectHandler(path, StatusMovedPermanently), pattern: pattern}    }}

 

 

2. rounter in beego

router