@@ -65,25 +65,30 @@ type (
65
65
)
66
66
67
67
// NewMux creates a top level mux using the default goa mux implementation.
68
+ // The default versioning handling assumes that the base path for the API is "/api" and the
69
+ // base paths for each version "/:version/api". Use SelectVersion to specify a different scheme
70
+ // if the service exposes a versioned API with different base paths.
68
71
func NewMux () ServeMux {
69
72
return & DefaultMux {
70
73
defaultVersionMux : & defaultVersionMux {
71
74
router : httprouter .New (),
72
75
handles : make (map [string ]HandleFunc ),
73
76
},
74
- selectVersion : PathSelectVersionFunc ("/:version/" ),
77
+ selectVersion : PathSelectVersionFunc ("/:version/" , "api" ),
75
78
}
76
79
}
77
80
78
81
// PathSelectVersionFunc returns a SelectVersionFunc that uses the given path pattern to extract the
79
82
// version from the request path. Use the same path pattern given in the DSL to define the API base
80
83
// path, e.g. "/api/:version".
81
- func PathSelectVersionFunc (pattern string ) SelectVersionFunc {
84
+ // If the pattern matches zeroVersion then the empty version is returned (i.e. the unversioned
85
+ // controller handles the request).
86
+ func PathSelectVersionFunc (pattern , zeroVersion string ) SelectVersionFunc {
82
87
rgs := design .WildcardRegex .ReplaceAllLiteralString (pattern , `/([^/]+)` )
83
88
rg := regexp .MustCompile ("^" + rgs )
84
89
return func (req * http.Request ) (version string ) {
85
90
match := rg .FindStringSubmatch (req .URL .Path )
86
- if len (match ) > 1 {
91
+ if len (match ) > 1 && match [ 1 ] != zeroVersion {
87
92
version = match [1 ]
88
93
}
89
94
return
0 commit comments