Working on content type negotiation API
This commit is contained in:
		@ -1,5 +1,10 @@
 | 
				
			|||||||
#Changelog
 | 
					#Changelog
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					###Gin 0.5 (Aug 21, 2014)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					- [NEW] Content Negotiation
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
###Gin 0.4 (Aug 21, 2014)
 | 
					###Gin 0.4 (Aug 21, 2014)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
- [NEW] Development mode
 | 
					- [NEW] Development mode
 | 
				
			||||||
 | 
				
			|||||||
							
								
								
									
										73
									
								
								context.go
									
									
									
									
									
								
							
							
						
						
									
										73
									
								
								context.go
									
									
									
									
									
								
							@ -67,6 +67,7 @@ type Context struct {
 | 
				
			|||||||
	Engine    *Engine
 | 
						Engine    *Engine
 | 
				
			||||||
	handlers  []HandlerFunc
 | 
						handlers  []HandlerFunc
 | 
				
			||||||
	index     int8
 | 
						index     int8
 | 
				
			||||||
 | 
						accepted  []string
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
/************************************/
 | 
					/************************************/
 | 
				
			||||||
@ -275,3 +276,75 @@ func (c *Context) Data(code int, contentType string, data []byte) {
 | 
				
			|||||||
func (c *Context) File(filepath string) {
 | 
					func (c *Context) File(filepath string) {
 | 
				
			||||||
	http.ServeFile(c.Writer, c.Request, filepath)
 | 
						http.ServeFile(c.Writer, c.Request, filepath)
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/************************************/
 | 
				
			||||||
 | 
					/******** CONTENT NEGOTIATION *******/
 | 
				
			||||||
 | 
					/************************************/
 | 
				
			||||||
 | 
					type Negotiate struct {
 | 
				
			||||||
 | 
						Offered  []string
 | 
				
			||||||
 | 
						Data     interface{}
 | 
				
			||||||
 | 
						JsonData interface{}
 | 
				
			||||||
 | 
						XMLData  interface{}
 | 
				
			||||||
 | 
						HTMLData interface{}
 | 
				
			||||||
 | 
						HTMLPath string
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func (c *Context) Negotiate2(code int, config Negotiate) {
 | 
				
			||||||
 | 
						result := c.NegotiateFormat(config.Offered...)
 | 
				
			||||||
 | 
						switch result {
 | 
				
			||||||
 | 
						case MIMEJSON:
 | 
				
			||||||
 | 
							c.JSON(code, config.Data)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						case MIMEHTML:
 | 
				
			||||||
 | 
							name := config.HTMLPath
 | 
				
			||||||
 | 
							c.HTML(code, name, config.Data)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						case MIMEXML:
 | 
				
			||||||
 | 
							c.XML(code, config.Data)
 | 
				
			||||||
 | 
						default:
 | 
				
			||||||
 | 
							c.Fail(400, errors.New("m"))
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func (c *Context) Negotiate(code int, config map[string]interface{}, offerts ...string) {
 | 
				
			||||||
 | 
						result := c.NegotiateFormat(offerts...)
 | 
				
			||||||
 | 
						switch result {
 | 
				
			||||||
 | 
						case MIMEJSON:
 | 
				
			||||||
 | 
							data := readData("json.data", config)
 | 
				
			||||||
 | 
							c.JSON(code, data)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						case MIMEHTML:
 | 
				
			||||||
 | 
							data := readData("html.data", config)
 | 
				
			||||||
 | 
							name := config["html.path"].(string)
 | 
				
			||||||
 | 
							c.HTML(code, name, data)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						case MIMEXML:
 | 
				
			||||||
 | 
							data := readData("xml.data", config)
 | 
				
			||||||
 | 
							c.XML(code, data)
 | 
				
			||||||
 | 
						default:
 | 
				
			||||||
 | 
							c.Fail(400, errors.New("m"))
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func (c *Context) NegotiateFormat(offered ...string) string {
 | 
				
			||||||
 | 
						if c.accepted == nil {
 | 
				
			||||||
 | 
							c.accepted = parseAccept(c.Request.Header.Get("Accept"))
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						if len(c.accepted) == 0 {
 | 
				
			||||||
 | 
							return offered[0]
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						} else {
 | 
				
			||||||
 | 
							for _, accepted := range c.accepted {
 | 
				
			||||||
 | 
								for _, offert := range offered {
 | 
				
			||||||
 | 
									if accepted == offert {
 | 
				
			||||||
 | 
										return offert
 | 
				
			||||||
 | 
									}
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
							return ""
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func (c *Context) SetAccepted(formats ...string) {
 | 
				
			||||||
 | 
						c.accepted = formats
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
				
			|||||||
							
								
								
									
										26
									
								
								utils.go
									
									
									
									
									
								
							
							
						
						
									
										26
									
								
								utils.go
									
									
									
									
									
								
							@ -8,6 +8,7 @@ import (
 | 
				
			|||||||
	"encoding/xml"
 | 
						"encoding/xml"
 | 
				
			||||||
	"reflect"
 | 
						"reflect"
 | 
				
			||||||
	"runtime"
 | 
						"runtime"
 | 
				
			||||||
 | 
						"strings"
 | 
				
			||||||
)
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
type H map[string]interface{}
 | 
					type H map[string]interface{}
 | 
				
			||||||
@ -45,6 +46,31 @@ func filterFlags(content string) string {
 | 
				
			|||||||
	return content
 | 
						return content
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func readData(key string, config map[string]interface{}) interface{} {
 | 
				
			||||||
 | 
						data, ok := config[key]
 | 
				
			||||||
 | 
						if ok {
 | 
				
			||||||
 | 
							return data
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						data, ok = config["*.data"]
 | 
				
			||||||
 | 
						if !ok {
 | 
				
			||||||
 | 
							panic("negotiation config is invalid")
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						return data
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func parseAccept(accept string) []string {
 | 
				
			||||||
 | 
						parts := strings.Split(accept, ",")
 | 
				
			||||||
 | 
						for i, part := range parts {
 | 
				
			||||||
 | 
							index := strings.IndexByte(part, ';')
 | 
				
			||||||
 | 
							if index >= 0 {
 | 
				
			||||||
 | 
								part = part[0:index]
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
							part = strings.TrimSpace(part)
 | 
				
			||||||
 | 
							parts[i] = part
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						return parts
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func funcName(f interface{}) string {
 | 
					func funcName(f interface{}) string {
 | 
				
			||||||
	return runtime.FuncForPC(reflect.ValueOf(f).Pointer()).Name()
 | 
						return runtime.FuncForPC(reflect.ValueOf(f).Pointer()).Name()
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user