routes

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Mar 2, 2019 License: MIT Imports: 3 Imported by: 0

README

GoDoc Go Report Card CircleCI Codecov Code Climate maintainability

routes

import "github.com/jspc/routes"

Overview

Package routes is a small, no-frills routing library for fasthttp. It's designed to sit within a fasthttp aware service to determine which route, from a map, to direct a ctx at.

It is designed:

  1. To contain no third party module (beyond fasthttp)

  2. To be as unobtrusive as possible

Index

Examples
Package files

doc.go routes.go

type Routes

type Routes struct {
    Routes  map[string]fasthttp.RequestHandler
    Catcher fasthttp.RequestHandler
}

Routes represents the fasthttp aware routes and configuration that determine which route to choose

func New
func New() *Routes

New is a friendly, convenience function for returning an instance of routes.Routes that can be used in client code

func (*Routes) Add
func (r *Routes) Add(pattern string, f fasthttp.RequestHandler)

Add takes a pattern, a function, and adds them to its self so requests can be routed correctly.

A pattern can be a full url, or can use parameters. Params in URLs look like:

/users/:user/address

This would match on:

/users/12345/address

(For instance)

Add() does no checking for existing routes; it is the responsibility of the developer to ensure there are no duplicates. The last function assigned to a patter will be used.

func (Routes) Route
func (r Routes) Route(ctx *fasthttp.RequestCtx)

Route will send a fasthttp request to the correct function based on the path in the request. Parameters, as defined in a route, are accessed by ctx.userValue(param)


Generated by godoc2md

Documentation

Overview

Package routes is a small, no-frills routing library for fasthttp. It's designed to sit within a fasthttp aware service to determine which route, from a map, to direct a ctx at.

It is designed:

1. To contain no third party module (beyond fasthttp)

2. To be as unobtrusive as possible

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Routes

type Routes struct {
	Routes  map[string]fasthttp.RequestHandler
	Catcher fasthttp.RequestHandler
}

Routes represents the fasthttp aware routes and configuration that determine which route to choose

func New

func New() *Routes

New is a friendly, convenience function for returning an instance of routes.Routes that can be used in client code

func (*Routes) Add

func (r *Routes) Add(pattern string, f fasthttp.RequestHandler)

Add takes a pattern, a function, and adds them to its self so requests can be routed correctly.

A pattern can be a full url, or can use parameters. Params in URLs look like:

/users/:user/address

This would match on:

/users/12345/address

(For instance)

Add() does no checking for existing routes; it is the responsibility of the developer to ensure there are no duplicates. The last function assigned to a patter will be used.

Example (Missing)
package main

import (
	"fmt"

	"github.com/jspc/routes"
	"github.com/valyala/fasthttp"
)

func main() {
	r := routes.New()
	r.Add("/", func(ctx *fasthttp.RequestCtx) {
		fmt.Println("root")
	})

	r.Catcher = func(_ *fasthttp.RequestCtx) {
		fmt.Println("404")
	}

	req := fasthttp.AcquireRequest()
	req.SetRequestURI("/this/url/doesnt/exist")
	req.Header.SetMethod("GET")

	resp := fasthttp.AcquireResponse()

	c := &fasthttp.RequestCtx{
		Request:  *req,
		Response: *resp,
	}

	r.Route(c)
}
Output:

404
Example (Params)
package main

import (
	"fmt"

	"github.com/jspc/routes"
	"github.com/valyala/fasthttp"
)

func main() {
	r := routes.New()
	r.Add("/hello/:name", func(ctx *fasthttp.RequestCtx) {
		name := ctx.UserValue("name")
		fmt.Printf("Pleased to meet you %v\n", name)
	})

	req := fasthttp.AcquireRequest()
	req.SetRequestURI("/hello/jspc")
	req.Header.SetMethod("GET")

	resp := fasthttp.AcquireResponse()

	c := &fasthttp.RequestCtx{
		Request:  *req,
		Response: *resp,
	}

	r.Route(c)
}
Output:

Pleased to meet you jspc
Example (Simple)
package main

import (
	"fmt"

	"github.com/jspc/routes"
	"github.com/valyala/fasthttp"
)

func main() {
	r := routes.New()
	r.Add("/", func(ctx *fasthttp.RequestCtx) {
		fmt.Println("root")
	})

	req := fasthttp.AcquireRequest()
	req.SetRequestURI("/")
	req.Header.SetMethod("GET")

	resp := fasthttp.AcquireResponse()

	c := &fasthttp.RequestCtx{
		Request:  *req,
		Response: *resp,
	}

	r.Route(c)
}
Output:

root

func (Routes) Route

func (r Routes) Route(ctx *fasthttp.RequestCtx)

Route will send a fasthttp request to the correct function based on the path in the request. Parameters, as defined in a route, are accessed by ctx.userValue(param)

Jump to

Keyboard shortcuts

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