mirror of
https://github.com/schollz/cowyo.git
synced 2023-08-10 21:13:00 +03:00
95401434a2
Former-commit-id: 900e1a398fd82aa1cea4f319e89b8088dd81cf6c [formerly f172c22b12c49c0291e0d986dc4af94fcc91d192] [formerly 43c5da81442a5f0ca79a6eabb1dfdfbfb3f22680 [formerly 7a39ef2b52fc10e7c980d377dbd52f5340e4e53a [formerly2694d0b183
]]] Former-commit-id: fa39d6a984adc4ca8f8c82c5df145c336885a53f [formerly 94543f8081bc18b1a39daf8500cfa7e0b1ba7393] Former-commit-id: 809aae62e28a3f99a01854f71fcd5a85f89d2972 Former-commit-id:e8a4d30139
155 lines
3.1 KiB
Go
155 lines
3.1 KiB
Go
// Copyright 2014 Manu Martinez-Almeida. All rights reserved.
|
|
// Use of this source code is governed by a MIT style
|
|
// license that can be found in the LICENSE file.
|
|
|
|
package gin
|
|
|
|
import (
|
|
"encoding/xml"
|
|
"net/http"
|
|
"os"
|
|
"path"
|
|
"reflect"
|
|
"runtime"
|
|
"strings"
|
|
)
|
|
|
|
const BindKey = "_gin-gonic/gin/bindkey"
|
|
|
|
func Bind(val interface{}) HandlerFunc {
|
|
value := reflect.ValueOf(val)
|
|
if value.Kind() == reflect.Ptr {
|
|
panic(`Bind struct can not be a pointer. Example:
|
|
Use: gin.Bind(Struct{}) instead of gin.Bind(&Struct{})
|
|
`)
|
|
}
|
|
typ := value.Type()
|
|
|
|
return func(c *Context) {
|
|
obj := reflect.New(typ).Interface()
|
|
if c.Bind(obj) == nil {
|
|
c.Set(BindKey, obj)
|
|
}
|
|
}
|
|
}
|
|
|
|
func WrapF(f http.HandlerFunc) HandlerFunc {
|
|
return func(c *Context) {
|
|
f(c.Writer, c.Request)
|
|
}
|
|
}
|
|
|
|
func WrapH(h http.Handler) HandlerFunc {
|
|
return func(c *Context) {
|
|
h.ServeHTTP(c.Writer, c.Request)
|
|
}
|
|
}
|
|
|
|
type H map[string]interface{}
|
|
|
|
// MarshalXML allows type H to be used with xml.Marshal
|
|
func (h H) MarshalXML(e *xml.Encoder, start xml.StartElement) error {
|
|
start.Name = xml.Name{
|
|
Space: "",
|
|
Local: "map",
|
|
}
|
|
if err := e.EncodeToken(start); err != nil {
|
|
return err
|
|
}
|
|
for key, value := range h {
|
|
elem := xml.StartElement{
|
|
Name: xml.Name{Space: "", Local: key},
|
|
Attr: []xml.Attr{},
|
|
}
|
|
if err := e.EncodeElement(value, elem); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
if err := e.EncodeToken(xml.EndElement{Name: start.Name}); err != nil {
|
|
return err
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func assert1(guard bool, text string) {
|
|
if !guard {
|
|
panic(text)
|
|
}
|
|
}
|
|
|
|
func filterFlags(content string) string {
|
|
for i, char := range content {
|
|
if char == ' ' || char == ';' {
|
|
return content[:i]
|
|
}
|
|
}
|
|
return content
|
|
}
|
|
|
|
func chooseData(custom, wildcard interface{}) interface{} {
|
|
if custom == nil {
|
|
if wildcard == nil {
|
|
panic("negotiation config is invalid")
|
|
}
|
|
return wildcard
|
|
}
|
|
return custom
|
|
}
|
|
|
|
func parseAccept(acceptHeader string) []string {
|
|
parts := strings.Split(acceptHeader, ",")
|
|
out := make([]string, 0, len(parts))
|
|
for _, part := range parts {
|
|
index := strings.IndexByte(part, ';')
|
|
if index >= 0 {
|
|
part = part[0:index]
|
|
}
|
|
part = strings.TrimSpace(part)
|
|
if len(part) > 0 {
|
|
out = append(out, part)
|
|
}
|
|
}
|
|
return out
|
|
}
|
|
|
|
func lastChar(str string) uint8 {
|
|
size := len(str)
|
|
if size == 0 {
|
|
panic("The length of the string can't be 0")
|
|
}
|
|
return str[size-1]
|
|
}
|
|
|
|
func nameOfFunction(f interface{}) string {
|
|
return runtime.FuncForPC(reflect.ValueOf(f).Pointer()).Name()
|
|
}
|
|
|
|
func joinPaths(absolutePath, relativePath string) string {
|
|
if len(relativePath) == 0 {
|
|
return absolutePath
|
|
}
|
|
|
|
finalPath := path.Join(absolutePath, relativePath)
|
|
appendSlash := lastChar(relativePath) == '/' && lastChar(finalPath) != '/'
|
|
if appendSlash {
|
|
return finalPath + "/"
|
|
}
|
|
return finalPath
|
|
}
|
|
|
|
func resolveAddress(addr []string) string {
|
|
switch len(addr) {
|
|
case 0:
|
|
if port := os.Getenv("PORT"); len(port) > 0 {
|
|
debugPrint("Environment variable PORT=\"%s\"", port)
|
|
return ":" + port
|
|
}
|
|
debugPrint("Environment variable PORT is undefined. Using port :8080 by default")
|
|
return ":8080"
|
|
case 1:
|
|
return addr[0]
|
|
default:
|
|
panic("too much parameters")
|
|
}
|
|
}
|