router

package module
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Sep 5, 2019 License: Apache-2.0 Imports: 4 Imported by: 0

README

router

Build Status GoDoc License Go version

This is a simple HTTP router library that does not have built-in support for middleware, but that doesn't preclude the use of other middleware libraries. Handler functions are nearly identical to the normal ServeHTTP(http.ResponseWriter, *http.Request) but the request object has been extended to include path variables and query parameters in a field named Params.

Features

  • Uses the same machinery as the standard HTTP server.
  • Should integrate well with all available middleware libraries.
  • Decodes path variables and query parameters.
  • AddRoute function can be chained.

Usage

func Example() {
    // Sample search handler.
    search := func(writer http.ResponseWriter, request *Request) {
        fmt.Printf("Search for: \"%s\"\n", request.Params["s"])
        writer.WriteHeader(200)
    }

    // Sample get resource handler.
    getBookByIsbn := func(writer http.ResponseWriter, request *Request) {
        fmt.Printf("Get book with ISBN = %s\n", request.Params["isbn"])
        writer.WriteHeader(200)
    }

    // build the router.
    router := NewRouter().
        AddRoute("GET", "/search",      search).
        AddRoute("GET", "/book/{isbn}", getBookByIsbn)

    // Start the server.
    server := httptest.NewServer(router)
    defer server.Close()

    http.Get(server.URL + "/search?s=stuff+to+search+for")
    http.Get(server.URL + "/book/978-0316371247")

    // OUTPUT: Search for: "stuff to search for"
    // Get book with ISBN = 978-0316371247
}

Contributing

  1. Fork it
  2. Create a feature branch (git checkout -b new-feature)
  3. Commit changes (git commit -am "Added new feature xyz")
  4. Push the branch (git push origin new-feature)
  5. Create a new pull request.

Maintainers

License

Copyright 2019 Metaleaf.io

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

Overview

Copyright 2019 Metaleaf.io

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.

Example

Examples that describe how to implement route handlers that accept query parameters and path variables.

// Sample search handler.
search := func(writer http.ResponseWriter, request *Request) {
	fmt.Printf("Search for: \"%s\"\n", request.Params["s"])
	writer.WriteHeader(200)
}

// Sample get resource handler.
getBookByIsbn := func(writer http.ResponseWriter, request *Request) {
	fmt.Printf("Get book with ISBN = %s\n", request.Params["isbn"])
	writer.WriteHeader(200)
}

// build the router.
router := NewRouter().
	AddRoute("GET", "/search", search).
	AddRoute("GET", "/book/{isbn}", getBookByIsbn)

// Start the server.
server := httptest.NewServer(router)
defer server.Close()

http.Get(server.URL + "/search?s=stuff+to+search+for")
http.Get(server.URL + "/book/978-0316371247")
Output:

Search for: "stuff to search for"
Get book with ISBN = 978-0316371247

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Handler

type Handler func(http.ResponseWriter, *Request)

Prototype for the handler function.

type Request

type Request struct {
	// The original request structure.
	*http.Request

	// Parameters are a hash of key/value strings. These are extracted from
	// the URL path and the query that appears after a question mark "?" in
	// the path.
	Params map[string]string
}

Enhances the regular http.Request structure by adding the parameters

type Router

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

Stores routes added by the application.

func NewRouter

func NewRouter() *Router

NewRouter initializes a new HTTP request httprouter.

func (*Router) AddRoute

func (router *Router) AddRoute(verb string, path string, handler Handler) *Router

Adds a new route with a handler function. The router structure is also returned to allow chaining.

func (*Router) ServeHTTP

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

Default global request handler that matches the incoming request with a registered handler.

Jump to

Keyboard shortcuts

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