func handledRoute(myroute *Route, rw http.ResponseWriter, r *http.Request) { start := time.Now() // debug transactions count count_before := 0 db.DB.Get(&count_before, "SELECT count(*) from pg_stat_activity where state = 'idle in transaction'") defer func() { count_end := 0 db.DB.Get(&count_end, "SELECT count(*) from pg_stat_activity where state = 'idle in transaction'") log.Println("idle in transaction : ", count_before, " => ", count_end) }() // session token := r.Header.Get("Authorization") log.Println("token ", token) s := session.GetSession(token) log.Print("user id from session : ", s.GetIntDef("user_id", -1)) // Retrieve user id from session user := model.User{} user.Id = s.GetIntDef("user_id", 0) // Open a transaction to load the user from db tx, err := db.DB.Beginx() if err != nil { log.Panicln("Can't start transaction for loading user") ServerError(rw, 500, "Can't start transaction for loading user") return } // get langs lang1 := model.Lang{ Isocode: "en", } lang2 := model.Lang{ Isocode: "fr", } if _lang1, err := r.Cookie("arkeogis_user_lang_1"); err == nil { lang1.Isocode = _lang1.Value } if _lang2, err := r.Cookie("arkeogis_user_lang_2"); err == nil { lang2.Isocode = _lang2.Value } err = lang1.Get(tx) if err != nil { lang1.Isocode = "en" err = lang1.Get(tx) if err != nil { fmt.Println("can't load lang1 !") } } err = lang2.Get(tx) if err != nil { lang2.Isocode = "fr" err = lang2.Get(tx) if err != nil { fmt.Println("can't load lang2 !") } } fmt.Println("langs: ", lang1, lang2) // Retrieve the user from db user.Get(tx) log.Println("user is : ", user.Username) s.Set("user", user) restlog.WriteString(fmt.Sprintf(`[%s][%s] %s %s %s %s`+"\n", start.Format(time.RFC3339), user.Username, r.RemoteAddr, r.Method, r.URL.Path, myroute.Method)) // Check global permsissions permok := true ok, err := user.HaveAtLeastOnePermission(tx, myroute.Permissions...) if err != nil { log.Println("user.HaveAtLeastOnePermission failed : ", err) permok = false } else if ok == false { log.Println("user has no permissions : ", myroute.Permissions) permok = false } // Close the transaction err = tx.Commit() if err != nil { if err, ok := err.(*pq.Error); ok { log.Println("commit while getting session user failed, pq error:", err.Code.Name()) } else { log.Println("commit while getting session user failed !", err) } ServerError(rw, 500, "Can't commit transaction") return } // Print a log log.Printf("[%s] %s %s ; authorized: %t\n", user.Username, myroute.Method, myroute.Path, permok) if !permok { ServerError(rw, 403, "unauthorized") return } o := decodeContent(myroute, rw, r, s) if o != nil { errors := sanitizer.SanitizeStruct(o, "json") if len(errors) > 0 { Errors(rw, errors) return } } params := decodeParams(myroute, rw, r) if params != nil { log.Println("params : ", params) errors := sanitizer.SanitizeStruct(params, "params") log.Println("Sanitized : ", params) if len(errors) > 0 { Errors(rw, errors) return } } proute := Proute{ Json: o, Params: params, Session: s, Lang1: lang1, Lang2: lang2, } myroute.Func(rw, r, proute) }
func loginAnswer(w http.ResponseWriter, tx *sqlx.Tx, user model.User, token string) (LoginAnswer, error) { // get langs lang1 := model.Lang{ Isocode: user.First_lang_isocode, } lang2 := model.Lang{ Isocode: user.Second_lang_isocode, } err := lang1.Get(tx) if err != nil { lang1.Isocode = "en" err = lang1.Get(tx) if err != nil { userSqlError(w, err) return LoginAnswer{}, err } } err = lang2.Get(tx) if err != nil { lang2.Isocode = "fr" err = lang2.Get(tx) if err != nil { tx.Rollback() log.Fatal("can't load lang2 !") return LoginAnswer{}, err } } log.Println("langs: ", lang1, lang2) permissions, err := user.GetPermissions(tx) if err != nil { log.Fatal("can't get permissions!") return LoginAnswer{}, err } log.Println("permissions : ", permissions) projectID, err := user.GetProjectId(tx) if err != nil { log.Fatal("can't get project!") return LoginAnswer{}, err } project := model.ProjectFullInfos{} if projectID > 0 { project.Id = projectID err = project.Get(tx) if err != nil { log.Fatal("can't get project informations!") return LoginAnswer{}, err } } else { project.Id = 0 } log.Println("project: ", project) a := LoginAnswer{ User: user, Token: token, Lang1: lang1, Lang2: lang2, Project: project, Permissions: permissions, } return a, nil }