This repository has been archived by the owner on Jan 4, 2018. It is now read-only.
/
user.go
92 lines (78 loc) · 1.82 KB
/
user.go
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package password
import (
"time"
"github.com/boltdb/bolt"
"github.com/garyburd/redigo/redis"
"golang.org/x/crypto/bcrypt"
)
var userStore = NewBoltUserStore()
// User represents a single user
type User struct {
ID []byte
Password []byte
Name []byte
Email []byte
DateCreated time.Time
LastLogin time.Time
PhoneNumber []byte
}
// UserStore stores users in DBs
type UserStore interface {
All() []*User
Create(u *User) ([]byte, error)
Find(id []byte) *User
Update(id []byte) error
Delete(id []byte) error
}
// BoltUser is the user DB for Bolt.
type BoltUser struct {
DB *bolt.DB
BucketName []byte
}
func (b *BoltUser) All() {}
// Create creates a new user in the DB
func (b *BoltUser) Create(u *User) ([]byte, error) {
err := b.DB.Update(func(tx *bolt.Tx) error {
b := tx.Bucket(b.BucketName)
hashedSecret, err := bcrypt.GenerateFromPassword(u.Password, bcrypt.DefaultCost)
u.Password = hashedSecret
if err != nil {
return err
}
err = b.Put(u.Email, hashedSecret)
return err
})
return u.ID, err
}
func (b *BoltUser) Find() {}
func (b *BoltUser) Update() {}
func (b *BoltUser) Delete() {}
// RedisUser is the user DB for Redis.
type RedisUser struct {
DB redis.Conn
}
func (u *RedisUser) All() {}
func (u *RedisUser) Create() {}
func (u *RedisUser) Find() {}
func (u *RedisUser) Update() {}
func (u *RedisUser) Delete() {}
// NewBoltUserStore creates a new instance of BoltUser.
func NewBoltUserStore() *BoltUser {
db, err := bolt.Open("boltuser.db", 0600, &bolt.Options{
Timeout: 1 * time.Second,
})
if err != nil {
panic(err)
}
db.Update(func(tx *bolt.Tx) error {
_, err := tx.CreateBucketIfNotExists([]byte("boltuserbucket"))
if err != nil {
return err
}
return nil
})
return &BoltUser{
DB: db,
BucketName: []byte("boltuserbucket"),
}
}