routemgr

package
v0.2.3 Latest Latest
Warning

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

Go to latest
Published: Jul 4, 2019 License: MIT Imports: 3 Imported by: 0

README

routemgr

Handles data or Callbacks with path or path pattern

Examples

Handle data
package main

import (
	"fmt"

	"gitlab.com/so_literate/drpc_utils/wsclient/routemgr"
)

type InfoPage struct{}
type itemsPage struct{}
type itemDetailPage struct{}
type itemDeletePage struct{}
type AllSpecPage struct{}
type AllPage struct{}
type AllNotSoPage struct{}
type AuthorPage struct{}
type CategoryPage struct{}

func main() {
	r := routemgr.New()

	r.Handle("/info", &InfoPage{})
	r.Handle("/items", &itemsPage{})
	r.Handle("/items/{id}/{color}", &itemDetailPage{})
	r.Handle("/items/{id}/delete", &itemDeletePage{})
	r.Handle("/all/special", &AllSpecPage{})
	r.Handle("/all/*", &AllPage{})
	r.Handle("/all/notso", &AllNotSoPage{})
	r.Handle("/{category}/{author}", &AuthorPage{})
	r.Handle("/{category}", &CategoryPage{})

	res, params := r.Process("/items/7/red")
	fmt.Printf("%#v\n", res)         // res will be &main.itemDetailPage
	fmt.Println(params.Get("id"))    // 7
	fmt.Println(params.Get("color")) // red

	fmt.Println()

	res, params = r.Process("/items")
	fmt.Printf("%#v\n\n", res) // &main.itemsPage

	res, params = r.Process("/items/7/red/delete")
	fmt.Printf("%#v\n\n", res) // <nil>

	res, params = r.Process("/items/7/delete")
	fmt.Printf("%#v\n\n", res) // &main.itemDeletePage

	res, params = r.Process("/all/jdsf/2323")
	fmt.Printf("%#v\n\n", res) // &main.AllPage

	res, params = r.Process("/all/special")
	fmt.Printf("%#v\n\n", res) // &main.AllSpecPage

	res, params = r.Process("/all/notso")
	fmt.Printf("%#v\n\n", res) // &main.AllNotSoPage

	res, params = r.Process("/music")
	fmt.Printf("%#v\n\n", res) // &main.CategoryPage

	res, params = r.Process("/music/Петля пристрастия")
	fmt.Printf("%#v\n", res)          // &main.AuthorPage
	fmt.Println(params.Get("author")) // Петля пристрастия
}
Handle execution functions
package main

import (
	"fmt"

	"gitlab.com/so_literate/drpc_utils/wsclient/routemgr"
)

func callback(p *routemgr.CallbackParams) {
	fmt.Println("method:", p.PathParams.Get("method")) // callback
	fmt.Println("data:", p.PathParams.Get("data"))     // withdata
	fmt.Println("path:", p.Path)                       // /exec/callback/withdata?param1=abc
}

func main() {
	r := routemgr.New()
	r.Debug = true

	r.Handle("/exec/{method}/{data}", callback)

	r.Execute("/exec/callback/withdata?param1=abc")
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type CallbackParams

type CallbackParams struct {
	PathParams   *PathParams
	Path         string
	PreviousPath string
}

CallbackParams is send to Callback in Execute PreviousPath is empty by default, you have to register callback use RegisterRouteWithPrev method

type PathParams

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

PathParams contains value of the path params

func (*PathParams) Get

func (pp *PathParams) Get(key string) string

Get returns value of the param path using its key

type RouteMgr

type RouteMgr struct {
	Debug bool
	// contains filtered or unexported fields
}

RouteMgr allows register patterns with values and get it back

func New

func New() *RouteMgr

New creates new route manager If debug true then Process will print pattern

func (*RouteMgr) Execute

func (r *RouteMgr) Execute(path string) bool

Execute calls function of the handled path Handled value should be func(p *CallbackParams) returns true if the path was found and the handler was executed

func (*RouteMgr) Handle

func (r *RouteMgr) Handle(pattern string, value interface{})

Handle adds the route `pattern` with this value

func (*RouteMgr) Process

func (r *RouteMgr) Process(path string) (interface{}, *PathParams)

Process finds path and returns saved value and path parameters from pattern

func (*RouteMgr) RegisterRoute

func (r *RouteMgr) RegisterRoute(url string, callback func(*CallbackParams), title string)

RegisterRoute handles a callback that will be called in the Execute method. Before callback call the path will be set in the URL bar and your title will be set if it not empty

func (*RouteMgr) RegisterRouteWithPrev

func (r *RouteMgr) RegisterRouteWithPrev(url string, callback func(*CallbackParams), title string)

RegisterRouteWithPrev adds previous path to CallbackParams

func (*RouteMgr) RegisterSimpleRoute

func (r *RouteMgr) RegisterSimpleRoute(url string, callback func(), title string)

RegisterSimpleRoute handles a callback without PathParams and path arguments Use it if you don't need them

Jump to

Keyboard shortcuts

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