http urls monitor.

binding.go 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. // Copyright 2014 Manu Martinez-Almeida. All rights reserved.
  2. // Use of this source code is governed by a MIT style
  3. // license that can be found in the LICENSE file.
  4. package binding
  5. import "net/http"
  6. // Content-Type MIME of the most common data formats.
  7. const (
  8. MIMEJSON = "application/json"
  9. MIMEHTML = "text/html"
  10. MIMEXML = "application/xml"
  11. MIMEXML2 = "text/xml"
  12. MIMEPlain = "text/plain"
  13. MIMEPOSTForm = "application/x-www-form-urlencoded"
  14. MIMEMultipartPOSTForm = "multipart/form-data"
  15. MIMEPROTOBUF = "application/x-protobuf"
  16. MIMEMSGPACK = "application/x-msgpack"
  17. MIMEMSGPACK2 = "application/msgpack"
  18. )
  19. // Binding describes the interface which needs to be implemented for binding the
  20. // data present in the request such as JSON request body, query parameters or
  21. // the form POST.
  22. type Binding interface {
  23. Name() string
  24. Bind(*http.Request, interface{}) error
  25. }
  26. // BindingBody adds BindBody method to Binding. BindBody is similar with Bind,
  27. // but it reads the body from supplied bytes instead of req.Body.
  28. type BindingBody interface {
  29. Binding
  30. BindBody([]byte, interface{}) error
  31. }
  32. // StructValidator is the minimal interface which needs to be implemented in
  33. // order for it to be used as the validator engine for ensuring the correctness
  34. // of the reqest. Gin provides a default implementation for this using
  35. // https://github.com/go-playground/validator/tree/v8.18.2.
  36. type StructValidator interface {
  37. // ValidateStruct can receive any kind of type and it should never panic, even if the configuration is not right.
  38. // If the received type is not a struct, any validation should be skipped and nil must be returned.
  39. // If the received type is a struct or pointer to a struct, the validation should be performed.
  40. // If the struct is not valid or the validation itself fails, a descriptive error should be returned.
  41. // Otherwise nil must be returned.
  42. ValidateStruct(interface{}) error
  43. // Engine returns the underlying validator engine which powers the
  44. // StructValidator implementation.
  45. Engine() interface{}
  46. }
  47. // Validator is the default validator which implements the StructValidator
  48. // interface. It uses https://github.com/go-playground/validator/tree/v8.18.2
  49. // under the hood.
  50. var Validator StructValidator = &defaultValidator{}
  51. // These implement the Binding interface and can be used to bind the data
  52. // present in the request to struct instances.
  53. var (
  54. JSON = jsonBinding{}
  55. XML = xmlBinding{}
  56. Form = formBinding{}
  57. Query = queryBinding{}
  58. FormPost = formPostBinding{}
  59. FormMultipart = formMultipartBinding{}
  60. ProtoBuf = protobufBinding{}
  61. MsgPack = msgpackBinding{}
  62. )
  63. // Default returns the appropriate Binding instance based on the HTTP method
  64. // and the content type.
  65. func Default(method, contentType string) Binding {
  66. if method == "GET" {
  67. return Form
  68. }
  69. switch contentType {
  70. case MIMEJSON:
  71. return JSON
  72. case MIMEXML, MIMEXML2:
  73. return XML
  74. case MIMEPROTOBUF:
  75. return ProtoBuf
  76. case MIMEMSGPACK, MIMEMSGPACK2:
  77. return MsgPack
  78. default: //case MIMEPOSTForm, MIMEMultipartPOSTForm:
  79. return Form
  80. }
  81. }
  82. func validate(obj interface{}) error {
  83. if Validator == nil {
  84. return nil
  85. }
  86. return Validator.ValidateStruct(obj)
  87. }