Documentation
¶
Overview ¶
pacakge actionhandler creates an http handler for a given controller/method
Index ¶
- type Dispatcher
- type Resource
- type Resources
- func (r *Resources) As(a string) *Resources
- func (r *Resources) Draw(fn func(s *Scope))
- func (r *Resources) Model(model any) *Resources
- func (r *Resources) Name(s string) *Resources
- func (r *Resources) Namespace(n string) *Resources
- func (r *Resources) ParamName(paramName string) *Resources
- func (r *Resources) Path(p string) *Resources
- func (r *Resources) PathNames(new, edit string) *Resources
- func (r *Resources) Plural(s string) *Resources
- func (r *Resources) Singular(s string) *Resources
- type Route
- type Scope
- func (s *Scope) As(a string) *Scope
- func (s *Scope) Delete(path string) *Scope
- func (s *Scope) Draw(fn func(s *Scope)) *Scope
- func (s *Scope) Get(path string) *Scope
- func (s *Scope) Namespace(n string) *Scope
- func (s *Scope) Options(path string) *Scope
- func (s *Scope) Patch(path string) *Scope
- func (s *Scope) Path(p string) *Scope
- func (s *Scope) Post(path string) *Scope
- func (s *Scope) Put(path string) *Scope
- func (s *Scope) RedirectTo(path string, code ...int)
- func (s *Scope) Resource(controller any) *Resource
- func (s *Scope) Resources(controller any, model ...any) *Resources
- func (s *Scope) To(h http.Handler)
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Dispatcher ¶
type Dispatcher struct {
Routes []*Route
// contains filtered or unexported fields
}
func New ¶
func New() *Dispatcher
func (*Dispatcher) Draw ¶
func (d *Dispatcher) Draw(fn func(r *Scope)) *Scope
Example ¶
// type PagesController struct{}
//
// func (*PagesController) Index() string {
// return "index"
// }
//
// func (*PagesController) Show(page string) string {
// return "This is " + page
// }
dispatcher := New()
dispatcher.Draw(func(routes *Scope) {
routes.Resources(&PagesController{})
})
for _, r := range dispatcher.Routes {
fmt.Printf("%s\t%s\t%s\n", r.URL, r.Target, r.Name)
}
Output: /pages PagesController#Index pages /pages/:page_id PagesController#Show page
func (*Dispatcher) PathFor ¶
func (d *Dispatcher) PathFor(args ...any) string
func (*Dispatcher) ServeHTTP ¶
func (d *Dispatcher) ServeHTTP(w http.ResponseWriter, r *http.Request)
func (*Dispatcher) Use ¶
func (d *Dispatcher) Use(middleware func(http.Handler) http.Handler)
Use adds a middleware to the dispatcher All the middlewares have to be setup before calling ServeHTTP
Example ¶
d := New()
d.Use(func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fmt.Println("Got request:", r.URL)
next.ServeHTTP(w, r)
})
})
s := httptest.NewServer(d)
defer s.Close()
r, _ := http.NewRequest("GET", s.URL+"/posts", nil)
s.Client().Do(r)
Output: Got request: /posts
type Resources ¶
type Resources struct {
Controller any
Scheme, Domain, Port string
// contains filtered or unexported fields
}
Record
func (*Resources) Model ¶
Model sets the model associated with the controller
Resources(&PostsController{}).Model(&Post{})
PathFor(&Post{ID: 1}) // => "/posts/1"
Look at []
func (*Resources) Name ¶
Name sets the controller name, and infers the path, plural, singular, and param name from it.
Resources(&PostsController{}).Name("articles")
// Is the same as:
r := Resources(&PostsController{})
r.Plural("articles")
r.Singular("article")
r.ParamName("article_id")
r.Path("articles")
type Route ¶
type Route struct {
// URL is the patch to match
// It supports two kind of wildcards:
// - :post_id as a named paramenter
// - * as a catch all that will match anything
//
// It is also extension aware, so the following request will match the same route:
// - /posts/1
// - /posts/1.html // Routes to /posts/1 and sets the content-type to text/html
// - /posts/1.json // Rotues to /posts/1 and sets the content-type to application/json
// - /posts/1/ // Trailing slash are ignored
//
// The url can include ports, domains and schemas:
// - http://example.com:8080/posts/1
// - https://example.com/posts/1
// - /example.com/posts/1
// - //:3000/posts/1
// - http://:3000/posts/1
URL string
Path string // Path is the path component of the URL.
Method string // One of GET, POST, PUT, PATCH, DELETE, OPTIONS, HEAD
// Models allows the router to generate paths just by providing the model
// for example path_for(&User{}) will return /users/:id
// In case of a nested resource it will require the parent model
// for example path_for(&Account{}, &Post{}) will return /accounts/:account_id/posts/:post_id
// In case there are named routes, the model can use the name to generate the path
// for example path_for(&User{}, "profile") will return /users/:id/profile
// There is an explicit way to get the path:
// path_for("posts", &Post{Id:3}) will return /posts/3
// path_for("posts", &Post{Id:3}, "comments") will return /posts/3/comments
// path_for("posts", &Post{Id:2}, &Comment{Id:5}, "review") will return /posts/2/comments/5/review
// The previous path can also be expressed as:
// path_for("review_posts_comment", &Post{}, &Comment{}) will return /posts/:post_id/comments/:comment_id/review
Models []any
// Name holds the name of the route
Name string
// Action Is the action on the resource. For example "review" on a post
Action string
// Controller is the name of the controller. For example "PostsController"
Controller string
// Namespace is the namespace of the controller
Namespace string
// Target is the name of the controller. For example "PostsController#Index" It is only use for debugging purposes
Target string
Handler http.Handler
}
type Scope ¶
type Scope struct {
// contains filtered or unexported fields
}
func (*Scope) RedirectTo ¶
Source Files
¶
Click to show internal directories.
Click to hide internal directories.