GO自研微服务框架-路由实现

路由实现

1.不用框架

不用框架的路由实现

go 复制代码
package main

import (
   "fmt"
   "log"
   "net/http"
)

func main() {
   http.HandleFunc("/hello", func(writer http.ResponseWriter, request *http.Request) {
      fmt.Fprintf(writer, "%s 欢迎来到goweb教程", "lisus.com")
   })
   err := http.ListenAndServe(":8111", nil)
   if err != nil {
      log.Fatal(err)
   }
}

2.路由实现

go 复制代码
package main

import (
   "log"
   "net/http"
)

type HandleFunc func(w http.ResponseWriter, r *http.Request)
//定义路由结构体
type router struct {
    //定义一个路由处理函数map
   handleFuncMap map[string]HandleFunc
}
//定义引擎
type Engine struct {
   router
}

func (r *router) Add(name string, handleFunc HandleFunc) {
   r.handleFuncMap[name] = handleFunc
}

func New() *Engine {
   return &Engine{
      router: router{handleFuncMap: make(map[string]HandleFunc)},
   }
}
func (e *Engine) Run() {
    //循环处理路由
   for key, value := range e.handleFuncMap {
      http.HandleFunc(key, value)
   }
   err := http.ListenAndServe(":8111", nil)
   if err != nil {
      log.Fatal(err)
   }
}

测试代码

go 复制代码
package main

import (
   "fmt"
   "net/http"
)

func main() {
   engine := New()
   engine.Add("/hello", func(w http.ResponseWriter, r *http.Request) {
      fmt.Fprintf(w, "%s 欢迎学习GO自研框架", "lisus2000")
   })
   engine.Run()
}

3.实现分组路由

go 复制代码
package main

import (
   "log"
   "net/http"
)

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

// 定义路由分组结构
type routerGroup struct {
   name          string
   handleFuncMap map[string]HandleFunc
}

type router struct {
   routerGroups []*routerGroup
}

// Group 分组方法
func (r *router) Group(name string) *routerGroup {
   routerGroup := &routerGroup{
      name:          name,
      handleFuncMap: make(map[string]HandleFunc),
   }
   r.routerGroups = append(r.routerGroups, routerGroup)
   return routerGroup
}

// Add 添加路由
func (r *routerGroup) Add(name string, handleFunc HandleFunc) {
   r.handleFuncMap[name] = handleFunc
}

type Engine struct {
   router
}

