博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
go web framework gin group api 设计
阅读量:4351 次
发布时间:2019-06-07

本文共 2730 字,大约阅读时间需要 9 分钟。

假如让你来设计group api, 你该怎么设计呢?

group api 和普通api的区别在于前缀不同,如果group api的版本为v1.0 那么相对应的url为/v1.0/xxx, 如果是普通api的话那么api相对应的版本为/xxx

在gin web framework 中设计的原则也是以相对路径来区分。

// RouterGroup is used internally to configure router, a RouterGroup is associated with// a prefix and an array of handlers (middleware).type RouterGroup struct {	Handlers HandlersChain #处理函数	basePath string  #相对路径	engine   *Engine #存在哪个engine上	root     bool  #基于基树判断是否为root}

先看gin中group 的添加规则:

router := gin.New()	users := router.Group("/users")

先看gin.New()默认的basePath是什么。

// New returns a new blank Engine instance without any middleware attached.// By default the configuration is:// - RedirectTrailingSlash:  true// - RedirectFixedPath:      false// - HandleMethodNotAllowed: false// - ForwardedByClientIP:    true// - UseRawPath:             false// - UnescapePathValues:     truefunc New() *Engine {	debugPrintWARNINGNew()	engine := &Engine{		RouterGroup: RouterGroup{			Handlers: nil,			basePath: "/",  #默认为“/”			root:     true,		},		FuncMap:                template.FuncMap{},		RedirectTrailingSlash:  true,		RedirectFixedPath:      false,		HandleMethodNotAllowed: false,		ForwardedByClientIP:    true,		AppEngine:              defaultAppEngine,		UseRawPath:             false,		UnescapePathValues:     true,		MaxMultipartMemory:     defaultMultipartMemory,		trees:                  make(methodTrees, 0, 9),		delims:                 render.Delims{Left: "{
{", Right: "}}"}, secureJsonPrefix: "while(1);", } engine.RouterGroup.engine = engine engine.pool.New = func() interface{} { return engine.allocateContext() } return engine}

  

查看router.Group("/users") 的调用

// Group creates a new router group. You should add all the routes that have common middlewares or the same path prefix.// For example, all the routes that use a common middleware for authorization could be grouped.func (group *RouterGroup) Group(relativePath string, handlers ...HandlerFunc) *RouterGroup {	return &RouterGroup{		Handlers: group.combineHandlers(handlers),		basePath: group.calculateAbsolutePath(relativePath), #根据relativePath这个参数,计算basePath		engine:   group.engine,	}}

 查看group.calculateAbsolutePath(relativePath) 的调用

func (group *RouterGroup) calculateAbsolutePath(relativePath string) string {	return joinPaths(group.basePath, relativePath)}

 再查看joinPaths的实现

func joinPaths(absolutePath, relativePath string) string {	if relativePath == "" { #如果没有relativePath 直接返回absolutePath,默认为“/”		return absolutePath	}        #处理最后的“/”	finalPath := path.Join(absolutePath, relativePath)	appendSlash := lastChar(relativePath) == '/' && lastChar(finalPath) != '/'	if appendSlash {		return finalPath + "/"	}	return finalPath}

  

转载于:https://www.cnblogs.com/Spider-spiders/p/10235516.html

你可能感兴趣的文章
Convert Sorted List to Binary Search Tree
查看>>
Leetcode:Unique Binary Search Trees
查看>>
D3.js 绘制散点图
查看>>
《图解HTTP》
查看>>
python之路_面向对象
查看>>
CSS
查看>>
jvm架构以及Tomcat优化
查看>>
数据库の目录
查看>>
vmware安装rhel 7
查看>>
[复合材料] 编织复合材料单胞周期性边界条件编程问题
查看>>
Paxos协议笔记
查看>>
php之快速入门学习-15(php函数)
查看>>
【01背包问题】
查看>>
我所知道的数据库8-DML语言
查看>>
Python学习笔记——面向对象基础
查看>>
SQL Server 2012安装时如何不安装自带的Visual Studio
查看>>
网络传输协议总结(转载)
查看>>
C#.NET 大型企业信息化系统集成快速开发平台 4.2 版本 - 角色权限的配置页面改进优化...
查看>>
如何编写Spring-Boot自动配置
查看>>
(三)Asp.net web api中的坑-【http post请求中的参数】
查看>>