aboutsummaryrefslogtreecommitdiffstats
path: root/store.go
blob: 55d808bdae5d0d0c5260c28bf8f472962965d043 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package main

import (
	"database/sql"
	"log"

	_ "github.com/lib/pq"
)

type Session struct {
	Id     string
	UserId int64
}

type User struct {
	Id       int64
	UserName string
}

type Store interface {
	GetSession(id string) (*Session, bool)
	GetUser(id int64) (*User, bool)
}

type PgStore struct {
	*sql.DB
	cache map[string]*Session
}

func NewPgStore() *PgSessionStore {
	db, err := sql.Open("postgres", "postgres://auth_master:pass@localhost/authdb")
	if err != nil {
		log.Panic(err)
	}
	return &PgSessionStore{db, make(map[string]*Session)}
}

func (store *PgSessionStore) Get(id string) (*Session, bool) {
	s, ok := store.cache[id]
	if !ok {
		row := store.QueryRow("SELECT id, user_id FROM session WHERE id = ?", id)
		var s Session
		row.Scan(&s.
	}
	return s, ok
}