func New() *Engine {
   return &Engine{
      router: router{},
   }
}
func (e *Engine) Run() {
   //user key:get value func
   for _, group := range e.routerGroups {
      for key, value := range group.handleFuncMap {
         http.HandleFunc("/"+group.name+key, value)
      }
   }

   err := http.ListenAndServe(":8111", nil)
   if err != nil {
      log.Fatal(err)
   }

测试代码

go 复制代码
package main

import (
   "fmt"
   "net/http"
)

func main() {
   engine := New()
   g := engine.Group("user")
   g.Add("/hello", func(w http.ResponseWriter, r *http.Request) {
      fmt.Fprintf(w, "%s 欢迎学习GO自研框架", "lisus2000")
   })
   g.Add("/info", func(w http.ResponseWriter, r *http.Request) {
      fmt.Fprintf(w, "%s info功能", "lisus2000")
   })
   engine.Run()
}

4.支持不同的请求方式

go 复制代码
package main

import (
   "fmt"
   "log"
   "net/http"
)

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

// 定义路由分组结构
type routerGroup struct {
   name             string
   handleFuncMap    map[string]HandleFunc
   handlerMethodMap map[string][]string
}

type router struct {
   routerGroups []*routerGroup
}

// Group 分组方法
func (r *router) Group(name string) *routerGroup {
   routerGroup := &routerGroup{
      name:             name,
      handleFuncMap:    make(map[string]HandleFunc),
      handlerMethodMap: make(map[string][]string),
   }
   r.routerGroups = append(r.routerGroups, routerGroup)
   return routerGroup
}

// Add 添加路由
func (r *routerGroup) Add(name string, handleFunc HandleFunc) {
   r.handleFuncMap[name] = handleFunc
}
func (r *routerGroup) Any(name string, handleFunc HandleFunc) {
   r.handleFuncMap[name] = handleFunc
   r.handlerMethodMap["ANY"] = append(r.handlerMethodMap["ANY"], name)
}
func (r *routerGroup) Get(name string, handleFunc HandleFunc) {
   r.handleFuncMap[name] = handleFunc
   r.handlerMethodMap[http.MethodGet] = append(r.handlerMethodMap[http.MethodGet], name)
}
func (r *routerGroup) Post(name string, handleFunc HandleFunc) {
   r.handleFuncMap[name] = handleFunc
   r.handlerMethodMap[http.MethodPost] = append(r.handlerMethodMap[http.MethodPost], name)
}

type Engine struct {
   router
}

func New() *Engine {
   return &Engine{
      router: router{},
   }
}
func (e *Engine) ServeHTTP(w http.ResponseWriter, r *http.Request) {
   method := r.Method
   for _, group := range e.routerGroups {
      for name, methodHandle := range group.handleFuncMap {
         url := "/" + group.name + name
         if r.RequestURI == url {
            routers, ok := group.handlerMethodMap["ANY"]
            if ok {
               for _, routerName := range routers {
                  if routerName == name {
                     methodHandle(w, r)
                     return
                  }
               }
            }
            //method 进行匹配
            routers, ok = group.handlerMethodMap[method]
            if ok {
               for _, routerName := range routers {
                  if routerName == name {
                     methodHandle(w, r)
                     return
                  }
               }
            }
            w.WriteHeader(http.StatusMethodNotAllowed)
            fmt.Fprintf(w, "%s %s not allow\n", r.RequestURI, method)
            return
         }
      }
   }
   w.WriteHeader(http.StatusNotFound)
   fmt.Fprintf(w, "%s not found\n", r.RequestURI)
}
func (e *Engine) Run() {
   http.Handle("/", e)
   err := http.ListenAndServe(":8111", nil)
   if err != nil {
      log.Fatal(err)
   }
}

测试代码

go 复制代码
package main

import (
   "fmt"
   "net/http"
)

func main() {
   engine := New()
   g := engine.Group("user")
   g.Get("/hello", func(w http.ResponseWriter, r *http.Request) {
      fmt.Fprintf(w, "%s 欢迎学习GO自研框架", "lisus2000")
   })
   g.Post("/info", func(w http.ResponseWriter, r *http.Request) {
      fmt.Fprintf(w, "%s info功能", "lisus2000")
   })
   engine.Run()
}

5.支持同一个路径不同请求方式

go 复制代码
package main

import "net/http"

type Context struct {
   W http.ResponseWriter
   R *http.Request
}
go 复制代码
package main

import (
   "fmt"
   "log"
   "net/http"
)

const ANY = "ANY"

type HandleFunc func(ctx *Context)

// 定义路由分组结构
type routerGroup struct {
   name             string
   handleFuncMap    map[string]map[string]HandleFunc
   handlerMethodMap map[string][]string
}

type router struct {
   routerGroups []*routerGroup
}

// Group 分组方法
func (r *router) Group(name string) *routerGroup {
   routerGroup := &routerGroup{
      name:             name,
      handleFuncMap:    make(map[string]map[string]HandleFunc),
      handlerMethodMap: make(map[string][]string),
   }
   r.routerGroups = append(r.routerGroups, routerGroup)
   return routerGroup
}

func (r *routerGroup) handle(name string, method string, handleFunc HandleFunc) {
   _, ok := r.handleFuncMap[name]
   if !ok {
      r.handleFuncMap[name] = make(map[string]HandleFunc)
   }
   _, ok = r.handleFuncMap[name][method]
   if ok {
      panic("有重复的路由")
   }
   r.handleFuncMap[name][method] = handleFunc
   //r.handlerMethodMap[method] = append(r.handlerMethodMap[method], name)
}

func (r *routerGroup) Any(name string, handleFunc HandleFunc) {
   r.handle(name, ANY, handleFunc)
}
func (r *routerGroup) Get(name string, handleFunc HandleFunc) {
   r.handle(name, http.MethodGet, handleFunc)
}
func (r *routerGroup) Post(name string, handleFunc HandleFunc) {
   r.handle(name, http.MethodPost, handleFunc)
}

type Engine struct {
   router
}

func New() *Engine {
   return &Engine{
      router: router{},
   }
}
func (e *Engine) ServeHTTP(w http.ResponseWriter, r *http.Request) {
   method := r.Method
   for _, group := range e.routerGroups {
      for name, methodHandle := range group.handleFuncMap {
         url := "/" + group.name + name
         if r.RequestURI == url {
            ctx := &Context{
               W: w,
               R: r,
            }
            handle, ok := methodHandle[ANY]
            if ok {
               handle(ctx)
               return
            }

            handle, ok = methodHandle[method]
            if ok {
               handle(ctx)
               return
            }
            //method 进行匹配
            w.WriteHeader(http.StatusMethodNotAllowed)
            fmt.Fprintf(w, "%s %s not allow\n", r.RequestURI, method)
            return

         }
      }
   }
   w.WriteHeader(http.StatusNotFound)
   fmt.Fprintf(w, "%s not found\n", r.RequestURI)
}
func (e *Engine) Run() {
   http.Handle("/", e)
   err := http.ListenAndServe(":8111", nil)
   if err != nil {
      log.Fatal(err)
   }
}

测试代码

go 复制代码
package main

import (
   "fmt"
)

func main() {
   engine := New()
   g := engine.Group("user")
   g.Get("/hello", func(ctx *Context) {
      fmt.Fprintf(ctx.W, "%s Get欢迎学习GO自研框架", "lisus2000")
   })
   g.Post("/hello", func(ctx *Context) {
      fmt.Fprintf(ctx.W, "%s Post欢迎学习GO自研框架", "lisus2000")
   })
   g.Post("/info", func(ctx *Context) {
      fmt.Fprintf(ctx.W, "%s info功能", "lisus2000")
   })
   engine.Run()
}

6.前缀树

在前面实现的时候,我们的路径匹配实现的很简陋,不能实现更为复杂的需求,比如/user/get/:id 这种带有参数的,这种带有参数的路径,我们称之为动态路由

除了带有参数的,一般情况下,我们可能还希望支持通配符**,比如/static/**, 可以匹配/static/vue.js或者/static/css/index.css这些。

实现代码

go 复制代码
package msgo

import "strings"

type treeNode struct {
	name       string
	children   []*treeNode
	routerName string
	isEnd      bool
}

//put path: /user/get/:id

func (t *treeNode) Put(path string) {
	root := t
	strs := strings.Split(path, "/")
	for index, name := range strs {
		if index == 0 {
			continue
		}
		children := t.children
		isMatch := false
		for _, node := range children {
			if node.name == name {
				isMatch = true
				t = node
				break
			}
		}
		if !isMatch {
			isEnd := false
			if index == len(strs)-1 {
				isEnd = true
			}
			node := &treeNode{name: name, children: make([]*treeNode, 0), isEnd: isEnd}
			children = append(children, node)
			t.children = children
			t = node
		}

	}
	t = root
}

//get path:/user/get/11
// hello

func (t *treeNode) Get(path string) *treeNode {
	strs := strings.Split(path, "/")
	routerName := ""
	for index, name := range strs {
		if index == 0 {
			continue
		}
		children := t.children
		isMatch := false
		for _, node := range children {
			if node.name == name || node.name == "*" ||
				strings.Contains(node.name, ":") {

				isMatch = true
				routerName += "/" + node.name
				node.routerName = routerName
				t = node
				//到达最尾部结点
				if index == len(strs)-1 {
					return node
				}
				break
			}
		}
		if !isMatch {
			for _, node := range children {
				// /user/**
				// /user/get/userinfo
				// /user/aa/bb
				if node.name == "**" {
					routerName += "/" + node.name
					node.routerName = routerName
					return node
				}
			}
		}
	}
	return nil
}

测试代码

go 复制代码
package msgo

import (
   "fmt"
   "testing"
)

func TestTreeNode(t *testing.T) {
   root := &treeNode{
      name:     "/",
      children: make([]*treeNode, 0),
   }
   root.Put("/user/get/:id")
   root.Put("/user/create/hello")
   root.Put("/user/create/aaa")
   root.Put("/order/get/aaa")

   node := root.Get("/user/get/1")
   fmt.Println(node)

   node = root.Get("/user/create/hello")
   fmt.Println(node)

   node = root.Get("/user/create/aaa")
   fmt.Println(node)

   node = root.Get("/order/get/aaa")
   fmt.Println(node)

}

6.1适配前缀树

实现代码

go 复制代码
package msgo

import (
	"fmt"
	"log"
	"net/http"
)

const ANY = "ANY"

type HandleFunc func(ctx *Context)

// 定义路由分组结构
type routerGroup struct {
	name             string
	handleFuncMap    map[string]map[string]HandleFunc
	handlerMethodMap map[string][]string
	treeNode         *treeNode
}

type router struct {
	routerGroups []*routerGroup
}

// Group 分组方法
func (r *router) Group(name string) *routerGroup {
	routerGroup := &routerGroup{
		name:             name,
		handleFuncMap:    make(map[string]map[string]HandleFunc),
		handlerMethodMap: make(map[string][]string),
		treeNode:         &treeNode{name: "/", children: make([]*treeNode, 0)},
	}
	r.routerGroups = append(r.routerGroups, routerGroup)
	return routerGroup
}

func (r *routerGroup) handle(name string, method string, handleFunc HandleFunc) {
	_, ok := r.handleFuncMap[name]
	if !ok {
		r.handleFuncMap[name] = make(map[string]HandleFunc)
	}
	_, ok = r.handleFuncMap[name][method]
	if ok {
		panic("有重复的路由")
	}
	r.handleFuncMap[name][method] = handleFunc
	r.treeNode.Put(name)
}

func (r *routerGroup) Any(name string, handleFunc HandleFunc) {
	r.handle(name, ANY, handleFunc)
}
func (r *routerGroup) Get(name string, handleFunc HandleFunc) {
	r.handle(name, http.MethodGet, handleFunc)
}
func (r *routerGroup) Post(name string, handleFunc HandleFunc) {
	r.handle(name, http.MethodPost, handleFunc)
}
func (r *routerGroup) Delete(name string, handlerFunc HandleFunc) {
	r.handle(name, http.MethodDelete, handlerFunc)
}
func (r *routerGroup) Put(name string, handlerFunc HandleFunc) {
	r.handle(name, http.MethodPut, handlerFunc)
}
func (r *routerGroup) Patch(name string, handlerFunc HandleFunc) {
	r.handle(name, http.MethodPatch, handlerFunc)
}
func (r *routerGroup) Options(name string, handlerFunc HandleFunc) {
	r.handle(name, http.MethodOptions, handlerFunc)
}
func (r *routerGroup) Head(name string, handlerFunc HandleFunc) {
	r.handle(name, http.MethodHead, handlerFunc)
}

type Engine struct {
	router
}

func New() *Engine {
	return &Engine{
		router: router{},
	}
}
func (e *Engine) ServeHTTP(w http.ResponseWriter, r *http.Request) {
	method := r.Method
	for _, group := range e.routerGroups {
		routerName := SubStringLast(r.RequestURI, "/"+group.name)
		// get/1
		node := group.treeNode.Get(routerName)
		if node != nil && node.isEnd {
			//路由匹配上了
			ctx := &Context{
				W: w,
				R: r,
			}
			handle, ok := group.handleFuncMap[node.routerName][ANY]
			if ok {
				handle(ctx)
				return
			}

			handle, ok = group.handleFuncMap[node.routerName][method]
			if ok {
				handle(ctx)
				return
			}
			//method 进行匹配
			w.WriteHeader(http.StatusMethodNotAllowed)
			fmt.Fprintf(w, "%s %s not allow\n", r.RequestURI, method)
			return
		}
	}
	w.WriteHeader(http.StatusNotFound)
	fmt.Fprintf(w, "%s not found\n", r.RequestURI)
}
func (e *Engine) Run() {
	http.Handle("/", e)
	err := http.ListenAndServe(":8111", nil)
	if err != nil {
		log.Fatal(err)
	}
}

测试代码

go 复制代码
package main

import (
	"fmt"
	"msgo"
)

func main() {
	engine := msgo.New()
	g := engine.Group("user")
	//g.Get("/hello", func(ctx *msgo.Context) {
	//	fmt.Fprintf(ctx.W, "%s Get欢迎学习GO自研框架", "lisus2000")
	//})
	g.Get("/hello/get", func(ctx *msgo.Context) {
		fmt.Fprintf(ctx.W, "%s Get欢迎学习GO自研框架", "lisus2000")
	})
	g.Post("/hello", func(ctx *msgo.Context) {
		fmt.Fprintf(ctx.W, "%s Post欢迎学习GO自研框架", "lisus2000")
	})
	g.Post("/info", func(ctx *msgo.Context) {
		fmt.Fprintf(ctx.W, "%s info功能", "lisus2000")
	})
	g.Get("/get/:id", func(ctx *msgo.Context) {
		fmt.Fprintf(ctx.W, "%s get user 路径变量的值", "lisus2000")
	})
	engine.Run()
}

il)

if err != nil {

log.Fatal(err)

}

}

测试代码

```go
package main

import (
	"fmt"
	"msgo"
)

func main() {
	engine := msgo.New()
	g := engine.Group("user")
	//g.Get("/hello", func(ctx *msgo.Context) {
	//	fmt.Fprintf(ctx.W, "%s Get欢迎学习GO自研框架", "lisus2000")
	//})
	g.Get("/hello/get", func(ctx *msgo.Context) {
		fmt.Fprintf(ctx.W, "%s Get欢迎学习GO自研框架", "lisus2000")
	})
	g.Post("/hello", func(ctx *msgo.Context) {
		fmt.Fprintf(ctx.W, "%s Post欢迎学习GO自研框架", "lisus2000")
	})
	g.Post("/info", func(ctx *msgo.Context) {
		fmt.Fprintf(ctx.W, "%s info功能", "lisus2000")
	})
	g.Get("/get/:id", func(ctx *msgo.Context) {
		fmt.Fprintf(ctx.W, "%s get user 路径变量的值", "lisus2000")
	})
	engine.Run()
}
相关推荐
lvbu_2024war0112 分钟前
MATLAB语言的网络编程
开发语言·后端·golang
Bony-2 小时前
Go语言反射从入门到进阶
开发语言·后端·golang
每天写点bug2 小时前
【golang】map遍历注意事项
开发语言·算法·golang
dbcat官方3 小时前
2.微服务灰度发布落地实践(agent实现)
微服务·云原生·架构
tatasix6 小时前
Go Redis实现排行榜
开发语言·redis·golang
Bony-8 小时前
Go语言高并发实战案例分析
开发语言·后端·golang
ac-er88889 小时前
Golang并发机制以及它所使⽤的CSP并发模型
开发语言·后端·golang
Pandaconda9 小时前
【Golang 面试题】每日 3 题(六)
开发语言·笔记·后端·面试·职场和发展·golang·go
C182981825759 小时前
微服务面试题
微服务·架构
峰子201211 小时前
Go语言实现守护进程的挑战
开发语言·后端·面试·架构·golang·go