Skip to content

Latest commit

 

History

History

lazyaction

router

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

Types

type CatchAllPath struct { ... }

type CatchAllPrefix struct { ... }

type Handler

type Handler interface { ... }

type HandlerFunc func(w ResponseWriter, r *Request)

func (HandlerFunc) ServeHTTP

func (h HandlerFunc) ServeHTTP(w ResponseWriter, r *Request)

type Method

type Method string

type Path

type Path struct { ... }

type PathSegment interface{ ... }

type Prefix

type Prefix struct { ... }

type RedirectPath struct { ... }

func (RedirectPath) ServeHTTP

func (rp RedirectPath) ServeHTTP(w ResponseWriter, r *Request)

type Request

type Request struct { ... }

func (*Request) GetParam

func (r *Request) GetParam(name string) string

type ResponseWriter struct { ... }

type Router

type Router Routes

func (Router) ServeHTTP

func (router Router) ServeHTTP(w http.ResponseWriter, r *http.Request)

type Routes

type Routes []PathSegment

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(Controller interface{ ... }) Routes

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

func (Routes) String

func (r Routes) String() string


Readme created from Go doc with goreadme