Documentation
¶
Overview ¶
Package router implements a http router that support url params and http verbs
It was design to use together lazyaction/controller:
But it can be used alone:
say := func(what string) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { w.Write([]byte(fmt.Sprint(what, r.Form))) } } router := &Router{ PrefixCatchAll{"page_id", Routes{ RoutePath{"", "", say("show_page")}, RoutePath{"publish", "", say("publish_page")}, }}, RoutePath{"posts", "", say("posts_index")}, Prefix{"posts", Routes{ RouteCatchAll{"post_id", "", say("post_show")}, RoutePath{"", "", say("post index")}, RoutePath{"new", "", say("post new")}, PrefixCatchAll{"post_id", Routes{ RoutePath{"publish", "", say("publish")}, }}, RoutePath{"publish", "", say("publish")}, }}, }
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type CatchAllPath ¶ added in v0.0.6
type CatchAllPrefix ¶ added in v0.0.6
type Handler ¶ added in v0.0.6
type Handler interface {
ServeHTTP(ResponseWriter, *Request)
}
type HandlerFunc ¶ added in v0.0.6
type HandlerFunc func(w ResponseWriter, r *Request)
func (HandlerFunc) ServeHTTP ¶ added in v0.0.6
func (h HandlerFunc) ServeHTTP(w ResponseWriter, r *Request)
type PathSegment ¶ added in v0.0.6
type PathSegment interface{}
type RedirectPath ¶ added in v0.0.6
func (RedirectPath) ServeHTTP ¶ added in v0.0.6
func (rp RedirectPath) ServeHTTP(w ResponseWriter, r *Request)
type ResponseWriter ¶
type ResponseWriter struct {
http.ResponseWriter
}
type Routes ¶ added in v0.0.6
type Routes []PathSegment
Example ¶
say := func(what string) HandlerFunc { return func(w ResponseWriter, r *Request) { params, _ := json.Marshal(r.Params) w.Write([]byte(fmt.Sprintf("%s Params: %s", what, params))) } } router := &Router{ Path{"", "GET", "", say("Home page")}, Path{"pages", "", "", say("Pages index")}, // HTTP Medoth defaults to GET Prefix{"pages", Routes{ // Handles `pages/` RedirectPath{ Path: "", To: "../pages", }, CatchAllPath{"page_id", "", "", say("Nice Page")}, // Matches `/pages/:page_id` assiging `page_id` to httpRequest.Form.Values.Get("page_id") CatchAllPrefix{"page_id", Routes{ Path{"share", "POST", "", say("Page shared!")}, // Matches `POST /pages/:page_id/share` Prefix{"paragraphs", Routes{ CatchAllPath{"paragraph_id", "", "", say("Paragraph")}, }}, }}, }}, } // For testing query := func(path string) string { w := httptest.NewRecorder() r := httptest.NewRequest("GET", path, nil) router.ServeHTTP(w, r) return w.Body.String() } fmt.Println(query("/pages/33")) fmt.Println(query("/pages/33/paragraph/42.json"))
Output: Nice Page Params: {"page_id":["33"]} Paragraph Params: {"format":["json"],"page_id":["33"],"paragraph_id":["42"]}
func Resource ¶ added in v0.0.6
func Resource(Controller interface{}) Routes
Example ¶
routes := Resource(new(PostsController)) fmt.Println(routes)
Output: METHOD PATH DESTINATION POST /posts PostsController#Create GET /posts PostsController#Index POST /posts/create_super PostsController#PostCreateSuper GET /posts/new PostsController#New DELETE /posts/:post_id PostsController#Delete GET /posts/:post_id PostsController#Show PUT /posts/:post_id PostsController#Update PATCH /posts/:post_id PostsController#Update PUT /posts/:post_id/activate_later PostsController#MemberPutActivateLater
Click to show internal directories.
Click to hide internal directories.