planetscale 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (378) hide show
  1. checksums.yaml +7 -0
  2. data/.github/workflows/ci.yml +24 -0
  3. data/.github/workflows/gem-push.yml +38 -0
  4. data/.github/workflows/licensed.yml +43 -0
  5. data/.gitignore +10 -0
  6. data/.licensed.yml +9 -0
  7. data/.licenses/go/github.com/armon/circbuf.dep.yml +31 -0
  8. data/.licenses/go/github.com/gorilla/mux.dep.yml +41 -0
  9. data/.licenses/go/github.com/hashicorp/go-cleanhttp.dep.yml +375 -0
  10. data/.licenses/go/github.com/mitchellh/go-homedir.dep.yml +32 -0
  11. data/.licenses/go/github.com/pkg/errors.dep.yml +36 -0
  12. data/.licenses/go/github.com/planetscale/planetscale-go/planetscale.dep.yml +160 -0
  13. data/.licenses/go/github.com/planetscale/sql-proxy/proxy.dep.yml +180 -0
  14. data/.licenses/go/go.uber.org/atomic.dep.yml +31 -0
  15. data/.licenses/go/go.uber.org/multierr.dep.yml +30 -0
  16. data/.licenses/go/go.uber.org/zap.dep.yml +30 -0
  17. data/.licenses/go/go.uber.org/zap/buffer.dep.yml +30 -0
  18. data/.licenses/go/go.uber.org/zap/internal/bufferpool.dep.yml +30 -0
  19. data/.licenses/go/go.uber.org/zap/internal/color.dep.yml +30 -0
  20. data/.licenses/go/go.uber.org/zap/internal/exit.dep.yml +31 -0
  21. data/.licenses/go/go.uber.org/zap/zapcore.dep.yml +31 -0
  22. data/.licenses/go/golang.org/x/net/context/ctxhttp.dep.yml +63 -0
  23. data/.licenses/go/golang.org/x/oauth2.dep.yml +44 -0
  24. data/.licenses/go/golang.org/x/oauth2/internal.dep.yml +38 -0
  25. data/.ruby_version +1 -0
  26. data/Gemfile +8 -0
  27. data/LICENSE +201 -0
  28. data/README.md +88 -0
  29. data/Rakefile +10 -0
  30. data/bin/console +15 -0
  31. data/bin/setup +17 -0
  32. data/controller.go +255 -0
  33. data/go.mod +16 -0
  34. data/go.sum +428 -0
  35. data/lib/generators/planetscale/install_generator.rb +58 -0
  36. data/lib/planetscale.rb +137 -0
  37. data/lib/planetscale/version.rb +5 -0
  38. data/planetscale.gemspec +44 -0
  39. data/proxy.go +128 -0
  40. data/vendor/github.com/armon/circbuf/.gitignore +22 -0
  41. data/vendor/github.com/armon/circbuf/LICENSE +20 -0
  42. data/vendor/github.com/armon/circbuf/README.md +28 -0
  43. data/vendor/github.com/armon/circbuf/circbuf.go +92 -0
  44. data/vendor/github.com/armon/circbuf/go.mod +1 -0
  45. data/vendor/github.com/golang/protobuf/AUTHORS +3 -0
  46. data/vendor/github.com/golang/protobuf/CONTRIBUTORS +3 -0
  47. data/vendor/github.com/golang/protobuf/LICENSE +28 -0
  48. data/vendor/github.com/golang/protobuf/proto/buffer.go +324 -0
  49. data/vendor/github.com/golang/protobuf/proto/defaults.go +63 -0
  50. data/vendor/github.com/golang/protobuf/proto/deprecated.go +113 -0
  51. data/vendor/github.com/golang/protobuf/proto/discard.go +58 -0
  52. data/vendor/github.com/golang/protobuf/proto/extensions.go +356 -0
  53. data/vendor/github.com/golang/protobuf/proto/properties.go +306 -0
  54. data/vendor/github.com/golang/protobuf/proto/proto.go +167 -0
  55. data/vendor/github.com/golang/protobuf/proto/registry.go +317 -0
  56. data/vendor/github.com/golang/protobuf/proto/text_decode.go +801 -0
  57. data/vendor/github.com/golang/protobuf/proto/text_encode.go +560 -0
  58. data/vendor/github.com/golang/protobuf/proto/wire.go +78 -0
  59. data/vendor/github.com/golang/protobuf/proto/wrappers.go +34 -0
  60. data/vendor/github.com/gorilla/mux/AUTHORS +8 -0
  61. data/vendor/github.com/gorilla/mux/LICENSE +27 -0
  62. data/vendor/github.com/gorilla/mux/README.md +805 -0
  63. data/vendor/github.com/gorilla/mux/doc.go +306 -0
  64. data/vendor/github.com/gorilla/mux/go.mod +3 -0
  65. data/vendor/github.com/gorilla/mux/middleware.go +74 -0
  66. data/vendor/github.com/gorilla/mux/mux.go +606 -0
  67. data/vendor/github.com/gorilla/mux/regexp.go +388 -0
  68. data/vendor/github.com/gorilla/mux/route.go +736 -0
  69. data/vendor/github.com/gorilla/mux/test_helpers.go +19 -0
  70. data/vendor/github.com/hashicorp/go-cleanhttp/LICENSE +363 -0
  71. data/vendor/github.com/hashicorp/go-cleanhttp/README.md +30 -0
  72. data/vendor/github.com/hashicorp/go-cleanhttp/cleanhttp.go +58 -0
  73. data/vendor/github.com/hashicorp/go-cleanhttp/doc.go +20 -0
  74. data/vendor/github.com/hashicorp/go-cleanhttp/go.mod +3 -0
  75. data/vendor/github.com/hashicorp/go-cleanhttp/handlers.go +48 -0
  76. data/vendor/github.com/mitchellh/go-homedir/LICENSE +21 -0
  77. data/vendor/github.com/mitchellh/go-homedir/README.md +14 -0
  78. data/vendor/github.com/mitchellh/go-homedir/go.mod +1 -0
  79. data/vendor/github.com/mitchellh/go-homedir/homedir.go +167 -0
  80. data/vendor/github.com/pkg/errors/.gitignore +24 -0
  81. data/vendor/github.com/pkg/errors/.travis.yml +10 -0
  82. data/vendor/github.com/pkg/errors/LICENSE +23 -0
  83. data/vendor/github.com/pkg/errors/Makefile +44 -0
  84. data/vendor/github.com/pkg/errors/README.md +59 -0
  85. data/vendor/github.com/pkg/errors/appveyor.yml +32 -0
  86. data/vendor/github.com/pkg/errors/errors.go +288 -0
  87. data/vendor/github.com/pkg/errors/go113.go +38 -0
  88. data/vendor/github.com/pkg/errors/stack.go +177 -0
  89. data/vendor/github.com/planetscale/planetscale-go/planetscale/backups.go +139 -0
  90. data/vendor/github.com/planetscale/planetscale-go/planetscale/branches.go +258 -0
  91. data/vendor/github.com/planetscale/planetscale-go/planetscale/certs.go +142 -0
  92. data/vendor/github.com/planetscale/planetscale-go/planetscale/client.go +305 -0
  93. data/vendor/github.com/planetscale/planetscale-go/planetscale/databases.go +131 -0
  94. data/vendor/github.com/planetscale/planetscale-go/planetscale/deploy_requests.go +368 -0
  95. data/vendor/github.com/planetscale/planetscale-go/planetscale/organizations.go +78 -0
  96. data/vendor/github.com/planetscale/planetscale-go/planetscale/service_tokens.go +163 -0
  97. data/vendor/github.com/planetscale/sql-proxy/proxy/client.go +467 -0
  98. data/vendor/github.com/planetscale/sql-proxy/proxy/tls_cache.go +73 -0
  99. data/vendor/go.uber.org/atomic/.codecov.yml +19 -0
  100. data/vendor/go.uber.org/atomic/.gitignore +12 -0
  101. data/vendor/go.uber.org/atomic/.travis.yml +27 -0
  102. data/vendor/go.uber.org/atomic/CHANGELOG.md +76 -0
  103. data/vendor/go.uber.org/atomic/LICENSE.txt +19 -0
  104. data/vendor/go.uber.org/atomic/Makefile +78 -0
  105. data/vendor/go.uber.org/atomic/README.md +63 -0
  106. data/vendor/go.uber.org/atomic/bool.go +81 -0
  107. data/vendor/go.uber.org/atomic/bool_ext.go +53 -0
  108. data/vendor/go.uber.org/atomic/doc.go +23 -0
  109. data/vendor/go.uber.org/atomic/duration.go +82 -0
  110. data/vendor/go.uber.org/atomic/duration_ext.go +40 -0
  111. data/vendor/go.uber.org/atomic/error.go +51 -0
  112. data/vendor/go.uber.org/atomic/error_ext.go +39 -0
  113. data/vendor/go.uber.org/atomic/float64.go +76 -0
  114. data/vendor/go.uber.org/atomic/float64_ext.go +47 -0
  115. data/vendor/go.uber.org/atomic/gen.go +26 -0
  116. data/vendor/go.uber.org/atomic/go.mod +8 -0
  117. data/vendor/go.uber.org/atomic/go.sum +9 -0
  118. data/vendor/go.uber.org/atomic/int32.go +102 -0
  119. data/vendor/go.uber.org/atomic/int64.go +102 -0
  120. data/vendor/go.uber.org/atomic/nocmp.go +35 -0
  121. data/vendor/go.uber.org/atomic/string.go +54 -0
  122. data/vendor/go.uber.org/atomic/string_ext.go +43 -0
  123. data/vendor/go.uber.org/atomic/uint32.go +102 -0
  124. data/vendor/go.uber.org/atomic/uint64.go +102 -0
  125. data/vendor/go.uber.org/atomic/value.go +31 -0
  126. data/vendor/go.uber.org/multierr/.codecov.yml +15 -0
  127. data/vendor/go.uber.org/multierr/.gitignore +4 -0
  128. data/vendor/go.uber.org/multierr/.travis.yml +23 -0
  129. data/vendor/go.uber.org/multierr/CHANGELOG.md +60 -0
  130. data/vendor/go.uber.org/multierr/LICENSE.txt +19 -0
  131. data/vendor/go.uber.org/multierr/Makefile +42 -0
  132. data/vendor/go.uber.org/multierr/README.md +23 -0
  133. data/vendor/go.uber.org/multierr/error.go +449 -0
  134. data/vendor/go.uber.org/multierr/glide.yaml +8 -0
  135. data/vendor/go.uber.org/multierr/go.mod +8 -0
  136. data/vendor/go.uber.org/multierr/go.sum +11 -0
  137. data/vendor/go.uber.org/multierr/go113.go +52 -0
  138. data/vendor/go.uber.org/zap/.codecov.yml +17 -0
  139. data/vendor/go.uber.org/zap/.gitignore +32 -0
  140. data/vendor/go.uber.org/zap/.readme.tmpl +109 -0
  141. data/vendor/go.uber.org/zap/.travis.yml +23 -0
  142. data/vendor/go.uber.org/zap/CHANGELOG.md +432 -0
  143. data/vendor/go.uber.org/zap/CODE_OF_CONDUCT.md +75 -0
  144. data/vendor/go.uber.org/zap/CONTRIBUTING.md +81 -0
  145. data/vendor/go.uber.org/zap/FAQ.md +156 -0
  146. data/vendor/go.uber.org/zap/LICENSE.txt +19 -0
  147. data/vendor/go.uber.org/zap/Makefile +63 -0
  148. data/vendor/go.uber.org/zap/README.md +134 -0
  149. data/vendor/go.uber.org/zap/array.go +320 -0
  150. data/vendor/go.uber.org/zap/buffer/buffer.go +123 -0
  151. data/vendor/go.uber.org/zap/buffer/pool.go +49 -0
  152. data/vendor/go.uber.org/zap/checklicense.sh +17 -0
  153. data/vendor/go.uber.org/zap/config.go +264 -0
  154. data/vendor/go.uber.org/zap/doc.go +113 -0
  155. data/vendor/go.uber.org/zap/encoder.go +79 -0
  156. data/vendor/go.uber.org/zap/error.go +80 -0
  157. data/vendor/go.uber.org/zap/field.go +539 -0
  158. data/vendor/go.uber.org/zap/flag.go +39 -0
  159. data/vendor/go.uber.org/zap/glide.yaml +34 -0
  160. data/vendor/go.uber.org/zap/global.go +168 -0
  161. data/vendor/go.uber.org/zap/global_go112.go +26 -0
  162. data/vendor/go.uber.org/zap/global_prego112.go +26 -0
  163. data/vendor/go.uber.org/zap/go.mod +13 -0
  164. data/vendor/go.uber.org/zap/go.sum +56 -0
  165. data/vendor/go.uber.org/zap/http_handler.go +81 -0
  166. data/vendor/go.uber.org/zap/internal/bufferpool/bufferpool.go +31 -0
  167. data/vendor/go.uber.org/zap/internal/color/color.go +44 -0
  168. data/vendor/go.uber.org/zap/internal/exit/exit.go +64 -0
  169. data/vendor/go.uber.org/zap/level.go +132 -0
  170. data/vendor/go.uber.org/zap/logger.go +344 -0
  171. data/vendor/go.uber.org/zap/options.go +140 -0
  172. data/vendor/go.uber.org/zap/sink.go +161 -0
  173. data/vendor/go.uber.org/zap/stacktrace.go +85 -0
  174. data/vendor/go.uber.org/zap/sugar.go +304 -0
  175. data/vendor/go.uber.org/zap/time.go +27 -0
  176. data/vendor/go.uber.org/zap/writer.go +99 -0
  177. data/vendor/go.uber.org/zap/zapcore/console_encoder.go +161 -0
  178. data/vendor/go.uber.org/zap/zapcore/core.go +113 -0
  179. data/vendor/go.uber.org/zap/zapcore/doc.go +24 -0
  180. data/vendor/go.uber.org/zap/zapcore/encoder.go +443 -0
  181. data/vendor/go.uber.org/zap/zapcore/entry.go +264 -0
  182. data/vendor/go.uber.org/zap/zapcore/error.go +115 -0
  183. data/vendor/go.uber.org/zap/zapcore/field.go +227 -0
  184. data/vendor/go.uber.org/zap/zapcore/hook.go +68 -0
  185. data/vendor/go.uber.org/zap/zapcore/increase_level.go +66 -0
  186. data/vendor/go.uber.org/zap/zapcore/json_encoder.go +534 -0
  187. data/vendor/go.uber.org/zap/zapcore/level.go +175 -0
  188. data/vendor/go.uber.org/zap/zapcore/level_strings.go +46 -0
  189. data/vendor/go.uber.org/zap/zapcore/marshaler.go +61 -0
  190. data/vendor/go.uber.org/zap/zapcore/memory_encoder.go +179 -0
  191. data/vendor/go.uber.org/zap/zapcore/sampler.go +208 -0
  192. data/vendor/go.uber.org/zap/zapcore/tee.go +81 -0
  193. data/vendor/go.uber.org/zap/zapcore/write_syncer.go +123 -0
  194. data/vendor/golang.org/x/net/AUTHORS +3 -0
  195. data/vendor/golang.org/x/net/CONTRIBUTORS +3 -0
  196. data/vendor/golang.org/x/net/LICENSE +27 -0
  197. data/vendor/golang.org/x/net/PATENTS +22 -0
  198. data/vendor/golang.org/x/net/context/context.go +56 -0
  199. data/vendor/golang.org/x/net/context/ctxhttp/ctxhttp.go +71 -0
  200. data/vendor/golang.org/x/net/context/go17.go +73 -0
  201. data/vendor/golang.org/x/net/context/go19.go +21 -0
  202. data/vendor/golang.org/x/net/context/pre_go17.go +301 -0
  203. data/vendor/golang.org/x/net/context/pre_go19.go +110 -0
  204. data/vendor/golang.org/x/oauth2/.travis.yml +13 -0
  205. data/vendor/golang.org/x/oauth2/AUTHORS +3 -0
  206. data/vendor/golang.org/x/oauth2/CONTRIBUTING.md +26 -0
  207. data/vendor/golang.org/x/oauth2/CONTRIBUTORS +3 -0
  208. data/vendor/golang.org/x/oauth2/LICENSE +27 -0
  209. data/vendor/golang.org/x/oauth2/README.md +36 -0
  210. data/vendor/golang.org/x/oauth2/go.mod +9 -0
  211. data/vendor/golang.org/x/oauth2/go.sum +361 -0
  212. data/vendor/golang.org/x/oauth2/internal/client_appengine.go +14 -0
  213. data/vendor/golang.org/x/oauth2/internal/doc.go +6 -0
  214. data/vendor/golang.org/x/oauth2/internal/oauth2.go +37 -0
  215. data/vendor/golang.org/x/oauth2/internal/token.go +294 -0
  216. data/vendor/golang.org/x/oauth2/internal/transport.go +33 -0
  217. data/vendor/golang.org/x/oauth2/oauth2.go +381 -0
  218. data/vendor/golang.org/x/oauth2/token.go +178 -0
  219. data/vendor/golang.org/x/oauth2/transport.go +89 -0
  220. data/vendor/google.golang.org/appengine/LICENSE +202 -0
  221. data/vendor/google.golang.org/appengine/internal/api.go +678 -0
  222. data/vendor/google.golang.org/appengine/internal/api_classic.go +169 -0
  223. data/vendor/google.golang.org/appengine/internal/api_common.go +123 -0
  224. data/vendor/google.golang.org/appengine/internal/app_id.go +28 -0
  225. data/vendor/google.golang.org/appengine/internal/base/api_base.pb.go +308 -0
  226. data/vendor/google.golang.org/appengine/internal/base/api_base.proto +33 -0
  227. data/vendor/google.golang.org/appengine/internal/datastore/datastore_v3.pb.go +4367 -0
  228. data/vendor/google.golang.org/appengine/internal/datastore/datastore_v3.proto +551 -0
  229. data/vendor/google.golang.org/appengine/internal/identity.go +55 -0
  230. data/vendor/google.golang.org/appengine/internal/identity_classic.go +61 -0
  231. data/vendor/google.golang.org/appengine/internal/identity_flex.go +11 -0
  232. data/vendor/google.golang.org/appengine/internal/identity_vm.go +134 -0
  233. data/vendor/google.golang.org/appengine/internal/internal.go +110 -0
  234. data/vendor/google.golang.org/appengine/internal/log/log_service.pb.go +1313 -0
  235. data/vendor/google.golang.org/appengine/internal/log/log_service.proto +150 -0
  236. data/vendor/google.golang.org/appengine/internal/main.go +16 -0
  237. data/vendor/google.golang.org/appengine/internal/main_common.go +7 -0
  238. data/vendor/google.golang.org/appengine/internal/main_vm.go +69 -0
  239. data/vendor/google.golang.org/appengine/internal/metadata.go +60 -0
  240. data/vendor/google.golang.org/appengine/internal/net.go +56 -0
  241. data/vendor/google.golang.org/appengine/internal/regen.sh +40 -0
  242. data/vendor/google.golang.org/appengine/internal/remote_api/remote_api.pb.go +361 -0
  243. data/vendor/google.golang.org/appengine/internal/remote_api/remote_api.proto +44 -0
  244. data/vendor/google.golang.org/appengine/internal/transaction.go +115 -0
  245. data/vendor/google.golang.org/appengine/internal/urlfetch/urlfetch_service.pb.go +527 -0
  246. data/vendor/google.golang.org/appengine/internal/urlfetch/urlfetch_service.proto +64 -0
  247. data/vendor/google.golang.org/appengine/urlfetch/urlfetch.go +210 -0
  248. data/vendor/google.golang.org/protobuf/AUTHORS +3 -0
  249. data/vendor/google.golang.org/protobuf/CONTRIBUTORS +3 -0
  250. data/vendor/google.golang.org/protobuf/LICENSE +27 -0
  251. data/vendor/google.golang.org/protobuf/PATENTS +22 -0
  252. data/vendor/google.golang.org/protobuf/encoding/prototext/decode.go +773 -0
  253. data/vendor/google.golang.org/protobuf/encoding/prototext/doc.go +7 -0
  254. data/vendor/google.golang.org/protobuf/encoding/prototext/encode.go +371 -0
  255. data/vendor/google.golang.org/protobuf/encoding/protowire/wire.go +538 -0
  256. data/vendor/google.golang.org/protobuf/internal/descfmt/stringer.go +318 -0
  257. data/vendor/google.golang.org/protobuf/internal/descopts/options.go +29 -0
  258. data/vendor/google.golang.org/protobuf/internal/detrand/rand.go +69 -0
  259. data/vendor/google.golang.org/protobuf/internal/encoding/defval/default.go +213 -0
  260. data/vendor/google.golang.org/protobuf/internal/encoding/messageset/messageset.go +241 -0
  261. data/vendor/google.golang.org/protobuf/internal/encoding/tag/tag.go +207 -0
  262. data/vendor/google.golang.org/protobuf/internal/encoding/text/decode.go +665 -0
  263. data/vendor/google.golang.org/protobuf/internal/encoding/text/decode_number.go +190 -0
  264. data/vendor/google.golang.org/protobuf/internal/encoding/text/decode_string.go +161 -0
  265. data/vendor/google.golang.org/protobuf/internal/encoding/text/decode_token.go +373 -0
  266. data/vendor/google.golang.org/protobuf/internal/encoding/text/doc.go +29 -0
  267. data/vendor/google.golang.org/protobuf/internal/encoding/text/encode.go +265 -0
  268. data/vendor/google.golang.org/protobuf/internal/errors/errors.go +89 -0
  269. data/vendor/google.golang.org/protobuf/internal/errors/is_go112.go +39 -0
  270. data/vendor/google.golang.org/protobuf/internal/errors/is_go113.go +12 -0
  271. data/vendor/google.golang.org/protobuf/internal/filedesc/build.go +158 -0
  272. data/vendor/google.golang.org/protobuf/internal/filedesc/desc.go +631 -0
  273. data/vendor/google.golang.org/protobuf/internal/filedesc/desc_init.go +471 -0
  274. data/vendor/google.golang.org/protobuf/internal/filedesc/desc_lazy.go +704 -0
  275. data/vendor/google.golang.org/protobuf/internal/filedesc/desc_list.go +450 -0
  276. data/vendor/google.golang.org/protobuf/internal/filedesc/desc_list_gen.go +356 -0
  277. data/vendor/google.golang.org/protobuf/internal/filedesc/placeholder.go +107 -0
  278. data/vendor/google.golang.org/protobuf/internal/filetype/build.go +297 -0
  279. data/vendor/google.golang.org/protobuf/internal/flags/flags.go +24 -0
  280. data/vendor/google.golang.org/protobuf/internal/flags/proto_legacy_disable.go +9 -0
  281. data/vendor/google.golang.org/protobuf/internal/flags/proto_legacy_enable.go +9 -0
  282. data/vendor/google.golang.org/protobuf/internal/genid/any_gen.go +34 -0
  283. data/vendor/google.golang.org/protobuf/internal/genid/api_gen.go +106 -0
  284. data/vendor/google.golang.org/protobuf/internal/genid/descriptor_gen.go +829 -0
  285. data/vendor/google.golang.org/protobuf/internal/genid/doc.go +11 -0
  286. data/vendor/google.golang.org/protobuf/internal/genid/duration_gen.go +34 -0
  287. data/vendor/google.golang.org/protobuf/internal/genid/empty_gen.go +19 -0
  288. data/vendor/google.golang.org/protobuf/internal/genid/field_mask_gen.go +31 -0
  289. data/vendor/google.golang.org/protobuf/internal/genid/goname.go +25 -0
  290. data/vendor/google.golang.org/protobuf/internal/genid/map_entry.go +16 -0
  291. data/vendor/google.golang.org/protobuf/internal/genid/source_context_gen.go +31 -0
  292. data/vendor/google.golang.org/protobuf/internal/genid/struct_gen.go +116 -0
  293. data/vendor/google.golang.org/protobuf/internal/genid/timestamp_gen.go +34 -0
  294. data/vendor/google.golang.org/protobuf/internal/genid/type_gen.go +184 -0
  295. data/vendor/google.golang.org/protobuf/internal/genid/wrappers.go +13 -0
  296. data/vendor/google.golang.org/protobuf/internal/genid/wrappers_gen.go +175 -0
  297. data/vendor/google.golang.org/protobuf/internal/impl/api_export.go +177 -0
  298. data/vendor/google.golang.org/protobuf/internal/impl/checkinit.go +141 -0
  299. data/vendor/google.golang.org/protobuf/internal/impl/codec_extension.go +223 -0
  300. data/vendor/google.golang.org/protobuf/internal/impl/codec_field.go +830 -0
  301. data/vendor/google.golang.org/protobuf/internal/impl/codec_gen.go +5637 -0
  302. data/vendor/google.golang.org/protobuf/internal/impl/codec_map.go +388 -0
  303. data/vendor/google.golang.org/protobuf/internal/impl/codec_map_go111.go +37 -0
  304. data/vendor/google.golang.org/protobuf/internal/impl/codec_map_go112.go +11 -0
  305. data/vendor/google.golang.org/protobuf/internal/impl/codec_message.go +217 -0
  306. data/vendor/google.golang.org/protobuf/internal/impl/codec_messageset.go +123 -0
  307. data/vendor/google.golang.org/protobuf/internal/impl/codec_reflect.go +209 -0
  308. data/vendor/google.golang.org/protobuf/internal/impl/codec_tables.go +557 -0
  309. data/vendor/google.golang.org/protobuf/internal/impl/codec_unsafe.go +17 -0
  310. data/vendor/google.golang.org/protobuf/internal/impl/convert.go +496 -0
  311. data/vendor/google.golang.org/protobuf/internal/impl/convert_list.go +141 -0
  312. data/vendor/google.golang.org/protobuf/internal/impl/convert_map.go +121 -0
  313. data/vendor/google.golang.org/protobuf/internal/impl/decode.go +276 -0
  314. data/vendor/google.golang.org/protobuf/internal/impl/encode.go +201 -0
  315. data/vendor/google.golang.org/protobuf/internal/impl/enum.go +21 -0
  316. data/vendor/google.golang.org/protobuf/internal/impl/extension.go +156 -0
  317. data/vendor/google.golang.org/protobuf/internal/impl/legacy_enum.go +219 -0
  318. data/vendor/google.golang.org/protobuf/internal/impl/legacy_export.go +92 -0
  319. data/vendor/google.golang.org/protobuf/internal/impl/legacy_extension.go +176 -0
  320. data/vendor/google.golang.org/protobuf/internal/impl/legacy_file.go +81 -0
  321. data/vendor/google.golang.org/protobuf/internal/impl/legacy_message.go +558 -0
  322. data/vendor/google.golang.org/protobuf/internal/impl/merge.go +176 -0
  323. data/vendor/google.golang.org/protobuf/internal/impl/merge_gen.go +209 -0
  324. data/vendor/google.golang.org/protobuf/internal/impl/message.go +276 -0
  325. data/vendor/google.golang.org/protobuf/internal/impl/message_reflect.go +465 -0
  326. data/vendor/google.golang.org/protobuf/internal/impl/message_reflect_field.go +543 -0
  327. data/vendor/google.golang.org/protobuf/internal/impl/message_reflect_gen.go +249 -0
  328. data/vendor/google.golang.org/protobuf/internal/impl/pointer_reflect.go +178 -0
  329. data/vendor/google.golang.org/protobuf/internal/impl/pointer_unsafe.go +174 -0
  330. data/vendor/google.golang.org/protobuf/internal/impl/validate.go +576 -0
  331. data/vendor/google.golang.org/protobuf/internal/impl/weak.go +74 -0
  332. data/vendor/google.golang.org/protobuf/internal/order/order.go +89 -0
  333. data/vendor/google.golang.org/protobuf/internal/order/range.go +115 -0
  334. data/vendor/google.golang.org/protobuf/internal/pragma/pragma.go +29 -0
  335. data/vendor/google.golang.org/protobuf/internal/set/ints.go +58 -0
  336. data/vendor/google.golang.org/protobuf/internal/strs/strings.go +196 -0
  337. data/vendor/google.golang.org/protobuf/internal/strs/strings_pure.go +27 -0
  338. data/vendor/google.golang.org/protobuf/internal/strs/strings_unsafe.go +94 -0
  339. data/vendor/google.golang.org/protobuf/internal/version/version.go +79 -0
  340. data/vendor/google.golang.org/protobuf/proto/checkinit.go +71 -0
  341. data/vendor/google.golang.org/protobuf/proto/decode.go +278 -0
  342. data/vendor/google.golang.org/protobuf/proto/decode_gen.go +603 -0
  343. data/vendor/google.golang.org/protobuf/proto/doc.go +94 -0
  344. data/vendor/google.golang.org/protobuf/proto/encode.go +319 -0
  345. data/vendor/google.golang.org/protobuf/proto/encode_gen.go +97 -0
  346. data/vendor/google.golang.org/protobuf/proto/equal.go +167 -0
  347. data/vendor/google.golang.org/protobuf/proto/extension.go +92 -0
  348. data/vendor/google.golang.org/protobuf/proto/merge.go +139 -0
  349. data/vendor/google.golang.org/protobuf/proto/messageset.go +93 -0
  350. data/vendor/google.golang.org/protobuf/proto/proto.go +43 -0
  351. data/vendor/google.golang.org/protobuf/proto/proto_methods.go +19 -0
  352. data/vendor/google.golang.org/protobuf/proto/proto_reflect.go +19 -0
  353. data/vendor/google.golang.org/protobuf/proto/reset.go +43 -0
  354. data/vendor/google.golang.org/protobuf/proto/size.go +97 -0
  355. data/vendor/google.golang.org/protobuf/proto/size_gen.go +55 -0
  356. data/vendor/google.golang.org/protobuf/proto/wrappers.go +29 -0
  357. data/vendor/google.golang.org/protobuf/reflect/protodesc/desc.go +276 -0
  358. data/vendor/google.golang.org/protobuf/reflect/protodesc/desc_init.go +248 -0
  359. data/vendor/google.golang.org/protobuf/reflect/protodesc/desc_resolve.go +286 -0
  360. data/vendor/google.golang.org/protobuf/reflect/protodesc/desc_validate.go +374 -0
  361. data/vendor/google.golang.org/protobuf/reflect/protodesc/proto.go +252 -0
  362. data/vendor/google.golang.org/protobuf/reflect/protoreflect/methods.go +77 -0
  363. data/vendor/google.golang.org/protobuf/reflect/protoreflect/proto.go +504 -0
  364. data/vendor/google.golang.org/protobuf/reflect/protoreflect/source.go +128 -0
  365. data/vendor/google.golang.org/protobuf/reflect/protoreflect/source_gen.go +461 -0
  366. data/vendor/google.golang.org/protobuf/reflect/protoreflect/type.go +665 -0
  367. data/vendor/google.golang.org/protobuf/reflect/protoreflect/value.go +285 -0
  368. data/vendor/google.golang.org/protobuf/reflect/protoreflect/value_pure.go +59 -0
  369. data/vendor/google.golang.org/protobuf/reflect/protoreflect/value_union.go +411 -0
  370. data/vendor/google.golang.org/protobuf/reflect/protoreflect/value_unsafe.go +98 -0
  371. data/vendor/google.golang.org/protobuf/reflect/protoregistry/registry.go +869 -0
  372. data/vendor/google.golang.org/protobuf/runtime/protoiface/legacy.go +15 -0
  373. data/vendor/google.golang.org/protobuf/runtime/protoiface/methods.go +167 -0
  374. data/vendor/google.golang.org/protobuf/runtime/protoimpl/impl.go +44 -0
  375. data/vendor/google.golang.org/protobuf/runtime/protoimpl/version.go +56 -0
  376. data/vendor/google.golang.org/protobuf/types/descriptorpb/descriptor.pb.go +4039 -0
  377. data/vendor/modules.txt +79 -0
  378. metadata +495 -0
