gin-gonic / gin
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

5
package gin
6

7
import (
8
	"encoding/xml"
9
	"net/http"
10
	"os"
11
	"path"
12
	"reflect"
13
	"runtime"
14
	"strings"
15
)
16

17
// BindKey indicates a default bind key.
18
const BindKey = "_gin-gonic/gin/bindkey"
19

20
// Bind is a helper function for given interface object and returns a Gin middleware.
21
func Bind(val interface{}) HandlerFunc {
22 16
	value := reflect.ValueOf(val)
23 16
	if value.Kind() == reflect.Ptr {
24 16
		panic(`Bind struct can not be a pointer. Example:
25 16
	Use: gin.Bind(Struct{}) instead of gin.Bind(&Struct{})
26 16
`)
27
	}
28 16
	typ := value.Type()
29

30 16
	return func(c *Context) {
31 16
		obj := reflect.New(typ).Interface()
32 16
		if c.Bind(obj) == nil {
33 16
			c.Set(BindKey, obj)
34
		}
35
	}
36
}
37

38
// WrapF is a helper function for wrapping http.HandlerFunc and returns a Gin middleware.
39
func WrapF(f http.HandlerFunc) HandlerFunc {
40 16
	return func(c *Context) {
41 16
		f(c.Writer, c.Request)
42
	}
43
}
44

45
// WrapH is a helper function for wrapping http.Handler and returns a Gin middleware.
46
func WrapH(h http.Handler) HandlerFunc {
47 16
	return func(c *Context) {
48 16
		h.ServeHTTP(c.Writer, c.Request)
49
	}
50
}
51

52
// H is a shortcut for map[string]interface{}
53
type H map[string]interface{}
54

55
// MarshalXML allows type H to be used with xml.Marshal.
56
func (h H) MarshalXML(e *xml.Encoder, start xml.StartElement) error {
57 16
	start.Name = xml.Name{
58 16
		Space: "",
59 16
		Local: "map",
60
	}
61 16
	if err := e.EncodeToken(start); err != nil {
62 0
		return err
63
	}
64
	for key, value := range h {
65 16
		elem := xml.StartElement{
66 16
			Name: xml.Name{Space: "", Local: key},
67 16
			Attr: []xml.Attr{},
68
		}
69 16
		if err := e.EncodeElement(value, elem); err != nil {
70 16
			return err
71
		}
72
	}
73

74 16
	return e.EncodeToken(xml.EndElement{Name: start.Name})
75
}
76

77
func assert1(guard bool, text string) {
78 16
	if !guard {
79 16
		panic(text)
80
	}
81
}
82

83
func filterFlags(content string) string {
84
	for i, char := range content {
85 16
		if char == ' ' || char == ';' {
86 16
			return content[:i]
87
		}
88
	}
89 16
	return content
90
}
91

92
func chooseData(custom, wildcard interface{}) interface{} {
93 16
	if custom != nil {
94 16
		return custom
95
	}
96 16
	if wildcard != nil {
97 16
		return wildcard
98
	}
99 16
	panic("negotiation config is invalid")
100
}
101

102
func parseAccept(acceptHeader string) []string {
103 16
	parts := strings.Split(acceptHeader, ",")
104 16
	out := make([]string, 0, len(parts))
105
	for _, part := range parts {
106 16
		if i := strings.IndexByte(part, ';'); i > 0 {
107 16
			part = part[:i]
108
		}
109 16
		if part = strings.TrimSpace(part); part != "" {
110 16
			out = append(out, part)
111
		}
112
	}
113 16
	return out
114
}
115

116
func lastChar(str string) uint8 {
117 16
	if str == "" {
118 16
		panic("The length of the string can't be 0")
119
	}
120 16
	return str[len(str)-1]
121
}
122

123
func nameOfFunction(f interface{}) string {
124 16
	return runtime.FuncForPC(reflect.ValueOf(f).Pointer()).Name()
125
}
126

127
func joinPaths(absolutePath, relativePath string) string {
128 16
	if relativePath == "" {
129 16
		return absolutePath
130
	}
131

132 16
	finalPath := path.Join(absolutePath, relativePath)
133 16
	if lastChar(relativePath) == '/' && lastChar(finalPath) != '/' {
134 16
		return finalPath + "/"
135
	}
136 16
	return finalPath
137
}
138

139
func resolveAddress(addr []string) string {
140 16
	switch len(addr) {
141 16
	case 0:
142 16
		if port := os.Getenv("PORT"); port != "" {
143 16
			debugPrint("Environment variable PORT=\"%s\"", port)
144 16
			return ":" + port
145
		}
146 16
		debugPrint("Environment variable PORT is undefined. Using port :8080 by default")
147 16
		return ":8080"
148 16
	case 1:
149 16
		return addr[0]
150 16
	default:
151 16
		panic("too many parameters")
152
	}
153
}

Read our documentation on viewing source code .

Loading