-
Notifications
You must be signed in to change notification settings - Fork 44
/
db.go
54 lines (44 loc) · 1.26 KB
/
db.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
// Copyright 2015 mokey Authors. All rights reserved.
// Use of this source code is governed by a BSD style
// license that can be found in the LICENSE file.
package model
import (
"errors"
_ "github.com/go-sql-driver/mysql"
"github.com/jmoiron/sqlx"
)
type Datastore interface {
// API Key
RandKey() (string, error)
FetchApiKeys(uid string) ([]*ApiKey, error)
FetchApiKey(key string) (*ApiKey, error)
CreateApiKey(uid, clientID, scopes string) (*ApiKey, string, error)
RefreshApiKey(ak *ApiKey) error
DestroyApiKey(user, clientID string) error
DestroyApiKeys(uid string) error
// Token
RandToken() (string, error)
SignToken(salt, token string) string
VerifyToken(salt, signedToken string) (string, bool)
FetchTokenByUser(uid string, maxAge int) (*Token, error)
FetchToken(token string, maxAge int) (*Token, error)
CreateToken(uid, email string) (*Token, error)
IncrementToken(token string) error
DestroyToken(token string) error
DestroyTokenByUser(uid string) error
}
type DB struct {
*sqlx.DB
}
var ErrNotFound = errors.New("Record not found in database")
func NewDB(driver, dsn string) (*DB, error) {
db, err := sqlx.Open(driver, dsn)
if err != nil {
return nil, err
}
err = db.Ping()
if err != nil {
return nil, err
}
return &DB{db}, nil
}