Mercurial > gemma
view auth/connection.go @ 160:061209505028
feat: Login and logout with session restoration implemented
Login information stored in local storage for restoration after
browser restart. If a non expired session is found, it
is restored before entering the main area.
Username and logout are located in the lower sidebar.
author | Thomas Junk <thomas.junk@intevation.de> |
---|---|
date | Wed, 04 Jul 2018 17:21:10 +0200 |
parents | 3349bfc2a047 |
children | 3457a60fb12d |
line wrap: on
line source
package auth import ( "database/sql" "errors" "log" "sync" "time" ) var ErrNoSuchToken = errors.New("No such token") type ConnectionPool interface { Delete(token string) bool Add(token string, session *Session) *Connection Renew(token string) (string, error) Do(token string, fn func(*sql.DB) error) error Session(token string) *Session Shutdown() error } var ConnPool ConnectionPool = NewInMemoryConnectionPool() const ( maxOpen = 16 maxDBIdle = time.Minute * 5 ) type Connection struct { session *Session access time.Time db *sql.DB refCount int mu sync.Mutex } func (c *Connection) set(session *Session) { c.session = session c.touch() } func (c *Connection) touch() { c.mu.Lock() c.access = time.Now() c.mu.Unlock() } func (c *Connection) last() time.Time { c.mu.Lock() access := c.access c.mu.Unlock() return access } func (c *Connection) close() { if c.db != nil { if err := c.db.Close(); err != nil { log.Printf("warn: %v\n", err) } c.db = nil } }