gowiki/pkg/db/db.go
2018-02-11 19:21:31 +01:00

68 lines
1.3 KiB
Go

// Copyright (C) 2017 Marius Schellenberger
package db
import (
"bytes"
"errors"
"git.giftfish.de/ston1th/gowiki/cache"
"github.com/boltdb/bolt"
)
const (
dbFile = "gowiki.db"
defUser = "admin"
defPassword = "gowiki"
userTable = "user"
pageTable = "page"
bcryptCost = 13
indexName = "Index"
welcome = `# Welcome to GoWiki
This is the [Index](/wiki/Index) page.
You can customize it how you like.`
)
const (
maxResult = 1e6
maxSearchResult = 101
)
func dbErr(i interface{}) error {
return fmt.Errorf("db: %s", i)
}
type BoltStore struct {
Marshaler
db *bolt.DB
cache *cache.Cache
}
func NewBoltStore() (bs *BoltStore, err error) {
db, err := bolt.Open(dbFile, 0666, nil)
if err != nil {
return
}
if err = db.Update(func(tx *bolt.Tx) error {
if _, err := tx.CreateBucketIfNotExists([]byte(userTable)); err != nil {
return err
}
if _, err := tx.CreateBucketIfNotExists([]byte(pageTable)); err != nil {
return err
}
return nil
}); err != nil {
return
}
bs = &BoltStore{NewGOB(), db, cache.NewCache()}
if !bs.UserExists(defUser) {
err = bs.CreateUser(defUser, defPassword, true)
if err != nil {
return
}
}
bs.UnlockUser(defUser)
return
}
func (bs *BoltStore) Close() error {
return bs.db.Close()
}