Skip to content
Go route splitting library
Go Shell
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
.gitignore
.travis.yml
LICENSE
README.md
errors.go
gosplitter.go
gosplitter_test.go
install-deps.sh
integration_test.go
utils.go

README.md

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
You can’t perform that action at this time.