added missing docs and fixed vet warns

This commit is contained in:
Carlos Alexandro Becker 2015-09-07 14:01:16 -03:00 committed by Carlos A Becker
parent 4cff112644
commit 7c98dee240
2 changed files with 19 additions and 19 deletions

View File

@ -48,17 +48,11 @@ func (db *PGStore) cleanup(interval time.Duration, quit <-chan struct{}, done ch
log.Printf("pgstore: unable to delete expired sessions: %v", err) log.Printf("pgstore: unable to delete expired sessions: %v", err)
} }
} }
} }
} }
// deleteExpired deletes expired sessions from the database. // deleteExpired deletes expired sessions from the database.
func (db *PGStore) deleteExpired() error { func (db *PGStore) deleteExpired() error {
_, err := db.DbMap.Exec("DELETE FROM http_sessions WHERE expireson < now()") _, err := db.DbMap.Exec("DELETE FROM http_sessions WHERE expireson < now()")
if err != nil { return err
return err
}
return nil
} }

View File

@ -13,6 +13,7 @@ import (
_ "github.com/lib/pq" _ "github.com/lib/pq"
) )
// PGStore represents the currently configured session store
type PGStore struct { type PGStore struct {
Codecs []securecookie.Codec Codecs []securecookie.Codec
Options *sessions.Options Options *sessions.Options
@ -22,16 +23,17 @@ type PGStore struct {
// Session type // Session type
type Session struct { type Session struct {
Id int64 `db: "id"` Id int64 `db:"id"`
Key string `db: "key"` Key string `db:"key"`
Data string `db: "data"` Data string `db:"data"`
CreatedOn time.Time `db: "created_on"` CreatedOn time.Time `db:"created_on"`
ModifiedOn time.Time `db: "modified_on"` ModifiedOn time.Time `db:"modified_on"`
ExpiresOn time.Time `db: "expires_on"` ExpiresOn time.Time `db:"expires_on"`
} }
func NewPGStore(dbUrl string, keyPairs ...[]byte) *PGStore { // NewPGStore creates a new PGStore instance
db, err := sql.Open("postgres", dbUrl) func NewPGStore(dbURL string, keyPairs ...[]byte) *PGStore {
db, err := sql.Open("postgres", dbURL)
dbmap := &gorp.DbMap{Db: db, Dialect: gorp.PostgresDialect{}} dbmap := &gorp.DbMap{Db: db, Dialect: gorp.PostgresDialect{}}
dbStore := &PGStore{ dbStore := &PGStore{
@ -60,11 +62,13 @@ func NewPGStore(dbUrl string, keyPairs ...[]byte) *PGStore {
return dbStore return dbStore
} }
// Close closes the database connection
func (db *PGStore) Close() { func (db *PGStore) Close() {
db.DbMap.Db.Close() db.DbMap.Db.Close()
} }
// Fetches a session for a given name after it has been added to the registry. // Get Fetches a session for a given name after it has been added to the
// registry.
func (db *PGStore) Get(r *http.Request, name string) (*sessions.Session, error) { func (db *PGStore) Get(r *http.Request, name string) (*sessions.Session, error) {
return sessions.GetRegistry(r).Get(db, name) return sessions.GetRegistry(r).Get(db, name)
} }
@ -95,6 +99,7 @@ func (db *PGStore) New(r *http.Request, name string) (*sessions.Session, error)
return session, err return session, err
} }
// Save saves the given session into the database and deletes cookies if needed
func (db *PGStore) Save(r *http.Request, w http.ResponseWriter, session *sessions.Session) error { func (db *PGStore) Save(r *http.Request, w http.ResponseWriter, session *sessions.Session) error {
// Set delete if max-age is < 0 // Set delete if max-age is < 0
if session.Options.MaxAge < 0 { if session.Options.MaxAge < 0 {
@ -130,8 +135,8 @@ func (db *PGStore) Save(r *http.Request, w http.ResponseWriter, session *session
// If l is 0 there is no limit to the size of a session, use with caution. // If l is 0 there is no limit to the size of a session, use with caution.
// The default for a new PGStore is 4096. PostgreSQL allows for max // The default for a new PGStore is 4096. PostgreSQL allows for max
// value sizes of up to 1GB (http://www.postgresql.org/docs/current/interactive/datatype-character.html) // value sizes of up to 1GB (http://www.postgresql.org/docs/current/interactive/datatype-character.html)
func (s *PGStore) MaxLength(l int) { func (db *PGStore) MaxLength(l int) {
for _, c := range s.Codecs { for _, c := range db.Codecs {
if codec, ok := c.(*securecookie.SecureCookie); ok { if codec, ok := c.(*securecookie.SecureCookie); ok {
codec.MaxLength(l) codec.MaxLength(l)
} }
@ -152,7 +157,8 @@ func (db *PGStore) MaxAge(age int) {
} }
} }
//load fetches a session by ID from the database and decodes its content into session.Values // load fetches a session by ID from the database and decodes its content
// into session.Values
func (db *PGStore) load(session *sessions.Session) error { func (db *PGStore) load(session *sessions.Session) error {
var s Session var s Session
err := db.DbMap.SelectOne(&s, "SELECT * FROM http_sessions WHERE key = $1", session.ID) err := db.DbMap.SelectOne(&s, "SELECT * FROM http_sessions WHERE key = $1", session.ID)