gowiki/pkg/db/db.go

70 lines
1.3 KiB
Go

// Copyright (C) 2018 Marius Schellenberger
package db
import (
"fmt"
"git.giftfish.de/ston1th/gowiki/pkg/cache"
"git.giftfish.de/ston1th/gowiki/pkg/core"
"git.giftfish.de/ston1th/gowiki/pkg/index"
"git.giftfish.de/ston1th/gowiki/pkg/store"
)
const (
defUser = "admin"
defPassword = "gowiki"
storeFile = "store.db"
bcryptCost = 13
welcome = `# Welcome to GoWiki
This is the [Index](/wiki/Index) page.
You can customize it how you like.`
)
const blevePath = "bleve"
func dbErr(i interface{}) error {
return fmt.Errorf("db: %s", i)
}
type DB struct {
store store.Store
cache *cache.Cache
Index *index.Index
}
func New(datadir string) (db *DB, err error) {
db = &DB{cache: cache.NewCache()}
db.Index, err = index.NewIndex(datadir + "/" + blevePath)
if err != nil {
return
}
db.store, err = store.NewBoltStore(storeFile, nil)
if err != nil {
return
}
if db.UserExists(defUser) != nil {
err = db.CreateUser(defUser, defPassword, true)
if err != nil {
return
}
}
err = db.UnlockUser(defUser)
if err != nil {
return
}
_, err = db.CreatePage(core.IndexPage, core.WikiSection, welcome, defUser, core.Public)
if err != nil && err != store.ErrKeyNotFound {
return
}
return
}
func (db *DB) Close() error {
err := db.store.Close()
if err != nil {
return err
}
return db.Index.Close()
}