work in progress

This commit is contained in:
ston1th 2018-09-04 23:15:47 +02:00
commit 07ff5ccffb
23 changed files with 228 additions and 240 deletions

View file

@ -3,28 +3,31 @@
package db
import (
"bytes"
"errors"
"git.giftfish.de/ston1th/gowiki/cache"
"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"
bcryptCost = 13
indexName = "Index"
welcome = `# Welcome to GoWiki
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 (
/*const (
maxResult = 1e6
maxSearchResult = 101
)
)*/
func dbErr(i interface{}) error {
return fmt.Errorf("db: %s", i)
@ -34,9 +37,13 @@ type BoltStore struct {
Marshaler
db *bolt.DB
cache *cache.Cache
index *index.Index
}
func NewBoltStore() (bs *BoltStore, err error) {
// 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
@ -52,8 +59,12 @@ func NewBoltStore() (bs *BoltStore, err error) {
}); err != nil {
return
}
bs = &BoltStore{NewGOB(), db, cache.NewCache()}
if !bs.UserExists(defUser) {
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

View file

@ -6,15 +6,17 @@ import (
"encoding/json"
"fmt"
"os"
"git.giftfish.de/ston1th/gowiki/pkg/core"
)
type Dump struct {
Articles []DumpArticle `json:"articles"`
Snippets []Snippet `json:"snippets"`
Users []User `json:"users"`
type DBDump struct {
Pages []DumpPage `json:"pages"`
Snippets []Snippet `json:"snippets"`
Users []User `json:"users"`
}
type DumpArticle struct {
type DumpPage struct {
Title string `json:"title"`
LinkTitle string `json:"link_title"`
MD string `json:"md"`
@ -29,38 +31,26 @@ func verboseLog(prefix, title string, v bool) {
}
}
func dump(path string, v bool) (err error) {
i, err := NewDumpIndex(path)
if err != nil {
return
}
defer i.Close()
func Dump(path string, v bool) (err error) {
bs, err := NewBoltStore()
if err != nil {
return
}
art, err := i.Get(false, indexName)
if err != nil {
return
}
var dump Dump
titles, err := i.Dump()
if err != nil {
return
}
for _, title := range titles {
art, err = i.Get(false, title)
var dump DBDump
//TODO bs.GetAllPages
var pages []*core.Page
for _, p := range pages {
if err != nil {
return
}
verboseLog("dump:", art.Title, v)
dump.Articles = append(dump.Articles, DumpArticle{
Title: art.Title,
LinkTitle: art.LinkTitle,
MD: art.MD,
Created: art.Created,
Updated: art.Updated,
Public: art.Public,
verboseLog("dump:", p.Title, v)
dump.Pages = append(dump.Pages, DumpPage{
Title: p.Title,
LinkTitle: p.LinkTitle,
MD: p.MD,
Created: p.Created,
Updated: p.Updated,
Public: p.Public,
})
}
//TODO snippets
@ -73,7 +63,7 @@ func dump(path string, v bool) (err error) {
return json.NewEncoder(os.Stdout).Encode(dump)
}
func restore(path string, v bool) (err error) {
func Restore(path string, v bool) (err error) {
i, err := NewIndex(path)
if err != nil {
return
@ -83,7 +73,7 @@ func restore(path string, v bool) (err error) {
if err != nil {
return
}
var dump Dump
var dump DBDump
err = json.NewDecoder(os.Stdin).Decode(&dump)
if err != nil {
return

View file

@ -1,6 +1,9 @@
package db
import "golang.org/x/crypto/sha3"
import (
"golang.org/x/crypto/sha3"
"time"
)
func validatePassword(pw string) (b []byte) {
b = []byte(pw)

View file

@ -1,6 +1,9 @@
package db
import "encoding/gob"
import (
"bytes"
"encoding/gob"
)
type Marshaler interface {
Marshal(v interface{}) ([]byte, error)

View file

@ -1,44 +1,22 @@
package db
import (
"git.giftfish.de/ston1th/gowiki/pkg/cache"
"git.giftfish.de/ston1th/gowiki/pkg/permission"
"git.giftfish.de/ston1th/gowiki/pkg/reneder"
"git.giftfish.de/ston1th/gowiki/pkg/core"
"git.giftfish.de/ston1th/gowiki/pkg/render"
"git.giftfish.de/ston1th/gowiki/pkg/util"
"github.com/boltdb/bolt"
)
type Article struct {
Title string `json:"title"`
LinkTitle string `json:"-"`
Search string `json:"search"`
Index template.HTML `json:"index"`
Text template.HTML `json:"text"`
MD string `json:"md"`
Created string `json:"created"`
Updated string `json:"updated"`
Public bool `json:"public"`
}
type Page struct {
Title string `json:"title"`
Markdown string `json:"markdown"`
Index template.HTML `json:"index"`
Text template.HTML `json:"text"`
Created string `json:"created"`
Updated string `json:"updated"`
Owner string `json:"owner"`
Perm permission.Permission `json:"perm"`
}
var (
errPageNotFound = dbErr("page not found")
errInvalidPermission = dbErr("invalid permission")
errPrivateWikiPage = dbErr("private wiki pages are not allowed")
)
func (bs *BoltStore) GetPage(title, username string) (p *Page, err error) {
func (bs *BoltStore) GetPage(title, username string) (p *core.Page, err error) {
p = bs.cache.Get(title)
if p == nil {
p = new(Page)
p = new(core.Page)
err = bs.db.View(func(tx *bolt.Tx) error {
v := tx.Bucket([]byte(pageTable)).Get([]byte(title))
if v == nil {
@ -47,43 +25,38 @@ func (bs *BoltStore) GetPage(title, username string) (p *Page, err error) {
return bs.Unmarshal(v, p)
})
if err != nil {
return err
return
}
bs.cache.Add(title, p)
}
if !permission.Read(username, p) {
if !core.ReadPerm(username, p) {
err = errPageNotFound
}
return
}
func (bs *BoltStore) pageExists(title, username string) (page *Page, err error) {
page, err = bs.GetPage(title)
if err != nil {
return
}
if !permission.Verify(username, page) {
err = errPageNotFound
}
// TODO access db not cache
func (bs *BoltStore) pageExists(title, username string) (p *core.Page, err error) {
p, err = bs.GetPage(title, username)
return
}
func (bs *BoltStore) CreatePage(title, section, markdown, username string, perm int) (page *Page, err error) {
p := permission.Parse(perm)
if p == permission.Invalid {
func (bs *BoltStore) CreatePage(title, section, markdown, username string, perm int) (page *core.Page, err error) {
p := core.ParsePerm(perm)
if p == core.Invalid {
err = errInvalidPermission
return
}
if section == wikiSection && p == permission.Private {
if section == wikiSection && p == core.Private {
err = errPrivateWikiPage
return
}
st := storeTitle(section, title)
st := util.StoreTitle(section, title)
if _, err = bs.pageExists(st, username); err != nil {
return
}
page = &Page{
page = &core.Page{
Title: title,
Markdown: markdown,
Created: created(username),
@ -103,25 +76,25 @@ func (bs *BoltStore) CreatePage(title, section, markdown, username string, perm
return
}
err = index.Add(title, search)
err = bs.index.Add(title, st, search)
if err != nil {
return
}
cache.Add(st, page)
bs.cache.Add(st, page)
return
}
func (bs *BoltStore) UpdatePage(title, section, markdown, username string, perm int) (page *Page, err error) {
p := permission.Parse(perm)
if p == permission.Invalid {
func (bs *BoltStore) UpdatePage(title, section, markdown, username string, perm int) (page *core.Page, err error) {
p := core.ParsePerm(perm)
if p == core.Invalid {
err = errInvalidPermission
return
}
if section == wikiSection && p == permission.Private {
if section == wikiSection && p == core.Private {
err = errPrivateWikiPage
return
}
st := storeTitle(section, title)
st := util.StoreTitle(section, title)
page, err = bs.pageExists(st, username)
if err != nil {
return
@ -143,20 +116,23 @@ func (bs *BoltStore) UpdatePage(title, section, markdown, username string, perm
return
}
err = index.Add(title, search)
err = bs.index.Add(title, st, search)
if err != nil {
return
}
cache.Add(st, page)
bs.cache.Add(st, page)
return
}
func (bs *BoltStore) DeletePage(title, section, username string) error {
st := storeTitle(section, title)
st := util.StoreTitle(section, title)
if _, err := bs.pageExists(st, username); err != nil {
return err
}
return bs.db.Update(func(tx *bolt.Tx) error {
if err := bs.db.Update(func(tx *bolt.Tx) error {
return tx.Bucket([]byte(pageTable)).Delete([]byte(st))
})
}); err != nil {
return err
}
return bs.index.Delete(st)
}

View file

@ -2,6 +2,7 @@ package db
import (
"bytes"
"git.giftfish.de/ston1th/gowiki/pkg/render"
"github.com/boltdb/bolt"
"html/template"
"strings"
@ -25,9 +26,8 @@ func copyBuf(buf []byte) (b []byte) {
}
var (
errSnippetNotExists = dbErr("snippet does not exist")
errSnippetExists = dbErr("snippet already exist")
errInvalidPermission = dbErr("invalid permission")
errSnippetNotExists = dbErr("snippet does not exist")
errSnippetExists = dbErr("snippet already exist")
)
func (bs *BoltStore) GetSnippets(username string) (snip []Snippet, err error) {
@ -100,7 +100,7 @@ func checkPerm(p int) error {
}
func (bs *BoltStore) CreateSnippet(username, title, text string, perm int) (newTitle string, err error) {
newTitle, title = makeLinkTitle(title)
//newTitle, title = makeLinkTitle(title)
err = checkPerm(perm)
if err != nil {
return
@ -111,7 +111,7 @@ func (bs *BoltStore) CreateSnippet(username, title, text string, perm int) (newT
return
}
err = bs.db.Update(func(tx *bolt.Tx) error {
snip := render(text).Snippet()
snip := render.Render(text).Snippet()
snip.Title = title
snip.Created = username + " " + now()
snip.Permission = perm
@ -134,7 +134,7 @@ func (bs *BoltStore) EditSnippet(username, title, text string, perm int) (err er
return
}
err = bs.db.Update(func(tx *bolt.Tx) error {
snip := render(text).Snippet()
snip := render.Render(text).Snippet()
snip.Title = title
snip.Created = username + " " + now()
snip.Permission = perm

View file

@ -5,6 +5,13 @@ import (
"golang.org/x/crypto/bcrypt"
)
type User struct {
Username string
Password string
Admin bool
Locked int
}
var (
errUserNotFound = dbErr("user not found")
errUserExists = dbErr("user already exist")
@ -28,7 +35,7 @@ func (bs *BoltStore) GetUser(username string) (u User, err error) {
err = bs.db.View(func(tx *bolt.Tx) error {
v := tx.Bucket([]byte(userTable)).Get([]byte(username))
if v == nil {
return errUserNotExists
return errUserNotFound
}
return bs.Unmarshal(v, &u)
})
@ -47,7 +54,7 @@ func (bs *BoltStore) UserIsAdmin(username string) (admin bool) {
func (bs *BoltStore) UserExists(username string) error {
return bs.db.View(func(tx *bolt.Tx) error {
if tx.Bucket([]byte(userTable)).Get([]byte(username)) == nil {
return errUserNotExists
return errUserNoFound
}
return nil
})