gosplitter

package module
v0.0.0-...-06d68d8 Latest Latest
Warning

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

Go to latest
Published: Dec 11, 2017 License: MIT Imports: 5 Imported by: 0

README

Go Splitter

Build Status GoDoc Go Report Card Coverage

Library for convenient HTTP route splitting

What for ?

Go provide out of the box route handling, but it is not possible to 'split' route part and handle entry part with custom type - now it's possible

Hot to use

  gosplitter.Match("/your/path", *http.ServeMux, http.Handler|http.HandleFunc|interface{})

You can pass handler or another structure to chain multiple routes


Examples

/**
* Specify handle types
 */
type APIV1Handler struct {
	mux *http.ServeMux
}

type ColorsHandler struct {
	mux *http.ServeMux
}

/**
* Start - binds parent to children
 */
func (a *APIV1Handler) Start() {
	var colorsHandler = ColorsHandler{
		mux: a.mux,
	}
	gosplitter.Match("/ping", a.mux, a.HandlePing())
	gosplitter.Match("/colors", a.mux, colorsHandler)
	colorsHandler.Start()
}

/**
* Simple http handler function
 */
func (a *APIV1Handler) HandlePing() func(w http.ResponseWriter, r *http.Request) {
	return func(w http.ResponseWriter, r *http.Request) {
		w.Write([]byte("pong"))
	}
}

func (c *ColorsHandler) Start() {
	gosplitter.Match("/black", c.mux, c.HandleBlack())
}

func (c *ColorsHandler) HandleBlack() func(w http.ResponseWriter, r *http.Request) {
	return func(w http.ResponseWriter, r *http.Request) {
		w.Write([]byte("#000000"))
	}
}

func main() {
  var mux = http.NewServeMux()
	var apiV1 = APIV1Handler{
		mux: mux,
	}

  /**
  * bind api handler to root
   */
  gosplitter.Match("/api/v1", mux, apiV1)
  /**
  * start api handler
   */

  apiV1.Start()
 
}

TODO:

  • More tests
  • Benchmarking
  • Clear code
  • Comment-out code

Documentation

Index

Constants

This section is empty.

Variables

View Source
var RegisteredPatterns = make(map[string]*RouterPoint)

RegisteredPatterns type

Functions

func CallerContext

func CallerContext() string

CallerContext func

func GetAbsoluteURL

func GetAbsoluteURL(url string, caller string) (string, error)

GetAbsoluteURL func

func GetFunctionName

func GetFunctionName(myvar interface{}) string

GetFunctionName func

func Match

func Match(url string, mux *http.ServeMux, h interface{})

Match matches route

func RegisterHandler

func RegisterHandler(url string, caller string, h interface{}, mux *http.ServeMux)

RegisterHandler func

func RegisterRouterPoint

func RegisterRouterPoint(caller string, url string, f interface{})

RegisterRouterPoint func

Types

type InvalidHandlerError

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

InvalidHandlerError error

func (InvalidHandlerError) Error

func (e InvalidHandlerError) Error() string

type NotRegisteredPatternError

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

NotRegisteredPatternError error

func (*NotRegisteredPatternError) Error

func (n *NotRegisteredPatternError) Error() string

type PatternAlreadyRegisteredError

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

PatternAlreadyRegisteredError error

func (PatternAlreadyRegisteredError) Error

type RouterPoint

type RouterPoint struct {
	URL         string
	Handler     http.Handler
	HandlerFunc http.HandlerFunc
}

RouterPoint type

Jump to

Keyboard shortcuts

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