Beispiel #1
0
func Encoder(c *rest.Context, data interface{}) error {
	var resp = new(struct {
		Code     int         `json:"code"`
		Status   string      `json:"status"`
		Success  bool        `json:"success"`
		Location string      `json:"location,omitempty"`
		Error    string      `json:"error,omitempty"`
		Data     interface{} `json:"data,omitempty"`
		*httpfs.FileInfo
	})
	switch data := data.(type) {
	case error:
		resp.Error = data.Error()
	case *rest.RedirectURL:
		resp.Location = string(data.URL)
	case *httpfs.FileInfo:
		resp.FileInfo = data
		resp.Location = data.Name
	case *urlinfo.Info:
		resp.Data = data
	default:
		resp.Data = data
	}
	resp.Code = c.Status()
	resp.Status = http.StatusText(resp.Code)
	resp.Success = resp.Code < 400
	c.SetContentType("application/json; charset=utf-8")
	enc := json.NewEncoder(c.Response)
	enc.SetIndent("", "  ")
	return enc.Encode(resp)
}
Beispiel #2
0
// AvatarGenerator возвращает сгенерированную аватарку пользователя
func AvatarGenerator(c *rest.Context) error {
	c.SetContentType("image/png")
	return c.Write(icon.Render([]byte(c.Param("id"))))
}