Exemplo n.º 1
0
func getLoginUser(sId string) (u *user, err error) {
	err = nil
	u = NewUser()
	sessionKey := "sessions:" + sId
	// get user
	db := radix.NewClient(redisConfiguration)
	defer db.Close()
	reply := db.Command("hgetall", sessionKey)
	if reply.Error() != nil {
		errText := fmt.Sprintf(
			"Failed to get Session data (Redis): %v",
			reply.Error())
		logger.Err(errText)
		err = errors.New(errText)
		return
	}
	userInfo, err := reply.StringMap()
	if err != nil {
		errText := fmt.Sprintf(
			"Stringmap failed (Redis): %v", reply.Error())
		logger.Err(errText)
		err = errors.New(errText)
		return
	}
	u, err = getUserForUserInfo(userInfo)
	if err != nil {
		return
	}
	u.setLoggedIn()
	logger.Info(fmt.Sprintf("User is logged in: %v", u.ID))
	return
}
Exemplo n.º 2
0
// Save an array of tweets in Redis.
func saveTweets(key string, tws tweets) (err error) {
	logger.Info(fmt.Sprintf("Saving '%v' tweets to Redis", key))
	// Saving tweets as a json blob.
	twsJSON, err := json.Marshal(tws)
	if err != nil {
		logger.Err(fmt.Sprintf("Failed marshalling '%v' tweets json: %v", key, err))
		return
	}
	db := radix.NewClient(redisConfiguration)
	defer db.Close()

	timestamp := time.Now().Unix()
	twsMap := map[string]string{
		"tweets":    string(twsJSON),
		"timestamp": strconv.Itoa(int(timestamp)),
	}
	tweetKey := "tweets:" + key
	reply := db.Command("hmset", tweetKey, twsMap)
	if reply.Error() != nil {
		errText := fmt.Sprintf("Failed to set '%v' tweet data (Redis): %v", key, reply.Error())
		logger.Err(errText)
		err = errors.New(errText)
		return
	}
	return
}
Exemplo n.º 3
0
func newLoginSession(w http.ResponseWriter, r *http.Request, u *user) (s *session, err error) {
	sess := session{newUUID(), u}
	s = &sess
	err = nil
	sessionKey := "sessions:" + s.id
	userInfo := map[string]string{
		"Id":           u.ID,
		"Username":     u.Username,
		"EmailAddress": u.EmailAddress,
		"Role":         u.Role,
	}
	db := radix.NewClient(redisConfiguration)
	defer db.Close()
	reply := db.Command("hmset", sessionKey, userInfo)
	if reply.Error() != nil {
		errText := fmt.Sprintf("Failed to set Session data (Redis): %v", reply.Error())
		logger.Err(errText)
		err = errors.New(errText)
		return
	}
	// set login cookie
	_, err = r.Cookie("vafanLogin")
	if err != nil {
		if err == http.ErrNoCookie {
			//no cookie, set one
			err = nil
			logger.Info("Setting login cookie.")
			si, env := getSite(r)
			c := new(http.Cookie)
			c.Name = "vafanLogin"
			c.Value = s.id
			c.Path = "/"
			//c.Domain = "." + env + "." + si.Host
			if env == "production" {
				c.Domain = "." + si.Host
			} else {
				c.Domain = "." + env + "." + si.Host
			}
			http.SetCookie(w, c)
		} else {
			logger.Err(fmt.Sprintf("Failed getting login cookie (when trying to set): %v", err))
			return
		}
	} else {
		logger.Notice("Login cookie already set!")
		err = nil
	}
	return
}
Exemplo n.º 4
0
// Get tweets from Redis cache. Returned error warns if empty or stale.
func getTweets(key string) (tws tweets, err error) {
	db := radix.NewClient(redisConfiguration)
	defer db.Close()
	reply := db.Command("hgetall", "tweets:"+key)
	if reply.Error() != nil {
		errText := fmt.Sprintf("Failed to get '%v' tweet data (Redis): %v", key, reply.Error())
		logger.Err(errText)
		err = errors.New(errText)
		return
	}
	twsMap, err := reply.StringMap()
	if err != nil {
		errText := fmt.Sprintf("Stringmap for '%v' tweets failed (Redis): %v", key, reply.Error())
		logger.Err(errText)
		err = errors.New(errText)
		return
	}

	tws = tweets{}
	err = json.Unmarshal([]byte(twsMap["tweets"]), &tws)
	if err != nil {
		logger.Err(fmt.Sprintf("Failed unmarshalling '%v' tweets json: %v", key, err))
		err = ErrNoTweetsFound
		return
	}

	// Check the timestamp for freshness.
	then, err := strconv.Atoi(twsMap["timestamp"])
	if err != nil {
		logger.Err(fmt.Sprintf("Failed getting timestamp for '%v' tweets: %v", key, err))
		return
	}
	now := int(time.Now().Unix())
	diff := now - then
	logger.Info(fmt.Sprintf("'%v' tweets are %v seconds old.", key, diff))
	if diff > 600 {
		logger.Info(fmt.Sprintf("'%v' tweets are more than 10 min old (%v secs).", key, diff))
		err = ErrStaleTweets
	}

	// Did we get any tweets?
	if len(tws) < 1 {
		err = ErrNoTweetsFound
	}

	return
}