// 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, context martini.Context, errors *Errors, ifacePtr ...interface{}) { context.Invoke(Validate(obj.Interface())) errors.combine(getErrors(context)) context.Map(*errors) context.Map(obj.Elem().Interface()) if len(ifacePtr) > 0 { context.MapTo(obj.Elem().Interface(), ifacePtr[0]) } }
func SearcherMiddle(c martini.Context, w http.ResponseWriter, r *http.Request) { switch r.URL.Query().Get("type") { case "bing": c.MapTo(BingSearcher{}, (*Searcher)(nil)) w.Header().Set("Content-Type", "application/json") default: c.MapTo(BingSearcher{}, (*Searcher)(nil)) w.Header().Set("Content-Type", "application/json") } }
// MapEncoder intercepts the request's URL, detects the requested format, // and injects the correct encoder dependency for this request. It rewrites // the URL to remove the format extension, so that routes can be defined // without it. func mapEncoder(c martini.Context, w http.ResponseWriter, r *http.Request) { // Get the format extension matches := rxExt.FindStringSubmatch(r.URL.Path) ft := ".json" if len(matches) > 1 { // Rewrite the URL without the format extension l := len(r.URL.Path) - len(matches[1]) if strings.HasSuffix(r.URL.Path, "/") { l-- } r.URL.Path = r.URL.Path[:l] ft = matches[1] } // Inject the requested encoder switch ft { // Add cases for other formats default: c.MapTo(jsonEncoder{}, (*encoder)(nil)) w.Header().Set("Content-Type", "application/json") } }
func GET_callback(c martini.Context, sess sessions.Session, r *http.Request, db *sqlx.DB) { flow, ok := sess.Get("flow").(FlowState) if !ok { c.Invoke(redirect_to("/login")) return } if flow.StartAt.Before(time.Now().Add(-10 * time.Minute)) { c.Invoke(redirect_to("/login")) return } if flow.State == "" { c.Invoke(redirect_to("/login")) return } if r.URL.Query().Get("code") == "" { c.Invoke(redirect_to("/login")) return } if flow.State != r.URL.Query().Get("state") { c.Invoke(redirect_to("/login")) return } var ( provider = tmp_new_provider(flow.Provider) transport = provider.Transport(nil) ) token, err := transport.Exchange(r.URL.Query().Get("code")) if err != nil { panic(err) } profile, err := provider.GetProfile(transport) if err != nil { panic(err) } var ( tx = db.MustBegin() success bool ) defer func() { if success { tx.Commit() } else { tx.Rollback() } }() account, err := data.GetAccountWithRemoteId(tx, profile.RemoteId()) if err != nil { panic(err) } c.MapTo(profile, (*providers.Profile)(nil)) c.Map(token) c.Map(tx) c.Map(account) if account != nil { c.Invoke(GET_callback_A) } else { c.Invoke(GET_callback_B) } success = true }