Re:API design: Which version of versioning is right for you?
by segmondy on Sunday October 06, 2019 @05:57PM
(#21175449)
func metrics(w http.ResponseWriter, r *http.Request) {
}
func AddV1Routes(r *mux.Router) {
r.HandleFunc("/metrics/", metrics).Methods("GET")
r.HandleFunc("/v1func/", v1func).Methods("GET")
}
func AddV2Routes(r *mux.Router) {
r.HandleFunc("/metrics/", metrics).Methods("GET")
r.HandleFunc("/v2func/", v1func).Methods("GET")
}
r := mux.NewRouter()
AddV1Routes(r.PathPrefix("/api/v1").Subrouter())
AddV2Routes(r.PathPrefix("/api/v2").Subrouter())
handler := c.Handler(r)
http.ListenAndServe(":5000", handler
With a pattern like this, you can just decide which functions to group by version.
Also, I really like when one piece of data can serve dual purpose use. In this case, the URL, which is highly visible, contains the version in it. So the one string of data reveals more information to me.