mux

package module
Version: v0.0.9 Latest Latest
Warning

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

Go to latest
Published: Aug 24, 2021 License: GPL-3.0 Imports: 5 Imported by: 0

README

Mux

Mux is a simple and efficient route distributor that supports the net/http interface of the standard library. Routing data is stored in the prefix tree preTree, supported by https://github.com/obity/pretree.

The current version only supports variable routes and simple routes such as /user/:id or /user/login. According to the http request method GET, HEAD, POST, PUT, PATCH, DELETE, CONNECT, OPTIONS, TRACE grouping is handled separately, and the routes between different groups are isolated from each other. Routing variable writing format supports both :id and {id},but the actual storage is :id.

Doc

See this document at GoDoc

Install

go get -u github.com/obity/mux@latest

ToDo

  • 需要获取路由中的{var}变量,handler处理需要这些参数值传输到后端
  • 抽象算法存储引擎,支持自定义算法,只要实现Engine接口就可以自己启用
  • 增加设置路由基础路径功能,统一设置API的版本如"/v1","/2"默认不启用

Example

package main

import (
	"net/http"

	"github.com/obity/mux"
)

func main() {
	m := mux.NewMux()
	m.GET("/pet/findByStatus", Findbystatus)
	m.GET("/pet/{id}", PetHandler)
	m.POST("/user/createWithList", Createwithlist)
	m.DELETE("/user/:username", Userinfo)
	m.Start(":8001")
}

func PetHandler(w http.ResponseWriter, r *http.Request) {
	vars := mux.Vars(r)
	ID := vars["id"]
	w.WriteHeader(http.StatusOK)
	w.Write([]byte("ID:" + ID))
	return
}

func Findbystatus(w http.ResponseWriter, r *http.Request) {
	w.WriteHeader(http.StatusOK)
	w.Write([]byte("FindByStatus matched:" + r.RequestURI))
	return
}

func Createwithlist(w http.ResponseWriter, r *http.Request) {
	w.WriteHeader(http.StatusOK)
	w.Write([]byte("CreateWithList matched:" + r.RequestURI))
	return
}

func Userinfo(w http.ResponseWriter, r *http.Request) {
	w.WriteHeader(http.StatusOK)
	w.Write([]byte("UserInfo matched:" + r.RequestURI))
	return
}

func UserHandler(w http.ResponseWriter, r *http.Request) {
	w.WriteHeader(http.StatusOK)
	w.Write([]byte("UserInfo matched:" + r.RequestURI))
	return
}


Documentation

Overview

Mux是一个简单高效的路由分配器,支持标准库的net/http的接口。路由数据存储在前缀树preTree,由 https://github.com/obity/pretree 提供支持。

目前版本只支持变量路由和简单路由,例如/user/:id或者/user/login。 根据http请求方法GET、HEAD、POST、PUT、PATCH、DELETE、CONNECT、OPTIONS、TRACE分组单独处理,不同分组之间路由相互隔离。 路由变量的书写格式同时支持:id和{id},但实际存储的是:id。

Mux is a simple and efficient route distributor that supports the net/http interface of the standard library.Routing data is stored in the prefix tree preTree, supported by https://github.com/obity/pretree.

The current version only supports variable routes and simple routes such as /user/:id or /user/login. According to the http request method GET, HEAD, POST, PUT, PATCH, DELETE, CONNECT, OPTIONS, TRACE grouping is handled separately, and the routes between different groups are isolated from each other. Routing variable writing format supports both :id and {id},but the actual storage is :id.

Example
package main

import (
	"net/http"

	"github.com/obity/mux"
)

func main() {
	m := mux.NewMux()
	m.GET("/pet/findByStatus", FindbystatusHandler)
	m.GET("/pet/{id}", PetHandler)
	m.POST("/user/createWithList", CreatewithlistHandler)
	m.DELETE("/user/:username", UserinfoHandler)
	m.Start(":8001")
}

