gohttprouter

package module
v1.0.2 Latest Latest
Warning

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

Go to latest
Published: Aug 22, 2019 License: Apache-2.0 Imports: 3 Imported by: 0

README

GoHTTPRouter

GoDoc pipeline status coverage report codecov Go Report Card

GoHTTPRouter is a framework used for HTTP request routing.

Examples

Simple Routing

Just replace the standard router of golang with this one:

router := gohttprouter.New()
router.HandleFunc(http.MethodGet, "/home", func(response http.ResponseWriter, request *http.Request, info gohttprouter.RoutingInfo) {
    response.Write([]byte("Home"))
})
err := http.ListenAndServe("localhost:8080", router)

Routing with placeholder

A path can also contain placeholder (like :id). If then a request is sent to the url "/user/123" the method gets executed and the URL part (in this case "123") is passed as parameter into your handler function.

router := gohttprouter.New()
router.HandleFunc(http.MethodGet, "/user/:id", func(response http.ResponseWriter, request *http.Request, info gohttprouter.RoutingInfo) {
    id := info.Parameters["id"]
})
err := http.ListenAndServe("localhost:8080", router)

License

Copyright 2019 Martin Riedl

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

	http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Must

func Must(err error)

Must is a wrapper that panics if an error was returned.

Types

type Handler

type Handler interface {
	ServeHTTP(http.ResponseWriter, *http.Request, RoutingInfo)
}

Handler interface that must be implemented by custom structs.

type RouteHandler

type RouteHandler func(http.ResponseWriter, *http.Request, RoutingInfo)

RouteHandler is the function construct called by this framework.

type Router

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

Router is the main struct for the whole routing logic.

func New

func New() *Router

New creates a new router instance.

func (*Router) Handle

func (router *Router) Handle(method string, path string, handler Handler) error

Handle registers a new handler using method and path for execution.

func (*Router) HandleFunc

func (router *Router) HandleFunc(method string, path string, handler RouteHandler) error

HandleFunc registers a new handler function using method and path for execution.

func (*Router) ServeHTTP

func (router *Router) ServeHTTP(response http.ResponseWriter, request *http.Request)

type RoutingInfo

type RoutingInfo struct {
	Parameters map[string]string
}

RoutingInfo is returned for each web server method call and includes details abount the execution.

Jump to

Keyboard shortcuts

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