routing

package module
v1.3.1 Latest Latest
Warning

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

Go to latest
Published: Dec 23, 2018 License: MIT Imports: 10 Imported by: 1

README

routing

GoDoc Build Status

Routing requests to handlers.

Install
go get github.com/altipla-consulting/routing

This library depends on the following ones:

Usage
package main

import (
  "fmt"
  "net/http"

  "github.com/altipla-consulting/routing"
  "github.com/altipla-consulting/langs"
  "github.com/julienschmidt/httprouter"
)

func RobotsHandler(w http.ResponseWriter, r *http.Request) error {
  fmt.Fprintln(w, "ok")
  return nil
}

func main {
  s := routing.NewServer(r)
  s.Get(langs.ES, "/robots.txt", RobotsHandler)
}
Contributing

You can make pull requests or create issues in GitHub. Any code you send should be formatted using gofmt.

Running tests

Run the tests

make test
License

MIT License

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func BadRequest

func BadRequest(s string, args ...interface{}) error

BadRequest returns a 400 HTTP error and formats its message.

func Internal

func Internal(s string, args ...interface{}) error

Internal returns a 500 HTTP error and formats its message.

func Lang

func Lang(r *http.Request) string

Lang returns the language linked to the request in the routing table.

func LangFromContext

func LangFromContext(ctx context.Context) string

LangFromContext returns the lang of the route linked to the request in the routing table. The context must have been previously extracted from r.Context().

func NotFound

func NotFound(s string, args ...interface{}) error

NotFound returns a 404 HTTP error and formats its message.

func Param

func Param(r *http.Request, name string) string

Param returns a request URL parameter value.

func RequestFromContext

func RequestFromContext(ctx context.Context) *http.Request

RequestFromContext returns the HTTP request from a context. The context must have been previously extracted from r.Context().

func Unauthorized

func Unauthorized(s string, args ...interface{}) error

Unauthorized returns a 401 HTTP error and formats its message.

Types

type Error

type Error struct {
	StatusCode int
	Message    string
}

Error stores info about a HTTP error returned from a route.

func (Error) Error

func (err Error) Error() string

Error implements the error interface with the message.

type Group

type Group struct {
	// Handler that will be called for all langs.
	Handler Handler

	// HTTP method to use. If empty or unknown it will use "GET".
	Method string

	// Map of language -> URL that should be registered.
	URL map[string]string
}

Group registers multiple URLs for each lang for the same handler.

func GroupFromContext

func GroupFromContext(ctx context.Context) Group

GroupFromContext returns the context of the request. If the route was direct and used no group it returns an empty group with no URLs.

func (Group) ResolveURL

func (g Group) ResolveURL(r *http.Request, lang string) string

ResolveURL returns the URL of the group linked to the language we pass. If no URL is found it will return an empty string. It replaces any parameter with the value passed right now in the request.

type Handler

type Handler func(w http.ResponseWriter, r *http.Request) error

Handler should be implemented by the handler functions that we want to register.

type Server

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

Server configures the routing table.

func NewServer

func NewServer(opts ...ServerOption) *Server

NewServer configures a new router with the options.

func (*Server) Delete

func (s *Server) Delete(lang, path string, handler Handler)

Delete registers a new DELETE route in the router.

func (*Server) Get

func (s *Server) Get(lang, path string, handler Handler)

Get registers a new GET route in the router.

func (*Server) Group

func (s *Server) Group(g Group)

Group registers all the routes of the group in the router.

func (*Server) Post

func (s *Server) Post(lang, path string, handler Handler)

Post registers a new POST route in the router.

func (*Server) Put

func (s *Server) Put(lang, path string, handler Handler)

Put registers a new PUT route in the router.

func (*Server) Router

func (s *Server) Router() *httprouter.Router

Router returns the raw underlying router to make advanced modifications. If you modify the NotFound handler remember to call routing.NotFoundHandler as fallback if you don't want to process the request.

type ServerOption

type ServerOption func(server *Server)

ServerOption is implement by any option that can be passed when constructing a new server.

func WithBetaAuth

func WithBetaAuth(username, password string) ServerOption

WithBetaAuth installs a rough authentication mechanism to avoid the final users to access beta sites.

func WithCustom404 added in v1.2.0

func WithCustom404(handler Handler) ServerOption

WithCustom404 uses a custom 404 template file.

func WithLogrus

func WithLogrus() ServerOption

WithLogrus enables logging of the errors of the handlers.

func WithSentry

func WithSentry(dsn string) ServerOption

WithSentry configures Sentry logging of issues in the handlers.

Jump to

Keyboard shortcuts

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