func PetHandler(w http.ResponseWriter, r *http.Request) {
	w.WriteHeader(http.StatusOK)
	w.Write([]byte("TestHandler matched:" + r.RequestURI))
	return
}

func FindbystatusHandler(w http.ResponseWriter, r *http.Request) {
	w.WriteHeader(http.StatusOK)
	w.Write([]byte("FindByStatus matched:" + r.RequestURI))
	return
}

func CreatewithlistHandler(w http.ResponseWriter, r *http.Request) {
	w.WriteHeader(http.StatusOK)
	w.Write([]byte("CreateWithList matched:" + r.RequestURI))
	return
}

func UserinfoHandler(w http.ResponseWriter, r *http.Request) {
	w.WriteHeader(http.StatusOK)
	w.Write([]byte("UserInfo matched:" + r.RequestURI))
	return
}
Output:

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func SetVars

func SetVars(r *http.Request, vars map[string]string) *http.Request

把路由变量放到http请求中传递

Route variables are passed in HTTP requests

func Vars

func Vars(r *http.Request) map[string]string

获取路由变量

Get routing variables

Types

type ContextType

type ContextType int
const VarsKey ContextType = iota

路由变量存储在上下文中的key

The key of the routing variable stored in the context

type Mux

type Mux struct {
	RouteGroup    map[string]*route
	StorageEngine storage.Engine
	// contains filtered or unexported fields
}

路由分配器存储结构

Routing distributor storage structure

func NewMux

func NewMux() *Mux

新建路由分配器工厂函数

New route allocator factory function

Example
package main

import (
	"net/http"

	"github.com/obity/mux"
)

func main() {
	m := mux.NewMux()
	m.GET("/pet/findByStatus", FindbystatusHandler)
	m.GET("/pet/{id}", PetHandler)
	m.POST("/user/createWithList", CreatewithlistHandler)
	m.DELETE("/user/:username", UserinfoHandler)
	m.Start(":8001")
}

func PetHandler(w http.ResponseWriter, r *http.Request) {
	w.WriteHeader(http.StatusOK)
	w.Write([]byte("TestHandler matched:" + r.RequestURI))
	return
}

func FindbystatusHandler(w http.ResponseWriter, r *http.Request) {
	w.WriteHeader(http.StatusOK)
	w.Write([]byte("FindByStatus matched:" + r.RequestURI))
	return
}

func CreatewithlistHandler(w http.ResponseWriter, r *http.Request) {
	w.WriteHeader(http.StatusOK)
	w.Write([]byte("CreateWithList matched:" + r.RequestURI))
	return
}

func UserinfoHandler(w http.ResponseWriter, r *http.Request) {
	w.WriteHeader(http.StatusOK)
	w.Write([]byte("UserInfo matched:" + r.RequestURI))
	return
}
Output:

func (*Mux) AppendRoute

func (m *Mux) AppendRoute(method, path string, f func(http.ResponseWriter, *http.Request))

通用新建路由函数

Generic new route function

func (*Mux) CONNECT

func (m *Mux) CONNECT(path string, f func(http.ResponseWriter, *http.Request))

新建CONNECT请求路由

Create a new CONNECT request route

func (*Mux) DELETE

func (m *Mux) DELETE(path string, f func(http.ResponseWriter, *http.Request))

新建DELETE请求路由

Create a new DELETE request route

func (*Mux) Default

func (m *Mux) Default()

启用默认存储引擎

Enable the default storage algorithm engine

func (*Mux) GET

func (m *Mux) GET(path string, f func(http.ResponseWriter, *http.Request))

新建GET请求路由

Create a new GET request route

Example
package main

import (
	"net/http"

	"github.com/obity/mux"
)

func main() {
	m := mux.NewMux()
	m.GET("/user/{id}", UserinfoHandler) // or m.GET("/user/:id", UserinfoHandler)

}

func UserinfoHandler(w http.ResponseWriter, r *http.Request) {
	w.WriteHeader(http.StatusOK)
	w.Write([]byte("UserInfo matched:" + r.RequestURI))
	return
}
Output:

func (*Mux) HEAD

