Help us shape the future of Go! Take the 2026 Go Developer Survey

grouter

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

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

Go to latest
Published: Jul 12, 2022 License: MIT Imports: 4 Imported by: 0

README

Grouter

package main

import (
	"context"
	"net/http"

	"github.com/iliubang/grouter"
)

func foo(_ context.Context, w http.ResponseWriter, _ *http.Request, _ map[string]string) {
	w.Header().Add("Content-Type", "application/json")
	w.Write([]byte(`{"code": 0, "data": "hello world"}`))
}

func bar(_ context.Context, w http.ResponseWriter, _ *http.Request, params map[string]string) {
	w.Write([]byte(params["name"]))
}

func main() {
	ctx, cancel := context.WithCancel(context.Background())
	defer cancel()
	r := grouter.NewRouter(ctx)
	r.Get("/index.html", foo)
	r.Get("/aaa/@name:([^/]+)", bar)
	http.ListenAndServe("127.0.0.1:8080", r)
}
......

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// HTTPMETHOD list the supported http methods.
	HTTPMETHOD = map[string]bool{
		"GET":       true,
		"POST":      true,
		"PUT":       true,
		"DELETE":    true,
		"PATCH":     true,
		"OPTIONS":   true,
		"HEAD":      true,
		"TRACE":     true,
		"CONNECT":   true,
		"MKCOL":     true,
		"COPY":      true,
		"MOVE":      true,
		"PROPFIND":  true,
		"PROPPATCH": true,
		"LOCK":      true,
		"UNLOCK":    true,
	}
)
View Source
var (
	NotFound = func(_ context.Context, w http.ResponseWriter, r *http.Request, _ map[string]string) {
		http.NotFound(w, r)
	}
)

Functions

This section is empty.

Types

type HandlerFunc

type HandlerFunc func(ctx context.Context, w http.ResponseWriter, r *http.Request, pathvariables map[string]string)

type Router

type Router struct {
	Rules    []*Rule
	NotFound HandlerFunc
	// contains filtered or unexported fields
}

func NewRouter

func NewRouter(ctx context.Context) *Router

创建一个Router

func (*Router) AddRule

func (router *Router) AddRule(rule *Rule)

func (*Router) Connect

func (router *Router) Connect(path string, handler HandlerFunc)

func (*Router) Delete

func (router *Router) Delete(path string, handler HandlerFunc)

func (*Router) Get

func (router *Router) Get(path string, handler HandlerFunc)

func (*Router) Head

func (router *Router) Head(path string, handler HandlerFunc)

func (*Router) Lookup

func (router *Router) Lookup(method string, uri string) (*Rule, bool)

func (*Router) Option

func (router *Router) Option(path string, handler HandlerFunc)

func (*Router) Post

func (router *Router) Post(path string, handler HandlerFunc)

func (*Router) Put

func (router *Router) Put(path string, handler HandlerFunc)

func (*Router) ServeHTTP

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

implements http.Handler interface.

func (*Router) SetNotFound

func (router *Router) SetNotFound(notFoundHandler HandlerFunc)

type Rule

type Rule struct {
	Method string
	Path   string
	Reg    string

	Params    map[string]string
	ParamKeys []string
	Handler   HandlerFunc
	// contains filtered or unexported fields
}

func NewRule

func NewRule(method string, path string, handler HandlerFunc) *Rule

create a new rule

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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