-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move Bolt DB access to a separate, centralized package.
- Loading branch information
1 parent
b93b2df
commit 91667b4
Showing
8 changed files
with
132 additions
and
92 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package db | ||
|
||
// Config provides the configuration for the Bolt database. | ||
type Config struct { | ||
StorageDir string | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package db | ||
|
||
import ( | ||
"path" | ||
|
||
bolt "go.etcd.io/bbolt" | ||
) | ||
|
||
const databaseName = "i5.db" | ||
|
||
// DB provides a centralized entrypoint to the Bolt database. | ||
type DB struct { | ||
db *bolt.DB | ||
} | ||
|
||
// New creates and initializes the database. | ||
func New(cfg *Config) (*DB, error) { | ||
d, err := bolt.Open( | ||
path.Join(cfg.StorageDir, databaseName), | ||
0600, | ||
nil, | ||
) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return &DB{d}, nil | ||
} | ||
|
||
// Close frees all database resources. | ||
func (d *DB) Close() { | ||
d.db.Close() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package db | ||
|
||
import ( | ||
"errors" | ||
|
||
bolt "go.etcd.io/bbolt" | ||
"golang.org/x/crypto/bcrypt" | ||
) | ||
|
||
var ( | ||
usersBucket = []byte("Users") | ||
|
||
errInvalidUsernamePassword = errors.New("invalid username or password") | ||
) | ||
|
||
// CreateUser creates a new user in the database. | ||
func (d *DB) CreateUser(username, password string) error { | ||
h, err := bcrypt.GenerateFromPassword([]byte(password), 0) | ||
if err != nil { | ||
return err | ||
} | ||
return d.db.Update(func(tx *bolt.Tx) error { | ||
b, err := tx.CreateBucket(usersBucket) | ||
if err != nil { | ||
return err | ||
} | ||
return b.Put([]byte(username), h) | ||
}) | ||
} | ||
|
||
// LoginUser verifies the specified login credentials. | ||
func (d *DB) LoginUser(username, password string) error { | ||
var hashedPassword []byte | ||
d.db.View(func(tx *bolt.Tx) error { | ||
b := tx.Bucket(usersBucket) | ||
if b == nil { | ||
return nil | ||
} | ||
hashedPassword = b.Get([]byte(username)) | ||
return nil | ||
}) | ||
if hashedPassword == nil { | ||
return errInvalidUsernamePassword | ||
} | ||
if err := bcrypt.CompareHashAndPassword(hashedPassword, []byte(password)); err != nil { | ||
return errInvalidUsernamePassword | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters