router

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

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

Go to latest
Published: Sep 13, 2019 License: MIT Imports: 4 Imported by: 0

README

goa-router

Router middleware for goa.

Build Status Codecov Go Doc Go Report

  • Based on httprouter
  • Multiple route middleware
  • Named URL parameters
  • Support for 405 Method Not Allowed
  • Responds to OPTIONS requests with matching methods

Installation

go get -u github.com/goa-go/goa 

Example

package main

import (
  ...

  "github.com/goa-go/goa"
  "github.com/goa-go/router"
)

func main() {
  app := goa.New()
  router := router.New()
  router.GET("/", func(c *goa.Context) {
    c.String("Hello Goa!")
  })
  ...

  app.Use(router.Routes())
  ...
}

License

MIT

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CleanPath

func CleanPath(p string) string

CleanPath is the URL version of path.Clean, it returns a canonical URL path for p, eliminating . and .. elements.

The following rules are applied iteratively until no further processing can be done:

  1. Replace multiple slashes with a single slash.
  2. Eliminate each . path name element (the current directory).
  3. Eliminate each inner .. path name element (the parent directory) along with the non-.. element that precedes it.
  4. Eliminate .. elements that begin a rooted path: that is, replace "/.." by "/" at the beginning of a path.

If the result of this process is an empty string, "/" is returned

Types

type Handler

type Handler func(*goa.Context)

Handler is the type of goa-router handle function.

type Router

type Router struct {

	// Enables automatic redirection if the current route can't be matched but a
	// handler for the path with (without) the trailing slash exists.
	// For example if /foo/ is requested but a route only exists for /foo, the
	// client is redirected to /foo with http status code 301 for GET requests
	// and 307 for all other request methods.
	RedirectTrailingSlash bool

	// If enabled, the router tries to fix the current request path, if no
	// handler is registered for it.
	// First superfluous path elements like ../ or // are removed.
	// Afterwards the router does a case-insensitive lookup of the cleaned path.
	// If a handler can be found for this route, the router makes a redirection
	// to the corrected path with status code 301 for GET requests and 307 for
	// all other request methods.
	// For example /FOO and /..//Foo could be redirected to /foo.
	// RedirectTrailingSlash is independent of this option.
	RedirectFixedPath bool

	// If enabled, the router checks if another method is allowed for the
	// current route, if the current request can not be routed.
	// If this is the case, the request is answered with 'Method Not Allowed'
	// and HTTP status code 405.
	// If no other Method is allowed, the request is delegated to the NotFound
	// handler.
	HandleMethodNotAllowed bool

	// If enabled, the router automatically replies to OPTIONS requests.
	// Custom OPTIONS handlers take priority over automatic replies.
	HandleOPTIONS bool

	// Configurable http.Handler which is called when no matching route is found.
	NotFound Handler

	// Configurable http.Handler which is called when a request
	// cannot be routed and HandleMethodNotAllowed is true.
	// If it is not set, http.Error with http.StatusMethodNotAllowed is used.
	// The "Allow" header with allowed request methods is set before the handler
	// is called.
	MethodNotAllowed Handler

	// All allow methods
	Methods []string
	// contains filtered or unexported fields
}

Router is a http.Handler which can be used to dispatch requests to different handler functions via configurable routes

func New

func New() *Router

New returns a new initialized Router. Path auto-correction, including trailing slashes, is enabled by default.

func (*Router) DELETE

func (r *Router) DELETE(path string, handler Handler)

DELETE registers a new request handle with the given path and delete method.

func (*Router) GET

func (r *Router) GET(path string, handler Handler)

GET registers a new request handle with the given path and get method.

func (*Router) HEAD

func (r *Router) HEAD(path string, handler Handler)

HEAD registers a new request handle with the given path and head method.

func (*Router) Handle

func (r *Router) Handle(c *goa.Context)

Handle is goa-router's handle function.

func (*Router) OPTIONS

func (r *Router) OPTIONS(path string, handler Handler)

OPTIONS registers a new request handle with the given path and options method.

func (*Router) PATCH

func (r *Router) PATCH(path string, handler Handler)

PATCH registers a new request handle with the given path and patch method.

func (*Router) POST

func (r *Router) POST(path string, handler Handler)

POST registers a new request handle with the given path and post method.

func (*Router) PUT

func (r *Router) PUT(path string, handler Handler)

PUT registers a new request handle with the given path and put method.

func (*Router) Register

func (r *Router) Register(method, path string, handler Handler)

Register registers a new request handle with the given path and method.

For GET, POST, PUT, PATCH and DELETE requests the respective shortcut functions can be used.

This function is intended for bulk loading and to allow the usage of less frequently used, non-standardized or custom methods (e.g. for internal communication with a proxy).

func (*Router) Routes

func (r *Router) Routes() goa.Middleware

Routes returns a goa.Middleware. app.Use(router.Routes())

func (*Router) ServeFiles

func (r *Router) ServeFiles(path string, root http.FileSystem)

ServeFiles serves files from the given file system root. The path must end with "/*filepath", files are then served from the local path /defined/root/dir/*filepath. For example if root is "/etc" and *filepath is "passwd", the local file "/etc/passwd" would be served. Internally a http.FileServer is used, therefore http.NotFound is used instead of the Router's NotFound handler. To use the operating system's file system implementation, use http.Dir: router.ServeFiles("/src/*filepath", http.Dir("/var/www"))

Jump to

Keyboard shortcuts

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