gin自動路由中介軟體
github地址: https://github.com/chenqinghe/gin-autorouter
gin-autorouter
gin-autorouter is a middleware which could automatic mapping request url to a handler method.
Api
the project have two main functions:
- AutoRouter
- RouterAny
Basic Uage
package main type T struct{ } func (t *T)Greet(c *gin.Context) { c.Writer.WriteString("hello from *T.Greet") } func (t *T)Hello(c *gin.Context) { c.Writer.WriteString("hello from *T.Hello") } func main(){ r:=gin.Default() r.Any("/*path",router.AutoRouter(&T{})) r.Run(":8080") }
you only need to register a router with pattern "/*path"
view http://localhost:8080/greet, you could see "hello from *T.Greet"
view http://localhost:8080/hello, you will see "hello from *T.Hello"
RESTful Api
with AutoRouter, you can create restful api very easily.
package main func (h *Article) Get(c *gin.Context) { articleId := c.Param("id") // search artile stuff..... article := model.SearchArticle(articleId) c.JSONP(http.StatusOK,article) } func (h *Article)Delete(c *gin.Context) { articleId := c.Param("id") model.DeleteArticle(articleId) c.JSONP(http.StatusOK,"ok") } func main(){ r := gin.Default() r.Any("/article/:id",router.AutoRouter(&Article{})) } // * GET /article/123 => *Article.Get // * Delete /article/123 => *Article.Delete
also, you can use RouterAny, things will be extremely easy!!
package main func (h *Article)Get(c *gin.Context, id int) { fmt.Println("article:",id) // output: article: 123 article := model.SearchArticle(id) c.JSONP(http.StatusOK,article) } func (h *Article)Delete(c *gin.Context, id int) { fmt.Println("article:",id) // output: article: 123 model.DeleteArticle(id) c.JSONP(http.StatusOK,"ok") } func main(){ r:= gin.Default() r.Any("/*path",router.RouterAny(&Article{})) } // GET /article/123 => *Article.Get(c, 123) // DELETE /article/123 => *Article.Delete(c, 123)
Mapping Rules
the mapping is basic on *gin.Context.Param("path"). path will be exploded to several segments by '/'
- if path is empty, method is request http method
- the first segment is method
- others will be method arguments
- segments number MUST be equal or greater than method arguments number
- if method is variadic, the segments mapping to last argument could be zero
- otherwise, "404 not found" will be returned
some examples:
path | method | arguments(exclude *gin.Context) |
/ | REQUEST METHOD | nil |
/foo | foo | nil |
/foo/bar | foo | [bar] |
/foo/bar/123 | foo | [bar,123] |
License
the project is under MIT license protected which you can find in LICENSE file.
相關文章
- gin使用中介軟體
- [系列] - go-gin-api 路由中介軟體 - 捕獲異常(四)GoAPI路由
- [系列] - go-gin-api 路由中介軟體 - 日誌記錄(三)GoAPI路由
- 路由中介軟體路由
- gin使用BasicAuth()(驗證)中介軟體
- [系列] - go-gin-api 路由中介軟體 - Jaeger 鏈路追蹤(六)GoAPI路由
- [系列] - go-gin-api 路由中介軟體 - Jaeger 鏈路追蹤(五)GoAPI路由
- 【Gin-API系列】Gin中介軟體之日誌模組(四)API
- go fiber:路由中介軟體Go路由
- 【Gin-API系列】Gin中介軟體之異常處理(六)API
- 【Gin-API系列】Gin中介軟體之鑑權訪問(五)API
- 使用 defineNuxtRouteMiddleware 建立路由中介軟體UX路由
- 基於gin的golang web開發:中介軟體GolangWeb
- go的web框架gin的使用(八):中介軟體GoWeb框架
- gin 註解路由,自動引數繫結工具路由
- API 路由中介軟體的詭異API路由
- 路由的中介軟體執行順序路由
- weblogic中介軟體自動監控告警及分析Web
- Nuxt.js 路由管理:useRouter 方法與路由中介軟體應用UXJS路由
- Redis中介軟體與Web中介軟體RedisWeb
- 初識NodeJS-使用Express框架路由和中介軟體NodeJSExpress框架路由
- 中介軟體之訊息中介軟體-pulsar
- Go Web輕量級框架Gin學習系列:中介軟體使用詳解GoWeb框架
- 用於快取http介面內容的gin高效能中介軟體快取HTTP
- 以中介軟體,路由,跨程式事件的姿勢使用WebSocket路由事件Web
- 資料庫路由中介軟體MyCat - 原始碼篇(15)資料庫路由原始碼
- MySQL中介軟體之ProxySQL(7):詳述ProxySQL的路由規MySql路由
- Flutter Getx 01 - 路由、中介軟體、鑑權、傳值、跳轉Flutter路由
- 啟動靜態檔案中介軟體
- redux中介軟體Redux
- Laravel 中介軟體Laravel
- 中介軟體(middleware)
- Django——中介軟體Django
- ThinkPHP 中介軟體PHP
- 中介軟體漏洞
- 中介軟體-NginxNginx
- MySQL中介軟體MySql
- django中介軟體Django