@@ -0,0 +1,306 @@
1
+ // Copyright 2012 The Gorilla Authors. All rights reserved.
2
+ // Use of this source code is governed by a BSD-style
3
+ // license that can be found in the LICENSE file.
4
+
5
+ /*
6
+ Package mux implements a request router and dispatcher.
7
+
8
+ The name mux stands for "HTTP request multiplexer". Like the standard
9
+ http.ServeMux, mux.Router matches incoming requests against a list of
10
+ registered routes and calls a handler for the route that matches the URL
11
+ or other conditions. The main features are:
12
+
13
+ * Requests can be matched based on URL host, path, path prefix, schemes,
14
+ header and query values, HTTP methods or using custom matchers.
15
+ * URL hosts, paths and query values can have variables with an optional
16
+ regular expression.
17
+ * Registered URLs can be built, or "reversed", which helps maintaining
18
+ references to resources.
19
+ * Routes can be used as subrouters: nested routes are only tested if the
20
+ parent route matches. This is useful to define groups of routes that
21
+ share common conditions like a host, a path prefix or other repeated
22
+ attributes. As a bonus, this optimizes request matching.
23
+ * It implements the http.Handler interface so it is compatible with the
24
+ standard http.ServeMux.
25
+
26
+ Let's start registering a couple of URL paths and handlers:
27
+
28
+ func main() {
29
+ r := mux.NewRouter()
30
+ r.HandleFunc("/", HomeHandler)
31
+ r.HandleFunc("/products", ProductsHandler)
32
+ r.HandleFunc("/articles", ArticlesHandler)
33
+ http.Handle("/", r)
34
+ }
35
+
36
+ Here we register three routes mapping URL paths to handlers. This is
37
+ equivalent to how http.HandleFunc() works: if an incoming request URL matches
38
+ one of the paths, the corresponding handler is called passing
39
+ (http.ResponseWriter, *http.Request) as parameters.
40
+
41
+ Paths can have variables. They are defined using the format {name} or
42
+ {name:pattern}. If a regular expression pattern is not defined, the matched
43
+ variable will be anything until the next slash. For example:
44
+
45
+ r := mux.NewRouter()
46
+ r.HandleFunc("/products/{key}", ProductHandler)
47
+ r.HandleFunc("/articles/{category}/", ArticlesCategoryHandler)
48
+ r.HandleFunc("/articles/{category}/{id:[0-9]+}", ArticleHandler)
49
+
50
+ Groups can be used inside patterns, as long as they are non-capturing (?:re). For example:
51
+
52
+ r.HandleFunc("/articles/{category}/{sort:(?:asc|desc|new)}", ArticlesCategoryHandler)
53
+
54
+ The names are used to create a map of route variables which can be retrieved
55
+ calling mux.Vars():
56
+
57
+ vars := mux.Vars(request)
58
+ category := vars["category"]
59
+
60
+ Note that if any capturing groups are present, mux will panic() during parsing. To prevent
61
+ this, convert any capturing groups to non-capturing, e.g. change "/{sort:(asc|desc)}" to
62
+ "/{sort:(?:asc|desc)}". This is a change from prior versions which behaved unpredictably
63
+ when capturing groups were present.
64
+
65
+ And this is all you need to know about the basic usage. More advanced options
66
+ are explained below.
67
+
68
+ Routes can also be restricted to a domain or subdomain. Just define a host
69
+ pattern to be matched. They can also have variables:
70
+
71
+ r := mux.NewRouter()
72
+ // Only matches if domain is "www.example.com".
73
+ r.Host("www.example.com")
74
+ // Matches a dynamic subdomain.
75
+ r.Host("{subdomain:[a-z]+}.domain.com")
76
+
77
+ There are several other matchers that can be added. To match path prefixes:
78
+
79
+ r.PathPrefix("/products/")
80
+
81
+ ...or HTTP methods:
82
+
83
+ r.Methods("GET", "POST")
84
+
85
+ ...or URL schemes:
86
+
87
+ r.Schemes("https")
88
+
89
+ ...or header values:
90
+
91
+ r.Headers("X-Requested-With", "XMLHttpRequest")
92
+
93
+ ...or query values:
94
+
95
+ r.Queries("key", "value")
96
+
97
+ ...or to use a custom matcher function:
98
+
99
+ r.MatcherFunc(func(r *http.Request, rm *RouteMatch) bool {
100
+ return r.ProtoMajor == 0
101
+ })
102
+
103
+ ...and finally, it is possible to combine several matchers in a single route:
104
+
105
+ r.HandleFunc("/products", ProductsHandler).
106
+ Host("www.example.com").
107
+ Methods("GET").
108
+ Schemes("http")
109
+
110
+ Setting the same matching conditions again and again can be boring, so we have
111
+ a way to group several routes that share the same requirements.
112
+ We call it "subrouting".
113
+
114
+ For example, let's say we have several URLs that should only match when the
115
+ host is "www.example.com". Create a route for that host and get a "subrouter"
116
+ from it:
117
+
118
+ r := mux.NewRouter()
119
+ s := r.Host("www.example.com").Subrouter()
120
+
121
+ Then register routes in the subrouter:
122
+
123
+ s.HandleFunc("/products/", ProductsHandler)
124
+ s.HandleFunc("/products/{key}", ProductHandler)
125
+ s.HandleFunc("/articles/{category}/{id:[0-9]+}"), ArticleHandler)
126
+
127
+ The three URL paths we registered above will only be tested if the domain is
128
+ "www.example.com", because the subrouter is tested first. This is not
129
+ only convenient, but also optimizes request matching. You can create
130
+ subrouters combining any attribute matchers accepted by a route.
131
+
132
+ Subrouters can be used to create domain or path "namespaces": you define
133
+ subrouters in a central place and then parts of the app can register its
134
+ paths relatively to a given subrouter.
135
+
136
+ There's one more thing about subroutes. When a subrouter has a path prefix,
137
+ the inner routes use it as base for their paths:
138
+
139
+ r := mux.NewRouter()
140
+ s := r.PathPrefix("/products").Subrouter()
141
+ // "/products/"
142
+ s.HandleFunc("/", ProductsHandler)
143
+ // "/products/{key}/"
144
+ s.HandleFunc("/{key}/", ProductHandler)
145
+ // "/products/{key}/details"
146
+ s.HandleFunc("/{key}/details", ProductDetailsHandler)
147
+
148
+ Note that the path provided to PathPrefix() represents a "wildcard": calling
149
+ PathPrefix("/static/").Handler(...) means that the handler will be passed any
150
+ request that matches "/static/*". This makes it easy to serve static files with mux:
151
+
152
+ func main() {
153
+ var dir string
154
+
155
+ flag.StringVar(&dir, "dir", ".", "the directory to serve files from. Defaults to the current dir")
156
+ flag.Parse()
157
+ r := mux.NewRouter()
158
+
159
+ // This will serve files under http://localhost:8000/static/<filename>
160
+ r.PathPrefix("/static/").Handler(http.StripPrefix("/static/", http.FileServer(http.Dir(dir))))
161
+
162
+ srv := &http.Server{
163
+ Handler: r,
164
+ Addr: "127.0.0.1:8000",
165
+ // Good practice: enforce timeouts for servers you create!
166
+ WriteTimeout: 15 * time.Second,
167
+ ReadTimeout: 15 * time.Second,
168
+ }
169
+
170
+ log.Fatal(srv.ListenAndServe())
171
+ }
172
+
173
+ Now let's see how to build registered URLs.
174
+
175
+ Routes can be named. All routes that define a name can have their URLs built,
176
+ or "reversed". We define a name calling Name() on a route. For example:
177
+
178
+ r := mux.NewRouter()
179
+ r.HandleFunc("/articles/{category}/{id:[0-9]+}", ArticleHandler).
180
+ Name("article")
181
+
182
+ To build a URL, get the route and call the URL() method, passing a sequence of
183
+ key/value pairs for the route variables. For the previous route, we would do:
184
+
185
+ url, err := r.Get("article").URL("category", "technology", "id", "42")
186
+
187
+ ...and the result will be a url.URL with the following path:
188
+
189
+ "/articles/technology/42"
190
+
191
+ This also works for host and query value variables:
192
+
193
+ r := mux.NewRouter()
194
+ r.Host("{subdomain}.domain.com").
195
+ Path("/articles/{category}/{id:[0-9]+}").
196
+ Queries("filter", "{filter}").
197
+ HandlerFunc(ArticleHandler).
198
+ Name("article")
199
+
200
+ // url.String() will be "http://news.domain.com/articles/technology/42?filter=gorilla"
201
+ url, err := r.Get("article").URL("subdomain", "news",
202
+ "category", "technology",
203
+ "id", "42",
204
+ "filter", "gorilla")
205
+
206
+ All variables defined in the route are required, and their values must
207
+ conform to the corresponding patterns. These requirements guarantee that a
208
+ generated URL will always match a registered route -- the only exception is
209
+ for explicitly defined "build-only" routes which never match.
210
+
211
+ Regex support also exists for matching Headers within a route. For example, we could do:
212
+
213
+ r.HeadersRegexp("Content-Type", "application/(text|json)")
214
+
215
+ ...and the route will match both requests with a Content-Type of `application/json` as well as
216
+ `application/text`
217
+
218
+ There's also a way to build only the URL host or path for a route:
219
+ use the methods URLHost() or URLPath() instead. For the previous route,
220
+ we would do:
221
+
222
+ // "http://news.domain.com/"
223
+ host, err := r.Get("article").URLHost("subdomain", "news")
224
+
225
+ // "/articles/technology/42"
226
+ path, err := r.Get("article").URLPath("category", "technology", "id", "42")
227
+
228
+ And if you use subrouters, host and path defined separately can be built
229
+ as well:
230
+
231
+ r := mux.NewRouter()
232
+ s := r.Host("{subdomain}.domain.com").Subrouter()
233
+ s.Path("/articles/{category}/{id:[0-9]+}").
234
+ HandlerFunc(ArticleHandler).
235
+ Name("article")
236
+
237
+ // "http://news.domain.com/articles/technology/42"
238
+ url, err := r.Get("article").URL("subdomain", "news",
239
+ "category", "technology",
240
+ "id", "42")
241
+
242
+ Mux supports the addition of middlewares to a Router, which are executed in the order they are added if a match is found, including its subrouters. Middlewares are (typically) small pieces of code which take one request, do something with it, and pass it down to another middleware or the final handler. Some common use cases for middleware are request logging, header manipulation, or ResponseWriter hijacking.
243
+
244
+ type MiddlewareFunc func(http.Handler) http.Handler
245
+
246
+ Typically, the returned handler is a closure which does something with the http.ResponseWriter and http.Request passed to it, and then calls the handler passed as parameter to the MiddlewareFunc (closures can access variables from the context where they are created).
247
+
248
+ A very basic middleware which logs the URI of the request being handled could be written as:
249
+
250
+ func simpleMw(next http.Handler) http.Handler {
251
+ return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
252
+ // Do stuff here
253
+ log.Println(r.RequestURI)
254
+ // Call the next handler, which can be another middleware in the chain, or the final handler.
255
+ next.ServeHTTP(w, r)
256
+ })
257
+ }
258
+
259
+ Middlewares can be added to a router using `Router.Use()`:
260
+
261
+ r := mux.NewRouter()
262
+ r.HandleFunc("/", handler)
263
+ r.Use(simpleMw)
264
+
265
+ A more complex authentication middleware, which maps session token to users, could be written as:
266
+
267
+ // Define our struct
268
+ type authenticationMiddleware struct {
269
+ tokenUsers map[string]string
270
+ }
271
+
272
+ // Initialize it somewhere
273
+ func (amw *authenticationMiddleware) Populate() {
274
+ amw.tokenUsers["00000000"] = "user0"
275
+ amw.tokenUsers["aaaaaaaa"] = "userA"
276
+ amw.tokenUsers["05f717e5"] = "randomUser"
277
+ amw.tokenUsers["deadbeef"] = "user0"
278
+ }
279
+
280
+ // Middleware function, which will be called for each request
281
+ func (amw *authenticationMiddleware) Middleware(next http.Handler) http.Handler {
282
+ return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
283
+ token := r.Header.Get("X-Session-Token")
284
+
285
+ if user, found := amw.tokenUsers[token]; found {
286
+ // We found the token in our map
287
+ log.Printf("Authenticated user %s\n", user)
288
+ next.ServeHTTP(w, r)
289
+ } else {
290
+ http.Error(w, "Forbidden", http.StatusForbidden)
291
+ }
292
+ })
293
+ }
294
+
295
+ r := mux.NewRouter()
296
+ r.HandleFunc("/", handler)
297
+
298
+ amw := authenticationMiddleware{tokenUsers: make(map[string]string)}
299
+ amw.Populate()
300
+
301
+ r.Use(amw.Middleware)
302
+
303
+ Note: The handler chain will be stopped if your middleware doesn't call `next.ServeHTTP()` with the corresponding parameters. This can be used to abort a request if the middleware writer wants to.
304
+
305
+ */
306
+ package mux
@@ -0,0 +1,3 @@
1
+ module github.com/gorilla/mux
2
+
3
+ go 1.12
@@ -0,0 +1,74 @@
1
+ package mux
2
+
3
+ import (
4
+ "net/http"
5
+ "strings"
6
+ )
7
+
8
+ // MiddlewareFunc is a function which receives an http.Handler and returns another http.Handler.
9
+ // Typically, the returned handler is a closure which does something with the http.ResponseWriter and http.Request passed
10
+ // to it, and then calls the handler passed as parameter to the MiddlewareFunc.
11
+ type MiddlewareFunc func(http.Handler) http.Handler
12
+
13
+ // middleware interface is anything which implements a MiddlewareFunc named Middleware.
14
+ type middleware interface {
15
+ Middleware(handler http.Handler) http.Handler
16
+ }
17
+
18
+ // Middleware allows MiddlewareFunc to implement the middleware interface.
19
+ func (mw MiddlewareFunc) Middleware(handler http.Handler) http.Handler {
20
+ return mw(handler)
21
+ }
22
+
23
+ // Use appends a MiddlewareFunc to the chain. Middleware can be used to intercept or otherwise modify requests and/or responses, and are executed in the order that they are applied to the Router.
24
+ func (r *Router) Use(mwf ...MiddlewareFunc) {
25
+ for _, fn := range mwf {
26
+ r.middlewares = append(r.middlewares, fn)
27
+ }
28
+ }
29
+
30
+ // useInterface appends a middleware to the chain. Middleware can be used to intercept or otherwise modify requests and/or responses, and are executed in the order that they are applied to the Router.
31
+ func (r *Router) useInterface(mw middleware) {
32
+ r.middlewares = append(r.middlewares, mw)
33
+ }
34
+
35
+ // CORSMethodMiddleware automatically sets the Access-Control-Allow-Methods response header
36
+ // on requests for routes that have an OPTIONS method matcher to all the method matchers on
37
+ // the route. Routes that do not explicitly handle OPTIONS requests will not be processed
38
+ // by the middleware. See examples for usage.
39
+ func CORSMethodMiddleware(r *Router) MiddlewareFunc {
40
+ return func(next http.Handler) http.Handler {
41
+ return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
42
+ allMethods, err := getAllMethodsForRoute(r, req)
43
+ if err == nil {
44
+ for _, v := range allMethods {
45
+ if v == http.MethodOptions {
46
+ w.Header().Set("Access-Control-Allow-Methods", strings.Join(allMethods, ","))
47
+ }
48
+ }
49
+ }
50
+
51
+ next.ServeHTTP(w, req)
52
+ })
53
+ }
54
+ }
55
+
56
+ // getAllMethodsForRoute returns all the methods from method matchers matching a given
57
+ // request.
58
+ func getAllMethodsForRoute(r *Router, req *http.Request) ([]string, error) {
59
+ var allMethods []string
60
+
61
+ for _, route := range r.routes {
62
+ var match RouteMatch
63
+ if route.Match(req, &match) || match.MatchErr == ErrMethodMismatch {
64
+ methods, err := route.GetMethods()
65
+ if err != nil {
66
+ return nil, err
67
+ }
68
+
69
+ allMethods = append(allMethods, methods...)
70
+ }
71
+ }
72
+
73
+ return allMethods, nil
74
+ }
@@ -0,0 +1,606 @@
1
+ // Copyright 2012 The Gorilla Authors. All rights reserved.
2
+ // Use of this source code is governed by a BSD-style
3
+ // license that can be found in the LICENSE file.
4
+
5
+ package mux
6
+
7
+ import (
8
+ "context"
9
+ "errors"
10
+ "fmt"
11
+ "net/http"
12
+ "path"
13
+ "regexp"
14
+ )
15
+
16
+ var (
17
+ // ErrMethodMismatch is returned when the method in the request does not match
18
+ // the method defined against the route.
19
+ ErrMethodMismatch = errors.New("method is not allowed")
20
+ // ErrNotFound is returned when no route match is found.
21
+ ErrNotFound = errors.New("no matching route was found")
22
+ )
23
+
24
+ // NewRouter returns a new router instance.
25
+ func NewRouter() *Router {
26
+ return &Router{namedRoutes: make(map[string]*Route)}
27
+ }
28
+
29
+ // Router registers routes to be matched and dispatches a handler.
30
+ //
31
+ // It implements the http.Handler interface, so it can be registered to serve
32
+ // requests:
33
+ //
34
+ // var router = mux.NewRouter()
35
+ //
36
+ // func main() {
37
+ // http.Handle("/", router)
38
+ // }
39
+ //
40
+ // Or, for Google App Engine, register it in a init() function:
41
+ //
42
+ // func init() {
43
+ // http.Handle("/", router)
44
+ // }
45
+ //
46
+ // This will send all incoming requests to the router.
47
+ type Router struct {
48
+ // Configurable Handler to be used when no route matches.
49
+ NotFoundHandler http.Handler
50
+
51
+ // Configurable Handler to be used when the request method does not match the route.
52
+ MethodNotAllowedHandler http.Handler
53
+
54
+ // Routes to be matched, in order.
55
+ routes []*Route
56
+
57
+ // Routes by name for URL building.
58
+ namedRoutes map[string]*Route
59
+
60
+ // If true, do not clear the request context after handling the request.
61
+ //
62
+ // Deprecated: No effect, since the context is stored on the request itself.
63
+ KeepContext bool
64
+
65
+ // Slice of middlewares to be called after a match is found
66
+ middlewares []middleware
67
+
68
+ // configuration shared with `Route`
69
+ routeConf
70
+ }
71
+
72
+ // common route configuration shared between `Router` and `Route`
73
+ type routeConf struct {
74
+ // If true, "/path/foo%2Fbar/to" will match the path "/path/{var}/to"
75
+ useEncodedPath bool
76
+
77
+ // If true, when the path pattern is "/path/", accessing "/path" will
78
+ // redirect to the former and vice versa.
79
+ strictSlash bool
80
+
81
+ // If true, when the path pattern is "/path//to", accessing "/path//to"
82
+ // will not redirect
83
+ skipClean bool
84
+
85
+ // Manager for the variables from host and path.
86
+ regexp routeRegexpGroup
87
+
88
+ // List of matchers.
89
+ matchers []matcher
90
+
91
+ // The scheme used when building URLs.
92
+ buildScheme string
93
+
94
+ buildVarsFunc BuildVarsFunc
95
+ }
96
+
97
+ // returns an effective deep copy of `routeConf`
98
+ func copyRouteConf(r routeConf) routeConf {
99
+ c := r
100
+
101
+ if r.regexp.path != nil {
102
+ c.regexp.path = copyRouteRegexp(r.regexp.path)
103
+ }
104
+
105
+ if r.regexp.host != nil {
106
+ c.regexp.host = copyRouteRegexp(r.regexp.host)
107
+ }
108
+
109
+ c.regexp.queries = make([]*routeRegexp, 0, len(r.regexp.queries))
110
+ for _, q := range r.regexp.queries {
111
+ c.regexp.queries = append(c.regexp.queries, copyRouteRegexp(q))
112
+ }
113
+
114
+ c.matchers = make([]matcher, len(r.matchers))
115
+ copy(c.matchers, r.matchers)
116
+
117
+ return c
118
+ }
119
+
120
+ func copyRouteRegexp(r *routeRegexp) *routeRegexp {
121
+ c := *r
122
+ return &c
123
+ }
124
+
125
+ // Match attempts to match the given request against the router's registered routes.
126
+ //
127
+ // If the request matches a route of this router or one of its subrouters the Route,
128
+ // Handler, and Vars fields of the the match argument are filled and this function
129
+ // returns true.
130
+ //
131
+ // If the request does not match any of this router's or its subrouters' routes
132
+ // then this function returns false. If available, a reason for the match failure
133
+ // will be filled in the match argument's MatchErr field. If the match failure type
134
+ // (eg: not found) has a registered handler, the handler is assigned to the Handler
135
+ // field of the match argument.
136
+ func (r *Router) Match(req *http.Request, match *RouteMatch) bool {
137
+ for _, route := range r.routes {
138
+ if route.Match(req, match) {
139
+ // Build middleware chain if no error was found
140
+ if match.MatchErr == nil {
141
+ for i := len(r.middlewares) - 1; i >= 0; i-- {
142
+ match.Handler = r.middlewares[i].Middleware(match.Handler)
143
+ }
144
+ }
145
+ return true
146
+ }
147
+ }
148
+
149
+ if match.MatchErr == ErrMethodMismatch {
150
+ if r.MethodNotAllowedHandler != nil {
151
+ match.Handler = r.MethodNotAllowedHandler
152
+ return true
153
+ }
154
+
155
+ return false
156
+ }
157
+
158
+ // Closest match for a router (includes sub-routers)
159
+ if r.NotFoundHandler != nil {
160
+ match.Handler = r.NotFoundHandler
161
+ match.MatchErr = ErrNotFound
162
+ return true
163
+ }
164
+
165
+ match.MatchErr = ErrNotFound
166
+ return false
167
+ }
168
+
169
+ // ServeHTTP dispatches the handler registered in the matched route.
170
+ //
171
+ // When there is a match, the route variables can be retrieved calling
172
+ // mux.Vars(request).
173
+ func (r *Router) ServeHTTP(w http.ResponseWriter, req *http.Request) {
174
+ if !r.skipClean {
175
+ path := req.URL.Path
176
+ if r.useEncodedPath {
177
+ path = req.URL.EscapedPath()
178
+ }
179
+ // Clean path to canonical form and redirect.
180
+ if p := cleanPath(path); p != path {
181
+
182
+ // Added 3 lines (Philip Schlump) - It was dropping the query string and #whatever from query.
183
+ // This matches with fix in go 1.2 r.c. 4 for same problem. Go Issue:
184
+ // http://code.google.com/p/go/issues/detail?id=5252
185
+ url := *req.URL
186
+ url.Path = p
187
+ p = url.String()
188
+
189
+ w.Header().Set("Location", p)
190
+ w.WriteHeader(http.StatusMovedPermanently)
191
+ return
192
+ }
193
+ }
194
+ var match RouteMatch
195
+ var handler http.Handler
196
+ if r.Match(req, &match) {
197
+ handler = match.Handler
198
+ req = requestWithVars(req, match.Vars)
199
+ req = requestWithRoute(req, match.Route)
200
+ }
201
+
202
+ if handler == nil && match.MatchErr == ErrMethodMismatch {
203
+ handler = methodNotAllowedHandler()
204
+ }
205
+
206
+ if handler == nil {
207
+ handler = http.NotFoundHandler()
208
+ }
209
+
210
+ handler.ServeHTTP(w, req)
211
+ }
212
+
213
+ // Get returns a route registered with the given name.
214
+ func (r *Router) Get(name string) *Route {
215
+ return r.namedRoutes[name]
216
+ }
217
+
218
+ // GetRoute returns a route registered with the given name. This method
219
+ // was renamed to Get() and remains here for backwards compatibility.
220
+ func (r *Router) GetRoute(name string) *Route {
221
+ return r.namedRoutes[name]
222
+ }
223
+
224
+ // StrictSlash defines the trailing slash behavior for new routes. The initial
225
+ // value is false.
226
+ //
227
+ // When true, if the route path is "/path/", accessing "/path" will perform a redirect
228
+ // to the former and vice versa. In other words, your application will always
229
+ // see the path as specified in the route.
230
+ //
231
+ // When false, if the route path is "/path", accessing "/path/" will not match
232
+ // this route and vice versa.
233
+ //
234
+ // The re-direct is a HTTP 301 (Moved Permanently). Note that when this is set for
235
+ // routes with a non-idempotent method (e.g. POST, PUT), the subsequent re-directed
236
+ // request will be made as a GET by most clients. Use middleware or client settings
237
+ // to modify this behaviour as needed.
238
+ //
239
+ // Special case: when a route sets a path prefix using the PathPrefix() method,
240
+ // strict slash is ignored for that route because the redirect behavior can't
241
+ // be determined from a prefix alone. However, any subrouters created from that
242
+ // route inherit the original StrictSlash setting.
243
+ func (r *Router) StrictSlash(value bool) *Router {
244
+ r.strictSlash = value
245
+ return r
246
+ }
247
+
248
+ // SkipClean defines the path cleaning behaviour for new routes. The initial
249
+ // value is false. Users should be careful about which routes are not cleaned
250
+ //
251
+ // When true, if the route path is "/path//to", it will remain with the double
252
+ // slash. This is helpful if you have a route like: /fetch/http://xkcd.com/534/
253
+ //
254
+ // When false, the path will be cleaned, so /fetch/http://xkcd.com/534/ will
255
+ // become /fetch/http/xkcd.com/534
256
+ func (r *Router) SkipClean(value bool) *Router {
257
+ r.skipClean = value
258
+ return r
259
+ }
260
+
261
+ // UseEncodedPath tells the router to match the encoded original path
262
+ // to the routes.
263
+ // For eg. "/path/foo%2Fbar/to" will match the path "/path/{var}/to".
264
+ //
265
+ // If not called, the router will match the unencoded path to the routes.
266
+ // For eg. "/path/foo%2Fbar/to" will match the path "/path/foo/bar/to"
267
+ func (r *Router) UseEncodedPath() *Router {
268
+ r.useEncodedPath = true
269
+ return r
270
+ }
271
+
272
+ // ----------------------------------------------------------------------------
273
+ // Route factories
274
+ // ----------------------------------------------------------------------------
275
+
276
+ // NewRoute registers an empty route.
277
+ func (r *Router) NewRoute() *Route {
278
+ // initialize a route with a copy of the parent router's configuration
279
+ route := &Route{routeConf: copyRouteConf(r.routeConf), namedRoutes: r.namedRoutes}
280
+ r.routes = append(r.routes, route)
281
+ return route
282
+ }
283
+
284
+ // Name registers a new route with a name.
285
+ // See Route.Name().
286
+ func (r *Router) Name(name string) *Route {
287
+ return r.NewRoute().Name(name)
288
+ }
289
+
290
+ // Handle registers a new route with a matcher for the URL path.
291
+ // See Route.Path() and Route.Handler().
292
+ func (r *Router) Handle(path string, handler http.Handler) *Route {
293
+ return r.NewRoute().Path(path).Handler(handler)
294
+ }
295
+
296
+ // HandleFunc registers a new route with a matcher for the URL path.
297
+ // See Route.Path() and Route.HandlerFunc().
298
+ func (r *Router) HandleFunc(path string, f func(http.ResponseWriter,
299
+ *http.Request)) *Route {
300
+ return r.NewRoute().Path(path).HandlerFunc(f)
301
+ }
302
+
303
+ // Headers registers a new route with a matcher for request header values.
304
+ // See Route.Headers().
305
+ func (r *Router) Headers(pairs ...string) *Route {
306
+ return r.NewRoute().Headers(pairs...)
307
+ }
308
+
309
+ // Host registers a new route with a matcher for the URL host.
310
+ // See Route.Host().
311
+ func (r *Router) Host(tpl string) *Route {
312
+ return r.NewRoute().Host(tpl)
313
+ }
314
+
315
+ // MatcherFunc registers a new route with a custom matcher function.
316
+ // See Route.MatcherFunc().
317
+ func (r *Router) MatcherFunc(f MatcherFunc) *Route {
318
+ return r.NewRoute().MatcherFunc(f)
319
+ }
320
+
321
+ // Methods registers a new route with a matcher for HTTP methods.
322
+ // See Route.Methods().
323
+ func (r *Router) Methods(methods ...string) *Route {
324
+ return r.NewRoute().Methods(methods...)
325
+ }
326
+
327
+ // Path registers a new route with a matcher for the URL path.
328
+ // See Route.Path().
329
+ func (r *Router) Path(tpl string) *Route {
330
+ return r.NewRoute().Path(tpl)
331
+ }
332
+
333
+ // PathPrefix registers a new route with a matcher for the URL path prefix.
334
+ // See Route.PathPrefix().
335
+ func (r *Router) PathPrefix(tpl string) *Route {
336
+ return r.NewRoute().PathPrefix(tpl)
337
+ }
338
+
339
+ // Queries registers a new route with a matcher for URL query values.
340
+ // See Route.Queries().
341
+ func (r *Router) Queries(pairs ...string) *Route {
342
+ return r.NewRoute().Queries(pairs...)
343
+ }
344
+
345
+ // Schemes registers a new route with a matcher for URL schemes.
346
+ // See Route.Schemes().
347
+ func (r *Router) Schemes(schemes ...string) *Route {
348
+ return r.NewRoute().Schemes(schemes...)
349
+ }
350
+
351
+ // BuildVarsFunc registers a new route with a custom function for modifying
352
+ // route variables before building a URL.
353
+ func (r *Router) BuildVarsFunc(f BuildVarsFunc) *Route {
354
+ return r.NewRoute().BuildVarsFunc(f)
355
+ }
356
+
357
+ // Walk walks the router and all its sub-routers, calling walkFn for each route
358
+ // in the tree. The routes are walked in the order they were added. Sub-routers
359
+ // are explored depth-first.
360
+ func (r *Router) Walk(walkFn WalkFunc) error {
361
+ return r.walk(walkFn, []*Route{})
362
+ }
363
+
364
+ // SkipRouter is used as a return value from WalkFuncs to indicate that the
365
+ // router that walk is about to descend down to should be skipped.
366
+ var SkipRouter = errors.New("skip this router")
367
+
368
+ // WalkFunc is the type of the function called for each route visited by Walk.
369
+ // At every invocation, it is given the current route, and the current router,
370
+ // and a list of ancestor routes that lead to the current route.
371
+ type WalkFunc func(route *Route, router *Router, ancestors []*Route) error
372
+
373
+ func (r *Router) walk(walkFn WalkFunc, ancestors []*Route) error {
374
+ for _, t := range r.routes {
375
+ err := walkFn(t, r, ancestors)
376
+ if err == SkipRouter {
377
+ continue
378
+ }
379
+ if err != nil {
380
+ return err
381
+ }
382
+ for _, sr := range t.matchers {
383
+ if h, ok := sr.(*Router); ok {
384
+ ancestors = append(ancestors, t)
385
+ err := h.walk(walkFn, ancestors)
386
+ if err != nil {
387
+ return err
388
+ }
389
+ ancestors = ancestors[:len(ancestors)-1]
390
+ }
391
+ }
392
+ if h, ok := t.handler.(*Router); ok {
393
+ ancestors = append(ancestors, t)
394
+ err := h.walk(walkFn, ancestors)
395
+ if err != nil {
396
+ return err
397
+ }
398
+ ancestors = ancestors[:len(ancestors)-1]
399
+ }
400
+ }
401
+ return nil
402
+ }
403
+
404
+ // ----------------------------------------------------------------------------
405
+ // Context
406
+ // ----------------------------------------------------------------------------
407
+
408
+ // RouteMatch stores information about a matched route.
409
+ type RouteMatch struct {
410
+ Route *Route
411
+ Handler http.Handler
412
+ Vars map[string]string
413
+
414
+ // MatchErr is set to appropriate matching error
415
+ // It is set to ErrMethodMismatch if there is a mismatch in
416
+ // the request method and route method
417
+ MatchErr error
418
+ }
419
+
420
+ type contextKey int
421
+
422
+ const (
423
+ varsKey contextKey = iota
424
+ routeKey
425
+ )
426
+
427
+ // Vars returns the route variables for the current request, if any.
428
+ func Vars(r *http.Request) map[string]string {
429
+ if rv := r.Context().Value(varsKey); rv != nil {
430
+ return rv.(map[string]string)
431
+ }
432
+ return nil
433
+ }
434
+
435
+ // CurrentRoute returns the matched route for the current request, if any.
436
+ // This only works when called inside the handler of the matched route
437
+ // because the matched route is stored in the request context which is cleared
438
+ // after the handler returns.
439
+ func CurrentRoute(r *http.Request) *Route {
440
+ if rv := r.Context().Value(routeKey); rv != nil {
441
+ return rv.(*Route)
442
+ }
443
+ return nil
444
+ }
445
+
446
+ func requestWithVars(r *http.Request, vars map[string]string) *http.Request {
447
+ ctx := context.WithValue(r.Context(), varsKey, vars)
448
+ return r.WithContext(ctx)
449
+ }
450
+
451
+ func requestWithRoute(r *http.Request, route *Route) *http.Request {
452
+ ctx := context.WithValue(r.Context(), routeKey, route)
453
+ return r.WithContext(ctx)
454
+ }
455
+
456
+ // ----------------------------------------------------------------------------
457
+ // Helpers
458
+ // ----------------------------------------------------------------------------
459
+
460
+ // cleanPath returns the canonical path for p, eliminating . and .. elements.
461
+ // Borrowed from the net/http package.
462
+ func cleanPath(p string) string {
463
+ if p == "" {
464
+ return "/"
465
+ }
466
+ if p[0] != '/' {
467
+ p = "/" + p
468
+ }
469
+ np := path.Clean(p)
470
+ // path.Clean removes trailing slash except for root;
471
+ // put the trailing slash back if necessary.
472
+ if p[len(p)-1] == '/' && np != "/" {
473
+ np += "/"
474
+ }
475
+
476
+ return np
477
+ }
478
+
479
+ // uniqueVars returns an error if two slices contain duplicated strings.
480
+ func uniqueVars(s1, s2 []string) error {
481
+ for _, v1 := range s1 {
482
+ for _, v2 := range s2 {
483
+ if v1 == v2 {
484
+ return fmt.Errorf("mux: duplicated route variable %q", v2)
485
+ }
486
+ }
487
+ }
488
+ return nil
489
+ }
490
+
491
+ // checkPairs returns the count of strings passed in, and an error if
492
+ // the count is not an even number.
493
+ func checkPairs(pairs ...string) (int, error) {
494
+ length := len(pairs)
495
+ if length%2 != 0 {
496
+ return length, fmt.Errorf(
497
+ "mux: number of parameters must be multiple of 2, got %v", pairs)
498
+ }
499
+ return length, nil
500
+ }
501
+
502
+ // mapFromPairsToString converts variadic string parameters to a
503
+ // string to string map.
504
+ func mapFromPairsToString(pairs ...string) (map[string]string, error) {
505
+ length, err := checkPairs(pairs...)
506
+ if err != nil {
507
+ return nil, err
508
+ }
509
+ m := make(map[string]string, length/2)
510
+ for i := 0; i < length; i += 2 {
511
+ m[pairs[i]] = pairs[i+1]
512
+ }
513
+ return m, nil
514
+ }
515
+
516
+ // mapFromPairsToRegex converts variadic string parameters to a
517
+ // string to regex map.
518
+ func mapFromPairsToRegex(pairs ...string) (map[string]*regexp.Regexp, error) {
519
+ length, err := checkPairs(pairs...)
520
+ if err != nil {
521
+ return nil, err
522
+ }
523
+ m := make(map[string]*regexp.Regexp, length/2)
524
+ for i := 0; i < length; i += 2 {
525
+ regex, err := regexp.Compile(pairs[i+1])
526
+ if err != nil {
527
+ return nil, err
528
+ }
529
+ m[pairs[i]] = regex
530
+ }
531
+ return m, nil
532
+ }
533
+
534
+ // matchInArray returns true if the given string value is in the array.
535
+ func matchInArray(arr []string, value string) bool {
536
+ for _, v := range arr {
537
+ if v == value {
538
+ return true
539
+ }
540
+ }
541
+ return false
542
+ }
543
+
544
+ // matchMapWithString returns true if the given key/value pairs exist in a given map.
545
+ func matchMapWithString(toCheck map[string]string, toMatch map[string][]string, canonicalKey bool) bool {
546
+ for k, v := range toCheck {
547
+ // Check if key exists.
548
+ if canonicalKey {
549
+ k = http.CanonicalHeaderKey(k)
550
+ }
551
+ if values := toMatch[k]; values == nil {
552
+ return false
553
+ } else if v != "" {
554
+ // If value was defined as an empty string we only check that the
555
+ // key exists. Otherwise we also check for equality.
556
+ valueExists := false
557
+ for _, value := range values {
558
+ if v == value {
559
+ valueExists = true
560
+ break
561
+ }
562
+ }
563
+ if !valueExists {
564
+ return false
565
+ }
566
+ }
567
+ }
568
+ return true
569
+ }
570
+
571
+ // matchMapWithRegex returns true if the given key/value pairs exist in a given map compiled against
572
+ // the given regex
573
+ func matchMapWithRegex(toCheck map[string]*regexp.Regexp, toMatch map[string][]string, canonicalKey bool) bool {
574
+ for k, v := range toCheck {
575
+ // Check if key exists.
576
+ if canonicalKey {
577
+ k = http.CanonicalHeaderKey(k)
578
+ }
579
+ if values := toMatch[k]; values == nil {
580
+ return false
581
+ } else if v != nil {
582
+ // If value was defined as an empty string we only check that the
583
+ // key exists. Otherwise we also check for equality.
584
+ valueExists := false
585
+ for _, value := range values {
586
+ if v.MatchString(value) {
587
+ valueExists = true
588
+ break
589
+ }
590
+ }
591
+ if !valueExists {
592
+ return false
593
+ }
594
+ }
595
+ }
596
+ return true
597
+ }
598
+
599
+ // methodNotAllowed replies to the request with an HTTP status code 405.
600
+ func methodNotAllowed(w http.ResponseWriter, r *http.Request) {
601
+ w.WriteHeader(http.StatusMethodNotAllowed)
602
+ }
603
+
604
+ // methodNotAllowedHandler returns a simple request handler
605
+ // that replies to each request with a status code 405.
606
+ func methodNotAllowedHandler() http.Handler { return http.HandlerFunc(methodNotAllowed) }