doudou6050
2015-04-21 17:41
浏览 176
已采纳

在Golang中处理api版本的惯用方式是什么?

I'm creating a server in Golang intended for a mobile app. I need to be able to support multiple versions of the API for cases were users don't update the app. The main concern with the versioning, is to return data in the correct format for the version of the mobile app.

I've seen that there are three basic ways to do this.
A. One way is by having one route handler on "/", and then allowing that function to parse the url for versioning.
Example:

func main() {
http.HandleFunc("/", routes.ParseFullURI)
}

B. Use a library such as gorilla/mux to handle patterns within the router, but I saw some warnings that this can be too slow.
Example:

  func main() {
            mux.HandleFunc("{version:}/", routes.ParseVersionForHome)
            mux.HandleFunc("{version:}/getData", routes.ParseVersionForGetDAta)
            mux.HandleFunc("{version:}/otherCall", routes.ParseVersionForOtherCall)
            }

C. Have individual urls that don't change, but based on the header, split into different versions. Example:

func main() {
   http.HandleFunc("/", routes.ParseHeaderForVersionForHome)
   http.HandleFunc("/getData", routes.ParseHeaderForVersionForGetData)
   http.HandleFunc("/otherCall", routes.ParseHeaderForVersionForOtherCall)
}

I'm concerned that option 1 will be too messy code wise. I'm concerned that option 2 will be too slow performance wise, and I'm concerned that option 3 will be difficult for the client to handle, or will get confusing since the version isn't clearly labeled.

Which method is the most idiomatic for Golang, and will result in the greatest performance for a mobile app which will be polling often?

图片转代码服务由CSDN问答提供 功能建议

我正在Golang中创建用于移动应用的服务器。 对于用户不更新应用程序的情况,我需要能够支持API的多个版本。 版本控制的主要问题在于,以正确的格式返回移动应用程序版本的数据。

我已经看到完成此操作的三种基本方法。
答:一种方法是在“ /”上使用一个路由处理程序,然后允许该功能 解析网址以进行版本控制。
示例:

  func main(){
http.HandleFunc(“ /”,route.ParseFullURI)
} \  ñ  
 
 

B中。 使用大猩猩/ mux之类的库来处理路由器中的模式,但我看到了一些警告,这可能是 太慢
示例:

  func main(){
 mux.HandleFunc(“ {version:} /”,route.ParseVersionForHome)  
 mux.HandleFunc(“ {version:} / getData”,route.ParseVersionForGetDAta)
 mux.HandleFunc(“ {version:} / otherCall”,route.ParseVersionForOtherCall)
} 
   
 
 

℃。 将单个网址保持不变,但会根据标题将其分为不同的版本。 示例:

  func main(){
 http.HandleFunc(“ /  “,routes.ParseHeaderForVersionForHome)
 http.HandleFunc(” / getData“,routes.ParseHeaderForVersionForGetData)
 http.HandleFunc(” / otherCall“,routes.ParseHeaderForVersionForOtherCall)
} 
   \  n 
 

我担心选项1在代码方面太混乱了。 我担心选项2在性能方面会太慢,并且担心选项3会使客户端难以处理,或者会因为版本未明确标记而变得混乱。

哪种方法是Golang最惯用的方法,它将为经常轮询的移动应用带来最高的性能?

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dty98339 2015-04-21 18:44
    已采纳

    There are many routing frameworks that allow for grouping, for instance with echo (a very good framework if you want speed)

    package main
    
    import "github.com/labstack/echo"
    
    func ping(c *echo.Context) {
            c.String(200, "pong")
    }
    
    func main() {
            e := echo.New()
    
            v1 := e.Group("/v1")
            v1.Get("/ping", ping)
    
            v2 := e.Group("/v2")
            v2.Get("/ping", ping)
    
            e.Run(":4444")
    }
    

    I think this is quite clean.

    I am sure many other frameworks allow for this. I know for a fact martini does, but that is not an idiomatic framework...

    打赏 评论

相关推荐 更多相似问题