planetscale 0.3.1 → 0.3.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (368) hide show
  1. checksums.yaml +4 -4
  2. data/.github/workflows/ci.yml +9 -1
  3. data/.github/workflows/gem-push.yml +9 -2
  4. data/.licenses/go/github.com/matoous/go-nanoid/v2.dep.yml +34 -0
  5. data/.licenses/go/github.com/planetscale/planetscale-go/planetscale.dep.yml +1 -1
  6. data/.licenses/go/github.com/planetscale/sql-proxy/proxy.dep.yml +1 -1
  7. data/.licenses/go/go.uber.org/zap/buffer.dep.yml +1 -1
  8. data/.licenses/go/go.uber.org/zap/internal/bufferpool.dep.yml +1 -1
  9. data/.licenses/go/go.uber.org/zap/internal/color.dep.yml +1 -1
  10. data/.licenses/go/go.uber.org/zap/internal/exit.dep.yml +1 -1
  11. data/.licenses/go/go.uber.org/zap/zapcore.dep.yml +1 -1
  12. data/.licenses/go/go.uber.org/zap.dep.yml +1 -1
  13. data/README.md +2 -0
  14. data/controller.go +21 -36
  15. data/go.mod +4 -3
  16. data/go.sum +32 -18
  17. data/lib/planetscale/version.rb +1 -1
  18. data/lib/planetscale.rb +2 -3
  19. data/planetscale.gemspec +1 -1
  20. data/proxy/planetscale-darwin.h +1 -1
  21. data/proxy/planetscale-darwin.so +0 -0
  22. data/proxy/planetscale-linux.h +1 -1
  23. data/proxy/planetscale-linux.so +0 -0
  24. data/proxy.go +1 -5
  25. metadata +3 -345
  26. data/vendor/github.com/armon/circbuf/.gitignore +0 -22
  27. data/vendor/github.com/armon/circbuf/LICENSE +0 -20
  28. data/vendor/github.com/armon/circbuf/README.md +0 -28
  29. data/vendor/github.com/armon/circbuf/circbuf.go +0 -92
  30. data/vendor/github.com/armon/circbuf/go.mod +0 -1
  31. data/vendor/github.com/golang/protobuf/AUTHORS +0 -3
  32. data/vendor/github.com/golang/protobuf/CONTRIBUTORS +0 -3
  33. data/vendor/github.com/golang/protobuf/LICENSE +0 -28
  34. data/vendor/github.com/golang/protobuf/proto/buffer.go +0 -324
  35. data/vendor/github.com/golang/protobuf/proto/defaults.go +0 -63
  36. data/vendor/github.com/golang/protobuf/proto/deprecated.go +0 -113
  37. data/vendor/github.com/golang/protobuf/proto/discard.go +0 -58
  38. data/vendor/github.com/golang/protobuf/proto/extensions.go +0 -356
  39. data/vendor/github.com/golang/protobuf/proto/properties.go +0 -306
  40. data/vendor/github.com/golang/protobuf/proto/proto.go +0 -167
  41. data/vendor/github.com/golang/protobuf/proto/registry.go +0 -317
  42. data/vendor/github.com/golang/protobuf/proto/text_decode.go +0 -801
  43. data/vendor/github.com/golang/protobuf/proto/text_encode.go +0 -560
  44. data/vendor/github.com/golang/protobuf/proto/wire.go +0 -78
  45. data/vendor/github.com/golang/protobuf/proto/wrappers.go +0 -34
  46. data/vendor/github.com/gorilla/mux/AUTHORS +0 -8
  47. data/vendor/github.com/gorilla/mux/LICENSE +0 -27
  48. data/vendor/github.com/gorilla/mux/README.md +0 -805
  49. data/vendor/github.com/gorilla/mux/doc.go +0 -306
  50. data/vendor/github.com/gorilla/mux/go.mod +0 -3
  51. data/vendor/github.com/gorilla/mux/middleware.go +0 -74
  52. data/vendor/github.com/gorilla/mux/mux.go +0 -606
  53. data/vendor/github.com/gorilla/mux/regexp.go +0 -388
  54. data/vendor/github.com/gorilla/mux/route.go +0 -736
  55. data/vendor/github.com/gorilla/mux/test_helpers.go +0 -19
  56. data/vendor/github.com/hashicorp/go-cleanhttp/LICENSE +0 -363
  57. data/vendor/github.com/hashicorp/go-cleanhttp/README.md +0 -30
  58. data/vendor/github.com/hashicorp/go-cleanhttp/cleanhttp.go +0 -58
  59. data/vendor/github.com/hashicorp/go-cleanhttp/doc.go +0 -20
  60. data/vendor/github.com/hashicorp/go-cleanhttp/go.mod +0 -3
  61. data/vendor/github.com/hashicorp/go-cleanhttp/handlers.go +0 -48
  62. data/vendor/github.com/mitchellh/go-homedir/LICENSE +0 -21
  63. data/vendor/github.com/mitchellh/go-homedir/README.md +0 -14
  64. data/vendor/github.com/mitchellh/go-homedir/go.mod +0 -1
  65. data/vendor/github.com/mitchellh/go-homedir/homedir.go +0 -167
  66. data/vendor/github.com/pkg/errors/.gitignore +0 -24
  67. data/vendor/github.com/pkg/errors/.travis.yml +0 -10
  68. data/vendor/github.com/pkg/errors/LICENSE +0 -23
  69. data/vendor/github.com/pkg/errors/Makefile +0 -44
  70. data/vendor/github.com/pkg/errors/README.md +0 -59
  71. data/vendor/github.com/pkg/errors/appveyor.yml +0 -32
  72. data/vendor/github.com/pkg/errors/errors.go +0 -288
  73. data/vendor/github.com/pkg/errors/go113.go +0 -38
  74. data/vendor/github.com/pkg/errors/stack.go +0 -177
  75. data/vendor/github.com/planetscale/planetscale-go/LICENSE +0 -202
  76. data/vendor/github.com/planetscale/planetscale-go/planetscale/audit_logs.go +0 -136
  77. data/vendor/github.com/planetscale/planetscale-go/planetscale/backups.go +0 -139
  78. data/vendor/github.com/planetscale/planetscale-go/planetscale/branches.go +0 -252
  79. data/vendor/github.com/planetscale/planetscale-go/planetscale/certs.go +0 -166
  80. data/vendor/github.com/planetscale/planetscale-go/planetscale/client.go +0 -312
  81. data/vendor/github.com/planetscale/planetscale-go/planetscale/databases.go +0 -133
  82. data/vendor/github.com/planetscale/planetscale-go/planetscale/deploy_requests.go +0 -368
  83. data/vendor/github.com/planetscale/planetscale-go/planetscale/organizations.go +0 -103
  84. data/vendor/github.com/planetscale/planetscale-go/planetscale/regions.go +0 -52
  85. data/vendor/github.com/planetscale/planetscale-go/planetscale/service_tokens.go +0 -163
  86. data/vendor/github.com/planetscale/sql-proxy/LICENSE +0 -202
  87. data/vendor/github.com/planetscale/sql-proxy/proxy/client.go +0 -476
  88. data/vendor/github.com/planetscale/sql-proxy/proxy/tls_cache.go +0 -73
  89. data/vendor/go.uber.org/atomic/.codecov.yml +0 -19
  90. data/vendor/go.uber.org/atomic/.gitignore +0 -12
  91. data/vendor/go.uber.org/atomic/.travis.yml +0 -27
  92. data/vendor/go.uber.org/atomic/CHANGELOG.md +0 -76
  93. data/vendor/go.uber.org/atomic/LICENSE.txt +0 -19
  94. data/vendor/go.uber.org/atomic/Makefile +0 -78
  95. data/vendor/go.uber.org/atomic/README.md +0 -63
  96. data/vendor/go.uber.org/atomic/bool.go +0 -81
  97. data/vendor/go.uber.org/atomic/bool_ext.go +0 -53
  98. data/vendor/go.uber.org/atomic/doc.go +0 -23
  99. data/vendor/go.uber.org/atomic/duration.go +0 -82
  100. data/vendor/go.uber.org/atomic/duration_ext.go +0 -40
  101. data/vendor/go.uber.org/atomic/error.go +0 -51
  102. data/vendor/go.uber.org/atomic/error_ext.go +0 -39
  103. data/vendor/go.uber.org/atomic/float64.go +0 -76
  104. data/vendor/go.uber.org/atomic/float64_ext.go +0 -47
  105. data/vendor/go.uber.org/atomic/gen.go +0 -26
  106. data/vendor/go.uber.org/atomic/go.mod +0 -8
  107. data/vendor/go.uber.org/atomic/go.sum +0 -9
  108. data/vendor/go.uber.org/atomic/int32.go +0 -102
  109. data/vendor/go.uber.org/atomic/int64.go +0 -102
  110. data/vendor/go.uber.org/atomic/nocmp.go +0 -35
  111. data/vendor/go.uber.org/atomic/string.go +0 -54
  112. data/vendor/go.uber.org/atomic/string_ext.go +0 -43
  113. data/vendor/go.uber.org/atomic/uint32.go +0 -102
  114. data/vendor/go.uber.org/atomic/uint64.go +0 -102
  115. data/vendor/go.uber.org/atomic/value.go +0 -31
  116. data/vendor/go.uber.org/multierr/.codecov.yml +0 -15
  117. data/vendor/go.uber.org/multierr/.gitignore +0 -4
  118. data/vendor/go.uber.org/multierr/.travis.yml +0 -23
  119. data/vendor/go.uber.org/multierr/CHANGELOG.md +0 -60
  120. data/vendor/go.uber.org/multierr/LICENSE.txt +0 -19
  121. data/vendor/go.uber.org/multierr/Makefile +0 -42
  122. data/vendor/go.uber.org/multierr/README.md +0 -23
  123. data/vendor/go.uber.org/multierr/error.go +0 -449
  124. data/vendor/go.uber.org/multierr/glide.yaml +0 -8
  125. data/vendor/go.uber.org/multierr/go.mod +0 -8
  126. data/vendor/go.uber.org/multierr/go.sum +0 -11
  127. data/vendor/go.uber.org/multierr/go113.go +0 -52
  128. data/vendor/go.uber.org/zap/.codecov.yml +0 -17
  129. data/vendor/go.uber.org/zap/.gitignore +0 -32
  130. data/vendor/go.uber.org/zap/.readme.tmpl +0 -109
  131. data/vendor/go.uber.org/zap/CHANGELOG.md +0 -492
  132. data/vendor/go.uber.org/zap/CODE_OF_CONDUCT.md +0 -75
  133. data/vendor/go.uber.org/zap/CONTRIBUTING.md +0 -75
  134. data/vendor/go.uber.org/zap/FAQ.md +0 -164
  135. data/vendor/go.uber.org/zap/LICENSE.txt +0 -19
  136. data/vendor/go.uber.org/zap/Makefile +0 -73
  137. data/vendor/go.uber.org/zap/README.md +0 -134
  138. data/vendor/go.uber.org/zap/array.go +0 -320
  139. data/vendor/go.uber.org/zap/buffer/buffer.go +0 -141
  140. data/vendor/go.uber.org/zap/buffer/pool.go +0 -49
  141. data/vendor/go.uber.org/zap/checklicense.sh +0 -17
  142. data/vendor/go.uber.org/zap/config.go +0 -264
  143. data/vendor/go.uber.org/zap/doc.go +0 -113
  144. data/vendor/go.uber.org/zap/encoder.go +0 -79
  145. data/vendor/go.uber.org/zap/error.go +0 -80
  146. data/vendor/go.uber.org/zap/field.go +0 -549
  147. data/vendor/go.uber.org/zap/flag.go +0 -39
  148. data/vendor/go.uber.org/zap/glide.yaml +0 -34
  149. data/vendor/go.uber.org/zap/global.go +0 -168
  150. data/vendor/go.uber.org/zap/global_go112.go +0 -26
  151. data/vendor/go.uber.org/zap/global_prego112.go +0 -26
  152. data/vendor/go.uber.org/zap/go.mod +0 -14
  153. data/vendor/go.uber.org/zap/go.sum +0 -54
  154. data/vendor/go.uber.org/zap/http_handler.go +0 -132
  155. data/vendor/go.uber.org/zap/internal/bufferpool/bufferpool.go +0 -31
  156. data/vendor/go.uber.org/zap/internal/color/color.go +0 -44
  157. data/vendor/go.uber.org/zap/internal/exit/exit.go +0 -64
  158. data/vendor/go.uber.org/zap/level.go +0 -132
  159. data/vendor/go.uber.org/zap/logger.go +0 -348
  160. data/vendor/go.uber.org/zap/options.go +0 -148
  161. data/vendor/go.uber.org/zap/sink.go +0 -161
  162. data/vendor/go.uber.org/zap/stacktrace.go +0 -85
  163. data/vendor/go.uber.org/zap/sugar.go +0 -315
  164. data/vendor/go.uber.org/zap/time.go +0 -27
  165. data/vendor/go.uber.org/zap/writer.go +0 -99
  166. data/vendor/go.uber.org/zap/zapcore/buffered_write_syncer.go +0 -188
  167. data/vendor/go.uber.org/zap/zapcore/clock.go +0 -50
  168. data/vendor/go.uber.org/zap/zapcore/console_encoder.go +0 -161
  169. data/vendor/go.uber.org/zap/zapcore/core.go +0 -113
  170. data/vendor/go.uber.org/zap/zapcore/doc.go +0 -24
  171. data/vendor/go.uber.org/zap/zapcore/encoder.go +0 -443
  172. data/vendor/go.uber.org/zap/zapcore/entry.go +0 -264
  173. data/vendor/go.uber.org/zap/zapcore/error.go +0 -132
  174. data/vendor/go.uber.org/zap/zapcore/field.go +0 -233
  175. data/vendor/go.uber.org/zap/zapcore/hook.go +0 -68
  176. data/vendor/go.uber.org/zap/zapcore/increase_level.go +0 -66
  177. data/vendor/go.uber.org/zap/zapcore/json_encoder.go +0 -534
  178. data/vendor/go.uber.org/zap/zapcore/level.go +0 -175
  179. data/vendor/go.uber.org/zap/zapcore/level_strings.go +0 -46
  180. data/vendor/go.uber.org/zap/zapcore/marshaler.go +0 -61
  181. data/vendor/go.uber.org/zap/zapcore/memory_encoder.go +0 -179
  182. data/vendor/go.uber.org/zap/zapcore/sampler.go +0 -208
  183. data/vendor/go.uber.org/zap/zapcore/tee.go +0 -81
  184. data/vendor/go.uber.org/zap/zapcore/write_syncer.go +0 -122
  185. data/vendor/golang.org/x/net/AUTHORS +0 -3
  186. data/vendor/golang.org/x/net/CONTRIBUTORS +0 -3
  187. data/vendor/golang.org/x/net/LICENSE +0 -27
  188. data/vendor/golang.org/x/net/PATENTS +0 -22
  189. data/vendor/golang.org/x/net/context/context.go +0 -56
  190. data/vendor/golang.org/x/net/context/ctxhttp/ctxhttp.go +0 -71
  191. data/vendor/golang.org/x/net/context/go17.go +0 -73
  192. data/vendor/golang.org/x/net/context/go19.go +0 -21
  193. data/vendor/golang.org/x/net/context/pre_go17.go +0 -301
  194. data/vendor/golang.org/x/net/context/pre_go19.go +0 -110
  195. data/vendor/golang.org/x/oauth2/.travis.yml +0 -13
  196. data/vendor/golang.org/x/oauth2/AUTHORS +0 -3
  197. data/vendor/golang.org/x/oauth2/CONTRIBUTING.md +0 -26
  198. data/vendor/golang.org/x/oauth2/CONTRIBUTORS +0 -3
  199. data/vendor/golang.org/x/oauth2/LICENSE +0 -27
  200. data/vendor/golang.org/x/oauth2/README.md +0 -36
  201. data/vendor/golang.org/x/oauth2/go.mod +0 -9
  202. data/vendor/golang.org/x/oauth2/go.sum +0 -361
  203. data/vendor/golang.org/x/oauth2/internal/client_appengine.go +0 -14
  204. data/vendor/golang.org/x/oauth2/internal/doc.go +0 -6
  205. data/vendor/golang.org/x/oauth2/internal/oauth2.go +0 -37
  206. data/vendor/golang.org/x/oauth2/internal/token.go +0 -294
  207. data/vendor/golang.org/x/oauth2/internal/transport.go +0 -33
  208. data/vendor/golang.org/x/oauth2/oauth2.go +0 -381
  209. data/vendor/golang.org/x/oauth2/token.go +0 -178
  210. data/vendor/golang.org/x/oauth2/transport.go +0 -89
  211. data/vendor/google.golang.org/appengine/LICENSE +0 -202
  212. data/vendor/google.golang.org/appengine/internal/api.go +0 -678
  213. data/vendor/google.golang.org/appengine/internal/api_classic.go +0 -169
  214. data/vendor/google.golang.org/appengine/internal/api_common.go +0 -123
  215. data/vendor/google.golang.org/appengine/internal/app_id.go +0 -28
  216. data/vendor/google.golang.org/appengine/internal/base/api_base.pb.go +0 -308
  217. data/vendor/google.golang.org/appengine/internal/base/api_base.proto +0 -33
  218. data/vendor/google.golang.org/appengine/internal/datastore/datastore_v3.pb.go +0 -4367
  219. data/vendor/google.golang.org/appengine/internal/datastore/datastore_v3.proto +0 -551
  220. data/vendor/google.golang.org/appengine/internal/identity.go +0 -55
  221. data/vendor/google.golang.org/appengine/internal/identity_classic.go +0 -61
  222. data/vendor/google.golang.org/appengine/internal/identity_flex.go +0 -11
  223. data/vendor/google.golang.org/appengine/internal/identity_vm.go +0 -134
  224. data/vendor/google.golang.org/appengine/internal/internal.go +0 -110
  225. data/vendor/google.golang.org/appengine/internal/log/log_service.pb.go +0 -1313
  226. data/vendor/google.golang.org/appengine/internal/log/log_service.proto +0 -150
  227. data/vendor/google.golang.org/appengine/internal/main.go +0 -16
  228. data/vendor/google.golang.org/appengine/internal/main_common.go +0 -7
  229. data/vendor/google.golang.org/appengine/internal/main_vm.go +0 -69
  230. data/vendor/google.golang.org/appengine/internal/metadata.go +0 -60
  231. data/vendor/google.golang.org/appengine/internal/net.go +0 -56
  232. data/vendor/google.golang.org/appengine/internal/regen.sh +0 -40
  233. data/vendor/google.golang.org/appengine/internal/remote_api/remote_api.pb.go +0 -361
  234. data/vendor/google.golang.org/appengine/internal/remote_api/remote_api.proto +0 -44
  235. data/vendor/google.golang.org/appengine/internal/transaction.go +0 -115
  236. data/vendor/google.golang.org/appengine/internal/urlfetch/urlfetch_service.pb.go +0 -527
  237. data/vendor/google.golang.org/appengine/internal/urlfetch/urlfetch_service.proto +0 -64
  238. data/vendor/google.golang.org/appengine/urlfetch/urlfetch.go +0 -210
  239. data/vendor/google.golang.org/protobuf/AUTHORS +0 -3
  240. data/vendor/google.golang.org/protobuf/CONTRIBUTORS +0 -3
  241. data/vendor/google.golang.org/protobuf/LICENSE +0 -27
  242. data/vendor/google.golang.org/protobuf/PATENTS +0 -22
  243. data/vendor/google.golang.org/protobuf/encoding/prototext/decode.go +0 -773
  244. data/vendor/google.golang.org/protobuf/encoding/prototext/doc.go +0 -7
  245. data/vendor/google.golang.org/protobuf/encoding/prototext/encode.go +0 -371
  246. data/vendor/google.golang.org/protobuf/encoding/protowire/wire.go +0 -538
  247. data/vendor/google.golang.org/protobuf/internal/descfmt/stringer.go +0 -318
  248. data/vendor/google.golang.org/protobuf/internal/descopts/options.go +0 -29
  249. data/vendor/google.golang.org/protobuf/internal/detrand/rand.go +0 -69
  250. data/vendor/google.golang.org/protobuf/internal/encoding/defval/default.go +0 -213
  251. data/vendor/google.golang.org/protobuf/internal/encoding/messageset/messageset.go +0 -241
  252. data/vendor/google.golang.org/protobuf/internal/encoding/tag/tag.go +0 -207
  253. data/vendor/google.golang.org/protobuf/internal/encoding/text/decode.go +0 -665
  254. data/vendor/google.golang.org/protobuf/internal/encoding/text/decode_number.go +0 -190
  255. data/vendor/google.golang.org/protobuf/internal/encoding/text/decode_string.go +0 -161
  256. data/vendor/google.golang.org/protobuf/internal/encoding/text/decode_token.go +0 -373
  257. data/vendor/google.golang.org/protobuf/internal/encoding/text/doc.go +0 -29
  258. data/vendor/google.golang.org/protobuf/internal/encoding/text/encode.go +0 -265
  259. data/vendor/google.golang.org/protobuf/internal/errors/errors.go +0 -89
  260. data/vendor/google.golang.org/protobuf/internal/errors/is_go112.go +0 -39
  261. data/vendor/google.golang.org/protobuf/internal/errors/is_go113.go +0 -12
  262. data/vendor/google.golang.org/protobuf/internal/filedesc/build.go +0 -158
  263. data/vendor/google.golang.org/protobuf/internal/filedesc/desc.go +0 -631
  264. data/vendor/google.golang.org/protobuf/internal/filedesc/desc_init.go +0 -471
  265. data/vendor/google.golang.org/protobuf/internal/filedesc/desc_lazy.go +0 -704
  266. data/vendor/google.golang.org/protobuf/internal/filedesc/desc_list.go +0 -450
  267. data/vendor/google.golang.org/protobuf/internal/filedesc/desc_list_gen.go +0 -356
  268. data/vendor/google.golang.org/protobuf/internal/filedesc/placeholder.go +0 -107
  269. data/vendor/google.golang.org/protobuf/internal/filetype/build.go +0 -297
  270. data/vendor/google.golang.org/protobuf/internal/flags/flags.go +0 -24
  271. data/vendor/google.golang.org/protobuf/internal/flags/proto_legacy_disable.go +0 -9
  272. data/vendor/google.golang.org/protobuf/internal/flags/proto_legacy_enable.go +0 -9
  273. data/vendor/google.golang.org/protobuf/internal/genid/any_gen.go +0 -34
  274. data/vendor/google.golang.org/protobuf/internal/genid/api_gen.go +0 -106
  275. data/vendor/google.golang.org/protobuf/internal/genid/descriptor_gen.go +0 -829
  276. data/vendor/google.golang.org/protobuf/internal/genid/doc.go +0 -11
  277. data/vendor/google.golang.org/protobuf/internal/genid/duration_gen.go +0 -34
  278. data/vendor/google.golang.org/protobuf/internal/genid/empty_gen.go +0 -19
  279. data/vendor/google.golang.org/protobuf/internal/genid/field_mask_gen.go +0 -31
  280. data/vendor/google.golang.org/protobuf/internal/genid/goname.go +0 -25
  281. data/vendor/google.golang.org/protobuf/internal/genid/map_entry.go +0 -16
  282. data/vendor/google.golang.org/protobuf/internal/genid/source_context_gen.go +0 -31
  283. data/vendor/google.golang.org/protobuf/internal/genid/struct_gen.go +0 -116
  284. data/vendor/google.golang.org/protobuf/internal/genid/timestamp_gen.go +0 -34
  285. data/vendor/google.golang.org/protobuf/internal/genid/type_gen.go +0 -184
  286. data/vendor/google.golang.org/protobuf/internal/genid/wrappers.go +0 -13
  287. data/vendor/google.golang.org/protobuf/internal/genid/wrappers_gen.go +0 -175
  288. data/vendor/google.golang.org/protobuf/internal/impl/api_export.go +0 -177
  289. data/vendor/google.golang.org/protobuf/internal/impl/checkinit.go +0 -141
  290. data/vendor/google.golang.org/protobuf/internal/impl/codec_extension.go +0 -223
  291. data/vendor/google.golang.org/protobuf/internal/impl/codec_field.go +0 -830
  292. data/vendor/google.golang.org/protobuf/internal/impl/codec_gen.go +0 -5637
  293. data/vendor/google.golang.org/protobuf/internal/impl/codec_map.go +0 -388
  294. data/vendor/google.golang.org/protobuf/internal/impl/codec_map_go111.go +0 -37
  295. data/vendor/google.golang.org/protobuf/internal/impl/codec_map_go112.go +0 -11
  296. data/vendor/google.golang.org/protobuf/internal/impl/codec_message.go +0 -217
  297. data/vendor/google.golang.org/protobuf/internal/impl/codec_messageset.go +0 -123
  298. data/vendor/google.golang.org/protobuf/internal/impl/codec_reflect.go +0 -209
  299. data/vendor/google.golang.org/protobuf/internal/impl/codec_tables.go +0 -557
  300. data/vendor/google.golang.org/protobuf/internal/impl/codec_unsafe.go +0 -17
  301. data/vendor/google.golang.org/protobuf/internal/impl/convert.go +0 -496
  302. data/vendor/google.golang.org/protobuf/internal/impl/convert_list.go +0 -141
  303. data/vendor/google.golang.org/protobuf/internal/impl/convert_map.go +0 -121
  304. data/vendor/google.golang.org/protobuf/internal/impl/decode.go +0 -276
  305. data/vendor/google.golang.org/protobuf/internal/impl/encode.go +0 -201
  306. data/vendor/google.golang.org/protobuf/internal/impl/enum.go +0 -21
  307. data/vendor/google.golang.org/protobuf/internal/impl/extension.go +0 -156
  308. data/vendor/google.golang.org/protobuf/internal/impl/legacy_enum.go +0 -219
  309. data/vendor/google.golang.org/protobuf/internal/impl/legacy_export.go +0 -92
  310. data/vendor/google.golang.org/protobuf/internal/impl/legacy_extension.go +0 -176
  311. data/vendor/google.golang.org/protobuf/internal/impl/legacy_file.go +0 -81
  312. data/vendor/google.golang.org/protobuf/internal/impl/legacy_message.go +0 -558
  313. data/vendor/google.golang.org/protobuf/internal/impl/merge.go +0 -176
  314. data/vendor/google.golang.org/protobuf/internal/impl/merge_gen.go +0 -209
  315. data/vendor/google.golang.org/protobuf/internal/impl/message.go +0 -276
  316. data/vendor/google.golang.org/protobuf/internal/impl/message_reflect.go +0 -465
  317. data/vendor/google.golang.org/protobuf/internal/impl/message_reflect_field.go +0 -543
  318. data/vendor/google.golang.org/protobuf/internal/impl/message_reflect_gen.go +0 -249
  319. data/vendor/google.golang.org/protobuf/internal/impl/pointer_reflect.go +0 -178
  320. data/vendor/google.golang.org/protobuf/internal/impl/pointer_unsafe.go +0 -174
  321. data/vendor/google.golang.org/protobuf/internal/impl/validate.go +0 -576
  322. data/vendor/google.golang.org/protobuf/internal/impl/weak.go +0 -74
  323. data/vendor/google.golang.org/protobuf/internal/order/order.go +0 -89
  324. data/vendor/google.golang.org/protobuf/internal/order/range.go +0 -115
  325. data/vendor/google.golang.org/protobuf/internal/pragma/pragma.go +0 -29
  326. data/vendor/google.golang.org/protobuf/internal/set/ints.go +0 -58
  327. data/vendor/google.golang.org/protobuf/internal/strs/strings.go +0 -196
  328. data/vendor/google.golang.org/protobuf/internal/strs/strings_pure.go +0 -27
  329. data/vendor/google.golang.org/protobuf/internal/strs/strings_unsafe.go +0 -94
  330. data/vendor/google.golang.org/protobuf/internal/version/version.go +0 -79
  331. data/vendor/google.golang.org/protobuf/proto/checkinit.go +0 -71
  332. data/vendor/google.golang.org/protobuf/proto/decode.go +0 -278
  333. data/vendor/google.golang.org/protobuf/proto/decode_gen.go +0 -603
  334. data/vendor/google.golang.org/protobuf/proto/doc.go +0 -94
  335. data/vendor/google.golang.org/protobuf/proto/encode.go +0 -319
  336. data/vendor/google.golang.org/protobuf/proto/encode_gen.go +0 -97
  337. data/vendor/google.golang.org/protobuf/proto/equal.go +0 -167
  338. data/vendor/google.golang.org/protobuf/proto/extension.go +0 -92
  339. data/vendor/google.golang.org/protobuf/proto/merge.go +0 -139
  340. data/vendor/google.golang.org/protobuf/proto/messageset.go +0 -93
  341. data/vendor/google.golang.org/protobuf/proto/proto.go +0 -43
  342. data/vendor/google.golang.org/protobuf/proto/proto_methods.go +0 -19
  343. data/vendor/google.golang.org/protobuf/proto/proto_reflect.go +0 -19
  344. data/vendor/google.golang.org/protobuf/proto/reset.go +0 -43
  345. data/vendor/google.golang.org/protobuf/proto/size.go +0 -97
  346. data/vendor/google.golang.org/protobuf/proto/size_gen.go +0 -55
  347. data/vendor/google.golang.org/protobuf/proto/wrappers.go +0 -29
  348. data/vendor/google.golang.org/protobuf/reflect/protodesc/desc.go +0 -276
  349. data/vendor/google.golang.org/protobuf/reflect/protodesc/desc_init.go +0 -248
  350. data/vendor/google.golang.org/protobuf/reflect/protodesc/desc_resolve.go +0 -286
  351. data/vendor/google.golang.org/protobuf/reflect/protodesc/desc_validate.go +0 -374
  352. data/vendor/google.golang.org/protobuf/reflect/protodesc/proto.go +0 -252
  353. data/vendor/google.golang.org/protobuf/reflect/protoreflect/methods.go +0 -77
  354. data/vendor/google.golang.org/protobuf/reflect/protoreflect/proto.go +0 -504
  355. data/vendor/google.golang.org/protobuf/reflect/protoreflect/source.go +0 -128
  356. data/vendor/google.golang.org/protobuf/reflect/protoreflect/source_gen.go +0 -461
  357. data/vendor/google.golang.org/protobuf/reflect/protoreflect/type.go +0 -665
  358. data/vendor/google.golang.org/protobuf/reflect/protoreflect/value.go +0 -285
  359. data/vendor/google.golang.org/protobuf/reflect/protoreflect/value_pure.go +0 -59
  360. data/vendor/google.golang.org/protobuf/reflect/protoreflect/value_union.go +0 -411
  361. data/vendor/google.golang.org/protobuf/reflect/protoreflect/value_unsafe.go +0 -98
  362. data/vendor/google.golang.org/protobuf/reflect/protoregistry/registry.go +0 -869
  363. data/vendor/google.golang.org/protobuf/runtime/protoiface/legacy.go +0 -15
  364. data/vendor/google.golang.org/protobuf/runtime/protoiface/methods.go +0 -167
  365. data/vendor/google.golang.org/protobuf/runtime/protoimpl/impl.go +0 -44
  366. data/vendor/google.golang.org/protobuf/runtime/protoimpl/version.go +0 -56
  367. data/vendor/google.golang.org/protobuf/types/descriptorpb/descriptor.pb.go +0 -4039
  368. data/vendor/modules.txt +0 -79
