GetVersion returns the current request version. By default the `GetVersion` will try to read from: - "Accept" header, i.e Accept: "application/json; version=1.0.0" - "Accept-Version" header, i.e Accept-Version: "1.0.0" However, the end developer can also set a custom version for a handler via a mi
(ctx *context.Context)
| 143 | // |
| 144 | // See `SetVersion` too. |
| 145 | func GetVersion(ctx *context.Context) string { |
| 146 | // firstly by context store, if manually set by a middleware. |
| 147 | version := ctx.Values().GetString(ctx.Application().ConfigurationReadOnly().GetVersionContextKey()) |
| 148 | if version != "" { |
| 149 | return version |
| 150 | } |
| 151 | |
| 152 | // secondly by the "Accept-Version" header. |
| 153 | if version = ctx.GetHeader(AcceptVersionHeaderKey); version != "" { |
| 154 | return version |
| 155 | } |
| 156 | |
| 157 | // thirdly by the "Accept" header which is like"...; version=1.0" |
| 158 | acceptValue := ctx.GetHeader(AcceptHeaderKey) |
| 159 | if acceptValue != "" { |
| 160 | if idx := strings.Index(acceptValue, AcceptHeaderVersionValue); idx != -1 { |
| 161 | rem := acceptValue[idx:] |
| 162 | startVersion := strings.Index(rem, "=") |
| 163 | if startVersion == -1 || len(rem) < startVersion+1 { |
| 164 | return "" |
| 165 | } |
| 166 | |
| 167 | rem = rem[startVersion+1:] |
| 168 | |
| 169 | end := strings.Index(rem, " ") |
| 170 | if end == -1 { |
| 171 | end = strings.Index(rem, ";") |
| 172 | } |
| 173 | if end == -1 { |
| 174 | end = len(rem) |
| 175 | } |
| 176 | |
| 177 | if version = rem[:end]; version != "" { |
| 178 | return version |
| 179 | } |
| 180 | } |
| 181 | } |
| 182 | |
| 183 | return "" |
| 184 | } |
| 185 | |
| 186 | // SetVersion force-sets the API Version. |
| 187 | // It can be used inside a middleware. |
searching dependent graphs…