func (m *Mux) HEAD(path string, f func(http.ResponseWriter, *http.Request))

新建HEAD请求路由

Create a new HEAD request route

func (*Mux) OPTIONS

func (m *Mux) OPTIONS(path string, f func(http.ResponseWriter, *http.Request))

新建OPTIONS请求路由

Create a new OPTIONS request route

func (*Mux) PATCH

func (m *Mux) PATCH(path string, f func(http.ResponseWriter, *http.Request))

新建PATCH请求路由

Create a new PATCH request route

func (*Mux) POST

func (m *Mux) POST(path string, f func(http.ResponseWriter, *http.Request))

新建POST请求路由

Create a new POST request route

Example
package main

import (
	"net/http"

	"github.com/obity/mux"
)

func main() {
	m := mux.NewMux()
	m.POST("/account", UserinfoHandler)
	m.POST("/account/:id/address", UserAddressHandler) // or m.POST("/account/{id}/address", UserAddressHandler)
	m.POST("/sendemail", SendEmailandler)
}

func UserinfoHandler(w http.ResponseWriter, r *http.Request) {
	w.WriteHeader(http.StatusOK)
	w.Write([]byte("UserInfo matched:" + r.RequestURI))
	return
}

func UserAddressHandler(w http.ResponseWriter, r *http.Request) {
	w.WriteHeader(http.StatusOK)
	w.Write([]byte("UserInfo matched:" + r.RequestURI))
	return
}

func SendEmailandler(w http.ResponseWriter, r *http.Request) {
	w.WriteHeader(http.StatusOK)
	w.Write([]byte("UserInfo matched:" + r.RequestURI))
	return
}
Output:

func (*Mux) PUT

func (m *Mux) PUT(path string, f func(http.ResponseWriter, *http.Request))

新建PUT请求路由

Create a new PUT request route

func (*Mux) ServeHTTP

func (m *Mux) ServeHTTP(w http.ResponseWriter, r *http.Request)

实现 http.ServerHTTP 接口函数

Implement the http.ServerHTTP interface function

func (*Mux) SetBasePath added in v0.0.6

func (m *Mux) SetBasePath(basePath string) *Mux

设置基础路径,例如API版本"/v1","/v2"

Set the basic path, such as API version "/v1" and "/v2"

Example
package main

import (
	"net/http"

	"github.com/obity/mux"
)

func main() {
	m := mux.NewMux().SetBasePath("/v1")
	m.POST("/account", UserinfoHandler)                 // /v1/account
	m.POST("/account/{id}/address", UserAddressHandler) // /v1/account/{id}/address
	m.POST("/sendemail", SendEmailandler)               // /v1/sendemail
}

func UserinfoHandler(w http.ResponseWriter, r *http.Request) {
	w.WriteHeader(http.StatusOK)
	w.Write([]byte("UserInfo matched:" + r.RequestURI))
	return
}

func UserAddressHandler(w http.ResponseWriter, r *http.Request) {
	w.WriteHeader(http.StatusOK)
	w.Write([]byte("UserInfo matched:" + r.RequestURI))
	return
}

func SendEmailandler(w http.ResponseWriter, r *http.Request) {
	w.WriteHeader(http.StatusOK)
	w.Write([]byte("UserInfo matched:" + r.RequestURI))
	return
}
Output:

func (*Mux) SetEngine

func (m *Mux) SetEngine(e storage.Engine)

修改存储算法引擎

change storage algorithm engine

func (*Mux) Start

func (m *Mux) Start(addr string)

启动http服务器,请在添加完路由最后在启动

Start the http server, please start it at the end after adding the route.

func (*Mux) StartTLS

func (m *Mux) StartTLS(addr, certFile, keyFile string)

启动带tls证书的https服务器,请在添加完路由后在启动

Start the https server with tls certificate, please start it after adding the route

func (*Mux) TRACE

func (m *Mux) TRACE(path string, f func(http.ResponseWriter, *http.Request))

新建TRACE请求路由

Create a new TRACE request route

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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