lazydispatch

package module
v0.0.10 Latest Latest
Warning

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

Go to latest
Published: Aug 15, 2024 License: BSD-3-Clause Imports: 14 Imported by: 2

README

Lazy Dispatch

lazydispatch is a module of golazy to route http request into user defined actions.

This document covers the funcionality of lazydispatch alone. Please read the golazy intro first before aproaching this document

  • Intro
  • Creating Routes
  • Using controllers
  • The base controller
  • The controller model
  • Testing actions
  • Using it without the rest of golazy

Intro

  d := lazydispatcher.New()
  d.Draw(func (drawer *lazydispatcher.Scope){
    drawer.Resources(&PostsController{})
  })

And a controller defined as:

type PostsController struct {}

func (c *PostsController) Index(){}

func (c *PostsController)



# base.Controller




* base.Controller to handle all things related with the request.




# lazydispatch.Dispatcher



```go

func main() {
	dispatcher := lazydispatch.New()
	dispatcher.Use(middlewares.Logger)
	dispatcher.Draw(func( routes lazydispatch.Scope){
		routes.Resources(&PostsController{})
	})
	http.ListenAndServe(":2000", dispatcher)
}

var postsDB = map[string]string{
	{"hello", "hi this is my first post"},
	{"golazy", "the fastests way to do webpages" },
}

type PostsController struct {

}

func (c *PostsController) Index() {

}

func (c *PostsController) Show(id string) error {



}

*/

Documentation

Overview

pacakge actionhandler creates an http handler for a given controller/method

Index

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 Resource

type Resource struct {
	Controller any
	// contains filtered or unexported fields
}

func (*Resource) Draw

func (r *Resource) Draw(fn func(s *Scope)) *Scope

func (*Resource) Name

func (r *Resource) Name(name string) *Resource

func (*Resource) Path

func (r *Resource) Path(path string) *Resource

type Resources

type Resources struct {
	Controller any

	Scheme, Domain, Port string
	// contains filtered or unexported fields
}

Record

func (*Resources) As

func (r *Resources) As(a string) *Resources

func (*Resources) Draw

func (r *Resources) Draw(fn func(s *Scope))

func (*Resources) Model

func (r *Resources) Model(model any) *Resources

Model sets the model associated with the controller

Resources(&PostsController{}).Model(&Post{})
PathFor(&Post{ID: 1}) // => "/posts/1"

Look at []

func (*Resources) Name

func (r *Resources) Name(s string) *Resources

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

func (*Resources) Namespace

func (r *Resources) Namespace(n string) *Resources

func (*Resources) ParamName

func (r *Resources) ParamName(paramName string) *Resources

func (*Resources) Path

func (r *Resources) Path(p string) *Resources

func (*Resources) PathNames

func (r *Resources) PathNames(new, edit string) *Resources

Pathnames sets the names of the new and edit paths

func (*Resources) Plural

func (r *Resources) Plural(s string) *Resources

Plural sets the plural name of the resource and infers the path from it.

func (*Resources) Singular

func (r *Resources) Singular(s string) *Resources

Singular sets the singular name of the resource and infers the param name from it.

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
}

func (*Route) String

func (r *Route) String() string

type Scope

type Scope struct {
	// contains filtered or unexported fields
}

func (*Scope) As

func (s *Scope) As(a string) *Scope

func (*Scope) Delete

func (s *Scope) Delete(path string) *Scope

func (*Scope) Draw

func (s *Scope) Draw(fn func(s *Scope)) *Scope

func (*Scope) Get

func (s *Scope) Get(path string) *Scope

func (*Scope) Namespace

func (s *Scope) Namespace(n string) *Scope

func (*Scope) Options

func (s *Scope) Options(path string) *Scope

func (*Scope) Patch

func (s *Scope) Patch(path string) *Scope

func (*Scope) Path

func (s *Scope) Path(p string) *Scope

func (*Scope) Post

func (s *Scope) Post(path string) *Scope

func (*Scope) Put

func (s *Scope) Put(path string) *Scope

func (*Scope) RedirectTo

func (s *Scope) RedirectTo(path string, code ...int)

func (*Scope) Resource

func (s *Scope) Resource(controller any) *Resource

func (*Scope) Resources

func (s *Scope) Resources(controller any, model ...any) *Resources

Scope

func (*Scope) To

func (s *Scope) To(h http.Handler)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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