gorouter

package module
v0.0.0-...-066a322 Latest Latest
Warning

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

Go to latest
Published: Apr 21, 2022 License: MIT Imports: 3 Imported by: 0

README

gorouter

A lightweight powerful HTTP router (or mux) in Go.

Features

  • 100% compatible with net/http
  • Trie based structure
  • Only exact path matches
  • Support path parameters, can use regexp

Getting Started

Installing

Install go route package with go get

go get -u github.com/akhrszk/gorouter

Start your first server. Create main.go file and add:

Examples
package main

import (
  "fmt"
  "net/http"

  "github.com/akhrszk/gorouter"
)

func hello(w http.ResponseWriter, r *http.Request, params gorouter.Params) {
  name := params["name"]
  fmt.Fprintf(w, "Hello, %s!", name)
}

func main() {
  rt := gorouter.New()
  rt.Get("/hello/:name", hello)
  http.ListenAndServe(":3000", rt)
}

Named Parameters

You can capture path segments. The path parameters is given to 3rd Handler function parameter. You can get the value of :name by params["name"].

Pattern: /hello/:name

/hello/suzuki            match
/hello/suzuki/           match
/hello/suzuki/welcome    no match
/hello/                  no match
Using regex pattern
Pattern: /users/:id(\\d+)

/hello/users/12          match
/hello/users/abc         no match
/hello/users/            no match

License

MIT License.

Authors

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Handler

type Handler func(http.ResponseWriter, *http.Request, Params)

type Node

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

type Params

type Params map[string]string

type Router

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

func New

func New() *Router

func (*Router) Connect

func (rt *Router) Connect(path string, fn Handler)

func (*Router) Delete

func (rt *Router) Delete(path string, fn Handler)

func (*Router) Get

func (rt *Router) Get(path string, fn Handler)

func (*Router) Handle

func (rt *Router) Handle(method string, path string, fn Handler)

func (*Router) Head

func (rt *Router) Head(path string, fn Handler)

func (*Router) Options

func (rt *Router) Options(path string, fn Handler)

func (*Router) Patch

func (rt *Router) Patch(path string, fn Handler)

func (*Router) Post

func (rt *Router) Post(path string, fn Handler)

func (*Router) Put

func (rt *Router) Put(path string, fn Handler)

func (*Router) ServeHTTP

func (rt *Router) ServeHTTP(w http.ResponseWriter, r *http.Request)

func (*Router) Trace

func (rt *Router) Trace(path string, fn Handler)

Directories

Path Synopsis
examples

Jump to

Keyboard shortcuts

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