@@ -1,805 +0,0 @@
1
- # gorilla/mux
2
-
3
- [![GoDoc](https://godoc.org/github.com/gorilla/mux?status.svg)](https://godoc.org/github.com/gorilla/mux)
4
- [![CircleCI](https://circleci.com/gh/gorilla/mux.svg?style=svg)](https://circleci.com/gh/gorilla/mux)
5
- [![Sourcegraph](https://sourcegraph.com/github.com/gorilla/mux/-/badge.svg)](https://sourcegraph.com/github.com/gorilla/mux?badge)
6
-
7
- ![Gorilla Logo](https://cloud-cdn.questionable.services/gorilla-icon-64.png)
8
-
9
- https://www.gorillatoolkit.org/pkg/mux
10
-
11
- Package `gorilla/mux` implements a request router and dispatcher for matching incoming requests to
12
- their respective handler.
13
-
14
- The name mux stands for "HTTP request multiplexer". Like the standard `http.ServeMux`, `mux.Router` matches incoming requests against a list of registered routes and calls a handler for the route that matches the URL or other conditions. The main features are:
15
-
16
- * It implements the `http.Handler` interface so it is compatible with the standard `http.ServeMux`.
17
- * Requests can be matched based on URL host, path, path prefix, schemes, header and query values, HTTP methods or using custom matchers.
18
- * URL hosts, paths and query values can have variables with an optional regular expression.
19
- * Registered URLs can be built, or "reversed", which helps maintaining references to resources.
20
- * Routes can be used as subrouters: nested routes are only tested if the parent route matches. This is useful to define groups of routes that share common conditions like a host, a path prefix or other repeated attributes. As a bonus, this optimizes request matching.
21
-
22
- ---
23
-
24
- * [Install](#install)
25
- * [Examples](#examples)
26
- * [Matching Routes](#matching-routes)
27
- * [Static Files](#static-files)
28
- * [Serving Single Page Applications](#serving-single-page-applications) (e.g. React, Vue, Ember.js, etc.)
29
- * [Registered URLs](#registered-urls)
30
- * [Walking Routes](#walking-routes)
31
- * [Graceful Shutdown](#graceful-shutdown)
32
- * [Middleware](#middleware)
33
- * [Handling CORS Requests](#handling-cors-requests)
34
- * [Testing Handlers](#testing-handlers)
35
- * [Full Example](#full-example)
36
-
37
- ---
38
-
39
- ## Install
40
-
41
- With a [correctly configured](https://golang.org/doc/install#testing) Go toolchain:
42
-
43
- ```sh
44
- go get -u github.com/gorilla/mux
45
- ```
46
-
47
- ## Examples
48
-
49
- Let's start registering a couple of URL paths and handlers:
50
-
51
- ```go
52
- func main() {
53
- r := mux.NewRouter()
54
- r.HandleFunc("/", HomeHandler)
55
- r.HandleFunc("/products", ProductsHandler)
56
- r.HandleFunc("/articles", ArticlesHandler)
57
- http.Handle("/", r)
58
- }
59
- ```
60
-
61
- Here we register three routes mapping URL paths to handlers. This is equivalent to how `http.HandleFunc()` works: if an incoming request URL matches one of the paths, the corresponding handler is called passing (`http.ResponseWriter`, `*http.Request`) as parameters.
62
-
63
- Paths can have variables. They are defined using the format `{name}` or `{name:pattern}`. If a regular expression pattern is not defined, the matched variable will be anything until the next slash. For example:
64
-
65
- ```go
66
- r := mux.NewRouter()
67
- r.HandleFunc("/products/{key}", ProductHandler)
68
- r.HandleFunc("/articles/{category}/", ArticlesCategoryHandler)
69
- r.HandleFunc("/articles/{category}/{id:[0-9]+}", ArticleHandler)
70
- ```
71
-
72
- The names are used to create a map of route variables which can be retrieved calling `mux.Vars()`:
73
-
74
- ```go
75
- func ArticlesCategoryHandler(w http.ResponseWriter, r *http.Request) {
76
- vars := mux.Vars(r)
77
- w.WriteHeader(http.StatusOK)
78
- fmt.Fprintf(w, "Category: %v\n", vars["category"])
79
- }
80
- ```
81
-
82
- And this is all you need to know about the basic usage. More advanced options are explained below.
83
-
84
- ### Matching Routes
85
-
86
- Routes can also be restricted to a domain or subdomain. Just define a host pattern to be matched. They can also have variables:
87
-
88
- ```go
89
- r := mux.NewRouter()
90
- // Only matches if domain is "www.example.com".
91
- r.Host("www.example.com")
92
- // Matches a dynamic subdomain.
93
- r.Host("{subdomain:[a-z]+}.example.com")
94
- ```
95
-
96
- There are several other matchers that can be added. To match path prefixes:
97
-
98
- ```go
99
- r.PathPrefix("/products/")
100
- ```
101
-
102
- ...or HTTP methods:
103
-
104
- ```go
105
- r.Methods("GET", "POST")
106
- ```
107
-
108
- ...or URL schemes:
109
-
110
- ```go
111
- r.Schemes("https")
112
- ```
113
-
114
- ...or header values:
115
-
116
- ```go
117
- r.Headers("X-Requested-With", "XMLHttpRequest")
118
- ```
119
-
120
- ...or query values:
121
-
122
- ```go
123
- r.Queries("key", "value")
124
- ```
125
-
126
- ...or to use a custom matcher function:
127
-
128
- ```go
129
- r.MatcherFunc(func(r *http.Request, rm *RouteMatch) bool {
130
- return r.ProtoMajor == 0
131
- })
132
- ```
133
-
134
- ...and finally, it is possible to combine several matchers in a single route:
135
-
136
- ```go
137
- r.HandleFunc("/products", ProductsHandler).
138
- Host("www.example.com").
139
- Methods("GET").
140
- Schemes("http")
141
- ```
142
-
143
- Routes are tested in the order they were added to the router. If two routes match, the first one wins:
144
-
145
- ```go
146
- r := mux.NewRouter()
147
- r.HandleFunc("/specific", specificHandler)
148
- r.PathPrefix("/").Handler(catchAllHandler)
149
- ```
150
-
151
- Setting the same matching conditions again and again can be boring, so we have a way to group several routes that share the same requirements. We call it "subrouting".
152
-
153
- For example, let's say we have several URLs that should only match when the host is `www.example.com`. Create a route for that host and get a "subrouter" from it:
154
-
155
- ```go
156
- r := mux.NewRouter()
157
- s := r.Host("www.example.com").Subrouter()
158
- ```
159
-
160
- Then register routes in the subrouter:
161
-
162
- ```go
163
- s.HandleFunc("/products/", ProductsHandler)
164
- s.HandleFunc("/products/{key}", ProductHandler)
165
- s.HandleFunc("/articles/{category}/{id:[0-9]+}", ArticleHandler)
166
- ```
167
-
168
- The three URL paths we registered above will only be tested if the domain is `www.example.com`, because the subrouter is tested first. This is not only convenient, but also optimizes request matching. You can create subrouters combining any attribute matchers accepted by a route.
169
-
170
- Subrouters can be used to create domain or path "namespaces": you define subrouters in a central place and then parts of the app can register its paths relatively to a given subrouter.
171
-
172
- There's one more thing about subroutes. When a subrouter has a path prefix, the inner routes use it as base for their paths:
173
-
174
- ```go
175
- r := mux.NewRouter()
176
- s := r.PathPrefix("/products").Subrouter()
177
- // "/products/"
178
- s.HandleFunc("/", ProductsHandler)
179
- // "/products/{key}/"
180
- s.HandleFunc("/{key}/", ProductHandler)
181
- // "/products/{key}/details"
182
- s.HandleFunc("/{key}/details", ProductDetailsHandler)
183
- ```
184
-
185
-
186
- ### Static Files
187
-
188
- Note that the path provided to `PathPrefix()` represents a "wildcard": calling
189
- `PathPrefix("/static/").Handler(...)` means that the handler will be passed any
190
- request that matches "/static/\*". This makes it easy to serve static files with mux:
191
-
192
- ```go
193
- func main() {
194
- var dir string
195
-
196
- flag.StringVar(&dir, "dir", ".", "the directory to serve files from. Defaults to the current dir")
197
- flag.Parse()
198
- r := mux.NewRouter()
199
-
200
- // This will serve files under http://localhost:8000/static/<filename>
201
- r.PathPrefix("/static/").Handler(http.StripPrefix("/static/", http.FileServer(http.Dir(dir))))
202
-
203
- srv := &http.Server{
204
- Handler: r,
205
- Addr: "127.0.0.1:8000",
206
- // Good practice: enforce timeouts for servers you create!
207
- WriteTimeout: 15 * time.Second,
208
- ReadTimeout: 15 * time.Second,
209
- }
210
-
211
- log.Fatal(srv.ListenAndServe())
212
- }
213
- ```
214
-
215
- ### Serving Single Page Applications
216
-
217
- Most of the time it makes sense to serve your SPA on a separate web server from your API,
218
- but sometimes it's desirable to serve them both from one place. It's possible to write a simple
219
- handler for serving your SPA (for use with React Router's [BrowserRouter](https://reacttraining.com/react-router/web/api/BrowserRouter) for example), and leverage
220
- mux's powerful routing for your API endpoints.
221
-
222
- ```go
223
- package main
224
-
225
- import (
226
- "encoding/json"
227
- "log"
228
- "net/http"
229
- "os"
230
- "path/filepath"
231
- "time"
232
-
233
- "github.com/gorilla/mux"
234
- )
235
-
236
- // spaHandler implements the http.Handler interface, so we can use it
237
- // to respond to HTTP requests. The path to the static directory and
238
- // path to the index file within that static directory are used to
239
- // serve the SPA in the given static directory.
240
- type spaHandler struct {
241
- staticPath string
242
- indexPath string
243
- }
244
-
245
- // ServeHTTP inspects the URL path to locate a file within the static dir
246
- // on the SPA handler. If a file is found, it will be served. If not, the
247
- // file located at the index path on the SPA handler will be served. This
248
- // is suitable behavior for serving an SPA (single page application).
249
- func (h spaHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
250
- // get the absolute path to prevent directory traversal
251
- path, err := filepath.Abs(r.URL.Path)
252
- if err != nil {
253
- // if we failed to get the absolute path respond with a 400 bad request
254
- // and stop
255
- http.Error(w, err.Error(), http.StatusBadRequest)
256
- return
257
- }
258
-
259
- // prepend the path with the path to the static directory
260
- path = filepath.Join(h.staticPath, path)
261
-
262
- // check whether a file exists at the given path
263
- _, err = os.Stat(path)
264
- if os.IsNotExist(err) {
265
- // file does not exist, serve index.html
266
- http.ServeFile(w, r, filepath.Join(h.staticPath, h.indexPath))
267
- return
268
- } else if err != nil {
269
- // if we got an error (that wasn't that the file doesn't exist) stating the
270
- // file, return a 500 internal server error and stop
271
- http.Error(w, err.Error(), http.StatusInternalServerError)
272
- return
273
- }
274
-
275
- // otherwise, use http.FileServer to serve the static dir
276
- http.FileServer(http.Dir(h.staticPath)).ServeHTTP(w, r)
277
- }
278
-
279
- func main() {
280
- router := mux.NewRouter()
281
-
282
- router.HandleFunc("/api/health", func(w http.ResponseWriter, r *http.Request) {
283
- // an example API handler
284
- json.NewEncoder(w).Encode(map[string]bool{"ok": true})
285
- })
286
-
287
- spa := spaHandler{staticPath: "build", indexPath: "index.html"}
288
- router.PathPrefix("/").Handler(spa)
289
-
290
- srv := &http.Server{
291
- Handler: router,
292
- Addr: "127.0.0.1:8000",
293
- // Good practice: enforce timeouts for servers you create!
294
- WriteTimeout: 15 * time.Second,
295
- ReadTimeout: 15 * time.Second,
296
- }
297
-
298
- log.Fatal(srv.ListenAndServe())
299
- }
300
- ```
301
-
302
- ### Registered URLs
303
-
304
- Now let's see how to build registered URLs.
305
-
306
- Routes can be named. All routes that define a name can have their URLs built, or "reversed". We define a name calling `Name()` on a route. For example:
307
-
308
- ```go
309
- r := mux.NewRouter()
310
- r.HandleFunc("/articles/{category}/{id:[0-9]+}", ArticleHandler).
311
- Name("article")
312
- ```
313
-
314
- To build a URL, get the route and call the `URL()` method, passing a sequence of key/value pairs for the route variables. For the previous route, we would do:
315
-
316
- ```go
317
- url, err := r.Get("article").URL("category", "technology", "id", "42")
318
- ```
319
-
320
- ...and the result will be a `url.URL` with the following path:
321
-
322
- ```
323
- "/articles/technology/42"
324
- ```
325
-
326
- This also works for host and query value variables:
327
-
328
- ```go
329
- r := mux.NewRouter()
330
- r.Host("{subdomain}.example.com").
331
- Path("/articles/{category}/{id:[0-9]+}").
332
- Queries("filter", "{filter}").
333
- HandlerFunc(ArticleHandler).
334
- Name("article")
335
-
336
- // url.String() will be "http://news.example.com/articles/technology/42?filter=gorilla"
337
- url, err := r.Get("article").URL("subdomain", "news",
338
- "category", "technology",
339
- "id", "42",
340
- "filter", "gorilla")
341
- ```
342
-
343
- All variables defined in the route are required, and their values must conform to the corresponding patterns. These requirements guarantee that a generated URL will always match a registered route -- the only exception is for explicitly defined "build-only" routes which never match.
344
-
345
- Regex support also exists for matching Headers within a route. For example, we could do:
346
-
347
- ```go
348
- r.HeadersRegexp("Content-Type", "application/(text|json)")
349
- ```
350
-
351
- ...and the route will match both requests with a Content-Type of `application/json` as well as `application/text`
352
-
353
- There's also a way to build only the URL host or path for a route: use the methods `URLHost()` or `URLPath()` instead. For the previous route, we would do:
354
-
355
- ```go
356
- // "http://news.example.com/"
357
- host, err := r.Get("article").URLHost("subdomain", "news")
358
-
359
- // "/articles/technology/42"
360
- path, err := r.Get("article").URLPath("category", "technology", "id", "42")
361
- ```
362
-
363
- And if you use subrouters, host and path defined separately can be built as well:
364
-
365
- ```go
366
- r := mux.NewRouter()
367
- s := r.Host("{subdomain}.example.com").Subrouter()
368
- s.Path("/articles/{category}/{id:[0-9]+}").
369
- HandlerFunc(ArticleHandler).
370
- Name("article")
371
-
372
- // "http://news.example.com/articles/technology/42"
373
- url, err := r.Get("article").URL("subdomain", "news",
374
- "category", "technology",
375
- "id", "42")
376
- ```
377
-
378
- ### Walking Routes
379
-
380
- The `Walk` function on `mux.Router` can be used to visit all of the routes that are registered on a router. For example,
381
- the following prints all of the registered routes:
382
-
383
- ```go
384
- package main
385
-
386
- import (
387
- "fmt"
388
- "net/http"
389
- "strings"
390
-
391
- "github.com/gorilla/mux"
392
- )
393
-
394
- func handler(w http.ResponseWriter, r *http.Request) {
395
- return
396
- }
397
-
398
- func main() {
399
- r := mux.NewRouter()
400
- r.HandleFunc("/", handler)
401
- r.HandleFunc("/products", handler).Methods("POST")
402
- r.HandleFunc("/articles", handler).Methods("GET")
403
- r.HandleFunc("/articles/{id}", handler).Methods("GET", "PUT")
404
- r.HandleFunc("/authors", handler).Queries("surname", "{surname}")
405
- err := r.Walk(func(route *mux.Route, router *mux.Router, ancestors []*mux.Route) error {
406
- pathTemplate, err := route.GetPathTemplate()
407
- if err == nil {
408
- fmt.Println("ROUTE:", pathTemplate)
409
- }
410
- pathRegexp, err := route.GetPathRegexp()
411
- if err == nil {
412
- fmt.Println("Path regexp:", pathRegexp)
413
- }
414
- queriesTemplates, err := route.GetQueriesTemplates()
415
- if err == nil {
416
- fmt.Println("Queries templates:", strings.Join(queriesTemplates, ","))
417
- }
418
- queriesRegexps, err := route.GetQueriesRegexp()
419
- if err == nil {
420
- fmt.Println("Queries regexps:", strings.Join(queriesRegexps, ","))
421
- }
422
- methods, err := route.GetMethods()
423
- if err == nil {
424
- fmt.Println("Methods:", strings.Join(methods, ","))
425
- }
426
- fmt.Println()
427
- return nil
428
- })
429
-
430
- if err != nil {
431
- fmt.Println(err)
432
- }
433
-
434
- http.Handle("/", r)
435
- }
436
- ```
437
-
438
- ### Graceful Shutdown
439
-
440
- Go 1.8 introduced the ability to [gracefully shutdown](https://golang.org/doc/go1.8#http_shutdown) a `*http.Server`. Here's how to do that alongside `mux`:
441
-
442
- ```go
443
- package main
444
-
445
- import (
446
- "context"
447
- "flag"
448
- "log"
449
- "net/http"
450
- "os"
451
- "os/signal"
452
- "time"
453
-
454
- "github.com/gorilla/mux"
455
- )
456
-
457
- func main() {
458
- var wait time.Duration
459
- flag.DurationVar(&wait, "graceful-timeout", time.Second * 15, "the duration for which the server gracefully wait for existing connections to finish - e.g. 15s or 1m")
460
- flag.Parse()
461
-
462
- r := mux.NewRouter()
463
- // Add your routes as needed
464
-
465
- srv := &http.Server{
466
- Addr: "0.0.0.0:8080",
467
- // Good practice to set timeouts to avoid Slowloris attacks.
468
- WriteTimeout: time.Second * 15,
469
- ReadTimeout: time.Second * 15,
470
- IdleTimeout: time.Second * 60,
471
- Handler: r, // Pass our instance of gorilla/mux in.
472
- }
473
-
474
- // Run our server in a goroutine so that it doesn't block.
475
- go func() {
476
- if err := srv.ListenAndServe(); err != nil {
477
- log.Println(err)
478
- }
479
- }()
480
-
481
- c := make(chan os.Signal, 1)
482
- // We'll accept graceful shutdowns when quit via SIGINT (Ctrl+C)
483
- // SIGKILL, SIGQUIT or SIGTERM (Ctrl+/) will not be caught.
484
- signal.Notify(c, os.Interrupt)
485
-
486
- // Block until we receive our signal.
487
- <-c
488
-
489
- // Create a deadline to wait for.
490
- ctx, cancel := context.WithTimeout(context.Background(), wait)
491
- defer cancel()
492
- // Doesn't block if no connections, but will otherwise wait
493
- // until the timeout deadline.
494
- srv.Shutdown(ctx)
495
- // Optionally, you could run srv.Shutdown in a goroutine and block on
496
- // <-ctx.Done() if your application should wait for other services
497
- // to finalize based on context cancellation.
498
- log.Println("shutting down")
499
- os.Exit(0)
500
- }
501
- ```
502
-
503
- ### Middleware
504
-
505
- Mux supports the addition of middlewares to a [Router](https://godoc.org/github.com/gorilla/mux#Router), which are executed in the order they are added if a match is found, including its subrouters.
506
- 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.
507
-
508
- Mux middlewares are defined using the de facto standard type:
509
-
510
- ```go
511
- type MiddlewareFunc func(http.Handler) http.Handler
512
- ```
513
-
514
- 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. This takes advantage of closures being able access variables from the context where they are created, while retaining the signature enforced by the receivers.
515
-
516
- A very basic middleware which logs the URI of the request being handled could be written as:
517
-
518
- ```go
519
- func loggingMiddleware(next http.Handler) http.Handler {
520
- return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
521
- // Do stuff here
522
- log.Println(r.RequestURI)
523
- // Call the next handler, which can be another middleware in the chain, or the final handler.
524
- next.ServeHTTP(w, r)
525
- })
526
- }
527
- ```
528
-
529
- Middlewares can be added to a router using `Router.Use()`:
530
-
531
- ```go
532
- r := mux.NewRouter()
533
- r.HandleFunc("/", handler)
534
- r.Use(loggingMiddleware)
535
- ```
536
-
537
- A more complex authentication middleware, which maps session token to users, could be written as:
538
-
539
- ```go
540
- // Define our struct
541
- type authenticationMiddleware struct {
542
- tokenUsers map[string]string
543
- }
544
-
545
- // Initialize it somewhere
546
- func (amw *authenticationMiddleware) Populate() {
547
- amw.tokenUsers["00000000"] = "user0"
548
- amw.tokenUsers["aaaaaaaa"] = "userA"
549
- amw.tokenUsers["05f717e5"] = "randomUser"
550
- amw.tokenUsers["deadbeef"] = "user0"
551
- }
552
-
553
- // Middleware function, which will be called for each request
554
- func (amw *authenticationMiddleware) Middleware(next http.Handler) http.Handler {
555
- return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
556
- token := r.Header.Get("X-Session-Token")
557
-
558
- if user, found := amw.tokenUsers[token]; found {
559
- // We found the token in our map
560
- log.Printf("Authenticated user %s\n", user)
561
- // Pass down the request to the next middleware (or final handler)
562
- next.ServeHTTP(w, r)
563
- } else {
564
- // Write an error and stop the handler chain
565
- http.Error(w, "Forbidden", http.StatusForbidden)
566
- }
567
- })
568
- }
569
- ```
570
-
571
- ```go
572
- r := mux.NewRouter()
573
- r.HandleFunc("/", handler)
574
-
575
- amw := authenticationMiddleware{}
576
- amw.Populate()
577
-
578
- r.Use(amw.Middleware)
579
- ```
580
-
581
- 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. Middlewares _should_ write to `ResponseWriter` if they _are_ going to terminate the request, and they _should not_ write to `ResponseWriter` if they _are not_ going to terminate it.
582
-
583
- ### Handling CORS Requests
584
-
585
- [CORSMethodMiddleware](https://godoc.org/github.com/gorilla/mux#CORSMethodMiddleware) intends to make it easier to strictly set the `Access-Control-Allow-Methods` response header.
586
-
587
- * You will still need to use your own CORS handler to set the other CORS headers such as `Access-Control-Allow-Origin`
588
- * The middleware will set the `Access-Control-Allow-Methods` header to all the method matchers (e.g. `r.Methods(http.MethodGet, http.MethodPut, http.MethodOptions)` -> `Access-Control-Allow-Methods: GET,PUT,OPTIONS`) on a route
589
- * If you do not specify any methods, then:
590
- > _Important_: there must be an `OPTIONS` method matcher for the middleware to set the headers.
591
-
592
- Here is an example of using `CORSMethodMiddleware` along with a custom `OPTIONS` handler to set all the required CORS headers:
593
-
594
- ```go
595
- package main
596
-
597
- import (
598
- "net/http"
599
- "github.com/gorilla/mux"
600
- )
601
-
602
- func main() {
603
- r := mux.NewRouter()
604
-
605
- // IMPORTANT: you must specify an OPTIONS method matcher for the middleware to set CORS headers
606
- r.HandleFunc("/foo", fooHandler).Methods(http.MethodGet, http.MethodPut, http.MethodPatch, http.MethodOptions)
607
- r.Use(mux.CORSMethodMiddleware(r))
608
-
609
- http.ListenAndServe(":8080", r)
610
- }
611
-
612
- func fooHandler(w http.ResponseWriter, r *http.Request) {
613
- w.Header().Set("Access-Control-Allow-Origin", "*")
614
- if r.Method == http.MethodOptions {
615
- return
616
- }
617
-
618
- w.Write([]byte("foo"))
619
- }
620
- ```
621
-
622
- And an request to `/foo` using something like:
623
-
624
- ```bash
625
- curl localhost:8080/foo -v
626
- ```
627
-
628
- Would look like:
629
-
630
- ```bash
631
- * Trying ::1...
632
- * TCP_NODELAY set
633
- * Connected to localhost (::1) port 8080 (#0)
634
- > GET /foo HTTP/1.1
635
- > Host: localhost:8080
636
- > User-Agent: curl/7.59.0
637
- > Accept: */*
638
- >
639
- < HTTP/1.1 200 OK
640
- < Access-Control-Allow-Methods: GET,PUT,PATCH,OPTIONS
641
- < Access-Control-Allow-Origin: *
642
- < Date: Fri, 28 Jun 2019 20:13:30 GMT
643
- < Content-Length: 3
644
- < Content-Type: text/plain; charset=utf-8
645
- <
646
- * Connection #0 to host localhost left intact
647
- foo
648
- ```
649
-
650
- ### Testing Handlers
651
-
652
- Testing handlers in a Go web application is straightforward, and _mux_ doesn't complicate this any further. Given two files: `endpoints.go` and `endpoints_test.go`, here's how we'd test an application using _mux_.
653
-
654
- First, our simple HTTP handler:
655
-
656
- ```go
657
- // endpoints.go
658
- package main
659
-
660
- func HealthCheckHandler(w http.ResponseWriter, r *http.Request) {
661
- // A very simple health check.
662
- w.Header().Set("Content-Type", "application/json")
663
- w.WriteHeader(http.StatusOK)
664
-
665
- // In the future we could report back on the status of our DB, or our cache
666
- // (e.g. Redis) by performing a simple PING, and include them in the response.
667
- io.WriteString(w, `{"alive": true}`)
668
- }
669
-
670
- func main() {
671
- r := mux.NewRouter()
672
- r.HandleFunc("/health", HealthCheckHandler)
673
-
674
- log.Fatal(http.ListenAndServe("localhost:8080", r))
675
- }
676
- ```
677
-
678
- Our test code:
679
-
680
- ```go
681
- // endpoints_test.go
682
- package main
683
-
684
- import (
685
- "net/http"
686
- "net/http/httptest"
687
- "testing"
688
- )
689
-
690
- func TestHealthCheckHandler(t *testing.T) {
691
- // Create a request to pass to our handler. We don't have any query parameters for now, so we'll
692
- // pass 'nil' as the third parameter.
693
- req, err := http.NewRequest("GET", "/health", nil)
694
- if err != nil {
695
- t.Fatal(err)
696
- }
697
-
698
- // We create a ResponseRecorder (which satisfies http.ResponseWriter) to record the response.
699
- rr := httptest.NewRecorder()
700
- handler := http.HandlerFunc(HealthCheckHandler)
701
-
702
- // Our handlers satisfy http.Handler, so we can call their ServeHTTP method
703
- // directly and pass in our Request and ResponseRecorder.
704
- handler.ServeHTTP(rr, req)
705
-
706
- // Check the status code is what we expect.
707
- if status := rr.Code; status != http.StatusOK {
708
- t.Errorf("handler returned wrong status code: got %v want %v",
709
- status, http.StatusOK)
710
- }
711
-
712
- // Check the response body is what we expect.
713
- expected := `{"alive": true}`
714
- if rr.Body.String() != expected {
715
- t.Errorf("handler returned unexpected body: got %v want %v",
716
- rr.Body.String(), expected)
717
- }
718
- }
719
- ```
720
-
721
- In the case that our routes have [variables](#examples), we can pass those in the request. We could write
722
- [table-driven tests](https://dave.cheney.net/2013/06/09/writing-table-driven-tests-in-go) to test multiple
723
- possible route variables as needed.
724
-
725
- ```go
726
- // endpoints.go
727
- func main() {
728
- r := mux.NewRouter()
729
- // A route with a route variable:
730
- r.HandleFunc("/metrics/{type}", MetricsHandler)
731
-
732
- log.Fatal(http.ListenAndServe("localhost:8080", r))
733
- }
734
- ```
735
-
736
- Our test file, with a table-driven test of `routeVariables`:
737
-
738
- ```go
739
- // endpoints_test.go
740
- func TestMetricsHandler(t *testing.T) {
741
- tt := []struct{
742
- routeVariable string
743
- shouldPass bool
744
- }{
745
- {"goroutines", true},
746
- {"heap", true},
747
- {"counters", true},
748
- {"queries", true},
749
- {"adhadaeqm3k", false},
750
- }
751
-
752
- for _, tc := range tt {
753
- path := fmt.Sprintf("/metrics/%s", tc.routeVariable)
754
- req, err := http.NewRequest("GET", path, nil)
755
- if err != nil {
756
- t.Fatal(err)
757
- }
758
-
759
- rr := httptest.NewRecorder()
760
-
761
- // Need to create a router that we can pass the request through so that the vars will be added to the context
762
- router := mux.NewRouter()
763
- router.HandleFunc("/metrics/{type}", MetricsHandler)
764
- router.ServeHTTP(rr, req)
765
-
766
- // In this case, our MetricsHandler returns a non-200 response
767
- // for a route variable it doesn't know about.
768
- if rr.Code == http.StatusOK && !tc.shouldPass {
769
- t.Errorf("handler should have failed on routeVariable %s: got %v want %v",
770
- tc.routeVariable, rr.Code, http.StatusOK)
771
- }
772
- }
773
- }
774
- ```
775
-
776
- ## Full Example
777
-
778
- Here's a complete, runnable example of a small `mux` based server:
779
-
780
- ```go
781
- package main
782
-
783
- import (
784
- "net/http"
785
- "log"
786
- "github.com/gorilla/mux"
787
- )
788
-
789
- func YourHandler(w http.ResponseWriter, r *http.Request) {
790
- w.Write([]byte("Gorilla!\n"))
791
- }
792
-
793
- func main() {
794
- r := mux.NewRouter()
795
- // Routes consist of a path and a handler function.
796
- r.HandleFunc("/", YourHandler)
797
-
798
- // Bind to a port and pass our router in
799
- log.Fatal(http.ListenAndServe(":8000", r))
800
- }
801
- ```
802
-
803
- ## License
804
-
805
- BSD licensed. See the LICENSE file for details.