You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

database.go 1.2KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. // Copyright (c) 2012-2014 Jeremy Latt
  2. // released under the MIT license
  3. package irc
  4. import (
  5. "encoding/base64"
  6. "fmt"
  7. "log"
  8. "os"
  9. "github.com/tidwall/buntdb"
  10. )
  11. const (
  12. // 'version' of the database schema
  13. keySchemaVersion = "db.version"
  14. // key for the primary salt used by the ircd
  15. keySalt = "crypto.salt"
  16. )
  17. // InitDB creates the database.
  18. func InitDB(path string) {
  19. // prepare kvstore db
  20. //TODO(dan): fail if already exists instead? don't want to overwrite good data
  21. os.Remove(path)
  22. store, err := buntdb.Open(path)
  23. if err != nil {
  24. log.Fatal(fmt.Sprintf("Failed to open datastore: %s", err.Error()))
  25. }
  26. defer store.Close()
  27. err = store.Update(func(tx *buntdb.Tx) error {
  28. // set base db salt
  29. salt, err := NewSalt()
  30. encodedSalt := base64.StdEncoding.EncodeToString(salt)
  31. if err != nil {
  32. log.Fatal("Could not generate cryptographically-secure salt for the user:", err.Error())
  33. }
  34. tx.Set(keySalt, encodedSalt, nil)
  35. // set schema version
  36. tx.Set(keySchemaVersion, "1", nil)
  37. return nil
  38. })
  39. if err != nil {
  40. log.Fatal("Could not save bunt store:", err.Error())
  41. }
  42. }
  43. // UpgradeDB upgrades the datastore to the latest schema.
  44. func UpgradeDB(path string) {
  45. return
  46. }