79 lines
1.5 KiB
Go
79 lines
1.5 KiB
Go
// Copyright (C) 2017 Marius Schellenberger
|
|
|
|
package db
|
|
|
|
import (
|
|
"fmt"
|
|
"git.giftfish.de/ston1th/gowiki/pkg/cache"
|
|
"git.giftfish.de/ston1th/gowiki/pkg/index"
|
|
"github.com/boltdb/bolt"
|
|
)
|
|
|
|
const (
|
|
dbFile = "gowiki.db"
|
|
defUser = "admin"
|
|
defPassword = "gowiki"
|
|
userTable = "user"
|
|
pageTable = "page"
|
|
snippetTable = "snippet"
|
|
bcryptCost = 13
|
|
indexName = "Index"
|
|
welcome = `# Welcome to GoWiki
|
|
This is the [Index](/wiki/Index) page.
|
|
You can customize it how you like.`
|
|
wikiSection = "wiki"
|
|
)
|
|
|
|
/*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
|
|
index *index.Index
|
|
}
|
|
|
|
// TODO create initial wiki page
|
|
// TODO init index
|
|
|
|
func NewBoltStore(indexPath string) (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
|
|
}
|
|
i, err := index.NewIndex(indexPath)
|
|
if err != nil {
|
|
return
|
|
}
|
|
bs = &BoltStore{NewGOB(), db, cache.NewCache(), i}
|
|
if bs.UserExists(defUser) == nil {
|
|
err = bs.CreateUser(defUser, defPassword, true)
|
|
if err != nil {
|
|
return
|
|
}
|
|
}
|
|
bs.UnlockUser(defUser)
|
|
return
|
|
}
|
|
|
|
func (bs *BoltStore) Close() error {
|
|
return bs.db.Close()
|
|
}
|