コード例 #1
0
ファイル: session.go プロジェクト: hyl87/hypermind
func (self *MySession) Destroy() (bool, error) {
	if len(self.key) == 0 || len(self.sessionId) == 0 {
		errorMsg := fmt.Sprintln("Uninitialized yet!")
		return false, errors.New(errorMsg)
	}
	go_lib.LogInfof("Destroy session (key=%s)... \n", self.key)
	grantors, err := dao.GetHash(self.key, SESSION_GRANTORS_KEY)
	if err != nil {
		return false, err
	}
	_, err = dao.DelKey(self.key)
	if err != nil {
		return false, err
	}
	if len(grantors) > 0 {
		_, err = dao.DelHashField(SESSION_MAP_KEY, grantors)
		if err != nil {
			return false, err
		}
	}
	go_lib.LogInfof("Delete session cookie (value=%s)...\n", self.sessionId)
	hmSessionCookie.Delete(SESSION_COOKIE_KEY, self.w)
	go_lib.LogInfof("The session (key=%s) is destroyed. \n", self.key)
	return true, nil
}
コード例 #2
0
ファイル: session.go プロジェクト: hyl87/hypermind
func (self *MySession) Initialize(
	grantors string,
	// survivalSeconds:
	//  '<0'- Don't set/Delete cookie;
	//  '0' - Set temporary cookie;
	//  '>0'- Set long term cookie according to this value & Set session expires
	survivalSeconds int,
	w http.ResponseWriter,
	r *http.Request) error {
	if len(grantors) == 0 {
		errorMsg := fmt.Sprintln("The session grantors is EMPTY!")
		return errors.New(errorMsg)
	}
	if w == nil {
		errorMsg := fmt.Sprintln("The pointer of http response writer is NIL!")
		return errors.New(errorMsg)
	}
	if r == nil {
		errorMsg := fmt.Sprintln("The pointer of http request is NIL!")
		return errors.New(errorMsg)
	}
	self.w = w
	self.r = r
	self.sessionId = generateSessionId(grantors, r)
	self.key = generateSessionKey(self.sessionId)
	go_lib.LogInfof("Initialize session (key=%s, grantors=%s)...\n", self.key, grantors)
	exists, err := dao.Exists(self.key)
	if err != nil {
		return err
	}
	if exists {
		_, err = dao.DelKey(self.key)
		if err != nil {
			return err
		}
	}
	_, err = dao.SetHash(self.key, SESSION_GRANTORS_KEY, grantors)
	if err != nil {
		return err
	}
	_, err = dao.SetHash(self.key, SESSION_SURVIVAL_SECONDS_KEY, strconv.FormatInt(int64(survivalSeconds), 10))
	if err != nil {
		return err
	}
	cookieMaxAge := survivalSeconds
	if survivalSeconds <= 0 {
		cookieMaxAge = -1
	}
	go_lib.LogInfof("Set session cookie (value=%s, grantors=%s, maxAge=%d)...\n", self.sessionId, grantors, cookieMaxAge)
	result := hmSessionCookie.SetOne(self.w, SESSION_COOKIE_KEY, self.sessionId, cookieMaxAge)
	if result {
		go_lib.LogInfof("Session cookie setting (value=%s, grantors=%s, maxAge=%d) is successful.\n", self.sessionId, grantors, cookieMaxAge)
	} else {
		go_lib.LogWarnf("Session cookie setting (value=%s, grantors=%s, maxAge=%d) is failing!\n", self.sessionId, grantors, cookieMaxAge)
	}
	if survivalSeconds > 0 {
		done, err := dao.SetExpires(self.key, uint64(survivalSeconds))
		if err != nil || !done {
			warningMsg := fmt.Sprintf("Setting session expires failed! (key=%s, survivalSeconds=%d, error=%s)\n", self.key, survivalSeconds, err)
			go_lib.LogWarnln(warningMsg)
		}
	}
	_, err = dao.SetHash(SESSION_MAP_KEY, grantors, self.key)
	if err != nil {
		return err
	}
	return nil
}