Exemplo n.º 1
0
// Performs validation and combines errors from validation
// with errors from deserialization, then maps both the
// resulting struct and the errors to the context.
func validateAndMap(obj reflect.Value, ctx *macaron.Context, errors *Errors, ifacePtr ...interface{}) {
	ctx.Invoke(Validate(obj.Interface()))
	errors.Combine(getErrors(ctx))
	ctx.Map(*errors)
	ctx.Map(obj.Elem().Interface())
	if len(ifacePtr) > 0 {
		ctx.MapTo(obj.Elem().Interface(), ifacePtr[0])
	}
}
Exemplo n.º 2
0
func UserNeeded(tokens oauth2.Tokens, ctx *macaron.Context) {
	user := &models.User{GithubToken: tokens.Access()}
	has, err := models.DB.Get(user)
	if err != nil {
		ctx.Error(500, err.Error())
		return
	}
	if !has {
		gh := github.New(tokens.Access())
		ghuser, err := gh.User()
		if err != nil {
			ctx.Error(500, err.Error())
			return
		}
		user.Name = ghuser.Name
		user.Email = ghuser.Email
		user.Admin = false
		models.DB.Insert(user)
	}
	ctx.Map(user)
}
Exemplo n.º 3
0
func bind(ctx *macaron.Context, obj interface{}, ifacePtr ...interface{}) {
	contentType := ctx.Req.Header.Get("Content-Type")
	if ctx.Req.Method == "POST" || ctx.Req.Method == "PUT" || len(contentType) > 0 {
		switch {
		case strings.Contains(contentType, "form-urlencoded"):
			ctx.Invoke(Form(obj, ifacePtr...))
		case strings.Contains(contentType, "multipart/form-data"):
			ctx.Invoke(MultipartForm(obj, ifacePtr...))
		case strings.Contains(contentType, "json"):
			ctx.Invoke(Json(obj, ifacePtr...))
		default:
			var errors Errors
			if contentType == "" {
				errors.Add([]string{}, ERR_CONTENT_TYPE, "Empty Content-Type")
			} else {
				errors.Add([]string{}, ERR_CONTENT_TYPE, "Unsupported Content-Type")
			}
			ctx.Map(errors)
		}
	} else {
		ctx.Invoke(Form(obj, ifacePtr...))
	}
}
Exemplo n.º 4
0
func myLogger(ctx *macaron.Context) {
	// 该服务的映射只会对当前请求的后续处理器有效
	ctx.Map(logger)
}