utils.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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 gin
  5. import (
  6. "encoding/xml"
  7. "net/http"
  8. "os"
  9. "path"
  10. "reflect"
  11. "runtime"
  12. "strings"
  13. )
  14. const BindKey = "_gin-gonic/gin/bindkey"
  15. func Bind(val interface{}) HandlerFunc {
  16. value := reflect.ValueOf(val)
  17. if value.Kind() == reflect.Ptr {
  18. panic(`Bind struct can not be a pointer. Example:
  19. Use: gin.Bind(Struct{}) instead of gin.Bind(&Struct{})
  20. `)
  21. }
  22. typ := value.Type()
  23. return func(c *Context) {
  24. obj := reflect.New(typ).Interface()
  25. if c.Bind(obj) == nil {
  26. c.Set(BindKey, obj)
  27. }
  28. }
  29. }
  30. func WrapF(f http.HandlerFunc) HandlerFunc {
  31. return func(c *Context) {
  32. f(c.Writer, c.Request)
  33. }
  34. }
  35. func WrapH(h http.Handler) HandlerFunc {
  36. return func(c *Context) {
  37. h.ServeHTTP(c.Writer, c.Request)
  38. }
  39. }
  40. type H map[string]interface{}
  41. // MarshalXML allows type H to be used with xml.Marshal.
  42. func (h H) MarshalXML(e *xml.Encoder, start xml.StartElement) error {
  43. start.Name = xml.Name{
  44. Space: "",
  45. Local: "map",
  46. }
  47. if err := e.EncodeToken(start); err != nil {
  48. return err
  49. }
  50. for key, value := range h {
  51. elem := xml.StartElement{
  52. Name: xml.Name{Space: "", Local: key},
  53. Attr: []xml.Attr{},
  54. }
  55. if err := e.EncodeElement(value, elem); err != nil {
  56. return err
  57. }
  58. }
  59. if err := e.EncodeToken(xml.EndElement{Name: start.Name}); err != nil {
  60. return err
  61. }
  62. return nil
  63. }
  64. func assert1(guard bool, text string) {
  65. if !guard {
  66. panic(text)
  67. }
  68. }
  69. func filterFlags(content string) string {
  70. for i, char := range content {
  71. if char == ' ' || char == ';' {
  72. return content[:i]
  73. }
  74. }
  75. return content
  76. }
  77. func chooseData(custom, wildcard interface{}) interface{} {
  78. if custom == nil {
  79. if wildcard == nil {
  80. panic("negotiation config is invalid")
  81. }
  82. return wildcard
  83. }
  84. return custom
  85. }
  86. func parseAccept(acceptHeader string) []string {
  87. parts := strings.Split(acceptHeader, ",")
  88. out := make([]string, 0, len(parts))
  89. for _, part := range parts {
  90. if index := strings.IndexByte(part, ';'); index >= 0 {
  91. part = part[0:index]
  92. }
  93. if part = strings.TrimSpace(part); len(part) > 0 {
  94. out = append(out, part)
  95. }
  96. }
  97. return out
  98. }
  99. func lastChar(str string) uint8 {
  100. size := len(str)
  101. if size == 0 {
  102. panic("The length of the string can't be 0")
  103. }
  104. return str[size-1]
  105. }
  106. func nameOfFunction(f interface{}) string {
  107. return runtime.FuncForPC(reflect.ValueOf(f).Pointer()).Name()
  108. }
  109. func joinPaths(absolutePath, relativePath string) string {
  110. if len(relativePath) == 0 {
  111. return absolutePath
  112. }
  113. finalPath := path.Join(absolutePath, relativePath)
  114. appendSlash := lastChar(relativePath) == '/' && lastChar(finalPath) != '/'
  115. if appendSlash {
  116. return finalPath + "/"
  117. }
  118. return finalPath
  119. }
  120. func resolveAddress(addr []string) string {
  121. switch len(addr) {
  122. case 0:
  123. if port := os.Getenv("PORT"); len(port) > 0 {
  124. debugPrint("Environment variable PORT=\"%s\"", port)
  125. return ":" + port
  126. }
  127. debugPrint("Environment variable PORT is undefined. Using port :8080 by default")
  128. return ":8080"
  129. case 1:
  130. return addr[0]
  131. default:
  132. panic("too much parameters")
  133. }
  134. }