grouter

package module
v0.0.0-...-d30e54a Latest Latest
Warning

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

Go to latest
Published: Jun 12, 2020 License: MIT Imports: 4 Imported by: 0

README

Go Router

Package grouter implements a request router based on the gorilla/mux package, in order to make sure you can bind controllers to the router in any order without endpoint URL conflicts.

Install

You can go get the package with

go get -u github.com/DynexIT/grouter
Examples
func main(){
	r := grouter.NewRouter()
	r.Bind("/path/{variable}/", func(w http.ResponseWriter, r *http.Request){
		//Some code to execute at the API endpoint here
	})
	log.Fatal(http.listenAndServe("localhost:8080", r.BuildMuxRouter()))
}

The bind function will pend the function for being handled by the router till the BuildMuxRouter() function has been called. When the function is called, it will bind the endpoints in the correct order as to not have API conflicts. This allows the endpoints to not know about each other and be handed off to controllers to have their functions bound.

URL's will also not be penalized for not having trailing or leading /'s and will have it reformatted to be a normalized URL for the router with expected behavior, as well as having mapped out the same function for URL's without the trailing /.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Route

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

func (*Route) Methods

func (a *Route) Methods(methods ...string) *Route

type Router

type Router struct {
	MuxRouter *mux.Router

	RespondToOptions bool
	// contains filtered or unexported fields
}

func NewRouter

func NewRouter() *Router

func (*Router) Bind

func (r *Router) Bind(path string, function func(w http.ResponseWriter, r *http.Request)) *Route

func (*Router) BuildMuxRouter

func (r *Router) BuildMuxRouter() *mux.Router

func (*Router) PathPrefix

func (r *Router) PathPrefix(tpl string, function func(w http.ResponseWriter, r *http.Request)) *Route

func (*Router) RespondOKToOptions

func (r *Router) RespondOKToOptions(respond bool)

func (*Router) Use

func (r *Router) Use(mwf ...mux.MiddlewareFunc)

Jump to

Keyboard shortcuts

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