router

package
v0.0.37 Latest Latest
Warning

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

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

README

Current implementation wraps (chi)[https://github.com/go-chi/chi]

Note that while chi's patterns do support regex, you are advised to limit this to simple expressions such as alpha/alpha-numeric/numeric-only.

It is also advised that all alpha params be case insensitive, such that functionality would not be effected as, for example, the result of router.StringParam(r, "name") returned strictly lowercase values.

import (
	"bitbucket.org/atlassian/go-stack/router"
	"bitbucket.org/atlassian/go-stack/log"
	"net/http"
	"io"
)

var LOG, _ = log.New("examples")

func accessLogger(f http.Handler) http.Handler {
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		defer LOG.Sync()
		LOG.Info(
			"access",
			log.String("method", r.Method),
			log.String("path", r.URL.Path),
			log.String("ip", r.RemoteAddr),
		)
		f.ServeHTTP(w, r)
	})
}

func helloWorld(w http.ResponseWriter, r *http.Request) {
	name := router.StringParam(r, "name")
	io.WriteString(w, "Hello "+name)
}

func main() {
	r := router.New()
	r.Use(accessLogger)
	r.Route(
		"/hello",
		func(r router.Router) {
			r.Get("/world/{name:[a-zA-Z]+}", helloWorld)
		},
	)
	http.ListenAndServe(":3000", r)
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func IntParam

func IntParam(r *http.Request, paramName string) int

*

  • Retrieves the named parameter from the request context *
  • PANIC if the param is not a valid integer (or is missing)

func StringParam

func StringParam(r *http.Request, paramName string) string

*

  • Returns a (lowercase) string from the path, as found in
  • the request path. *
  • If the parameter is not found, returns an empty string

Types

type Router

type Router interface {
	Mount(pattern string, handler http.Handler)
	Get(pattern string, handler http.HandlerFunc)
	Post(pattern string, handler http.HandlerFunc)
	Put(pattern string, handler http.HandlerFunc)
	Delete(pattern string, handler http.HandlerFunc)
	Route(pattern string, f func(Router)) Router
	Use(middlewares ...func(http.Handler) http.Handler)

	http.Handler
}

func New

func New() Router

Jump to

Keyboard shortcuts

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