mostly finished db pages
This commit is contained in:
parent
c5d41b0965
commit
38f7c31517
10 changed files with 278 additions and 125 deletions
49
pkg/cache/cache.go
vendored
Normal file
49
pkg/cache/cache.go
vendored
Normal file
|
|
@ -0,0 +1,49 @@
|
||||||
|
package cache
|
||||||
|
|
||||||
|
import (
|
||||||
|
"sync"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
type CachedPage struct {
|
||||||
|
Page *Page
|
||||||
|
CacheTime time.Time
|
||||||
|
}
|
||||||
|
|
||||||
|
type Cache struct {
|
||||||
|
sync.RWMutex
|
||||||
|
Cache map[string]*CachedPage
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewCache() *Cache {
|
||||||
|
return &Cache{Cache: make(map[string]*CachedPage)}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *Cache) Add(title string, p *Page) {
|
||||||
|
now := time.Now()
|
||||||
|
c.Lock()
|
||||||
|
defer c.Unlock()
|
||||||
|
cp, ok := c.Cache[title]
|
||||||
|
if ok && now.After(c.CacheTime) {
|
||||||
|
cp.Page = p
|
||||||
|
cp.CacheTime = now
|
||||||
|
c.Cache[title] = cp
|
||||||
|
return
|
||||||
|
}
|
||||||
|
c.Cache[title] = &CachedPage{p, now}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *Cache) Get(title string) *Page {
|
||||||
|
c.RLock()
|
||||||
|
defer c.RUnlock()
|
||||||
|
if cp, ok := c.Cache[title]; ok {
|
||||||
|
return cp.Page
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *Cache) Delete(title string) {
|
||||||
|
c.Lock()
|
||||||
|
delete(c.Cache, title)
|
||||||
|
c.Unlock()
|
||||||
|
}
|
||||||
13
pkg/db/db.go
13
pkg/db/db.go
|
|
@ -5,6 +5,7 @@ package db
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"errors"
|
"errors"
|
||||||
|
"git.giftfish.de/ston1th/gowiki/cache"
|
||||||
"github.com/boltdb/bolt"
|
"github.com/boltdb/bolt"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
@ -25,18 +26,14 @@ const (
|
||||||
maxSearchResult = 101
|
maxSearchResult = 101
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
|
||||||
errUserNotExists = dbErr("user does not exist")
|
|
||||||
errUserExists = dbErr("user already exist")
|
|
||||||
)
|
|
||||||
|
|
||||||
func dbErr(i interface{}) error {
|
func dbErr(i interface{}) error {
|
||||||
return fmt.Errorf("db: %s", i)
|
return fmt.Errorf("db: %s", i)
|
||||||
}
|
}
|
||||||
|
|
||||||
type BoltStore struct {
|
type BoltStore struct {
|
||||||
Marshaler
|
Marshaler
|
||||||
db *bolt.DB
|
db *bolt.DB
|
||||||
|
cache *cache.Cache
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewBoltStore() (bs *BoltStore, err error) {
|
func NewBoltStore() (bs *BoltStore, err error) {
|
||||||
|
|
@ -48,14 +45,14 @@ func NewBoltStore() (bs *BoltStore, err error) {
|
||||||
if _, err := tx.CreateBucketIfNotExists([]byte(userTable)); err != nil {
|
if _, err := tx.CreateBucketIfNotExists([]byte(userTable)); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if _, err := tx.CreateBucketIfNotExists([]byte(pagesTable)); err != nil {
|
if _, err := tx.CreateBucketIfNotExists([]byte(pageTable)); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}); err != nil {
|
}); err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
bs = &BoltStore{NewGOB(), db}
|
bs = &BoltStore{NewGOB(), db, cache.NewCache()}
|
||||||
if !bs.UserExists(defUser) {
|
if !bs.UserExists(defUser) {
|
||||||
err = bs.CreateUser(defUser, defPassword, true)
|
err = bs.CreateUser(defUser, defPassword, true)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
|
||||||
|
|
@ -12,3 +12,13 @@ func validatePassword(pw string) (b []byte) {
|
||||||
b = hash.Sum(nil)
|
b = hash.Sum(nil)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const timeFmt = "2006-01-02 15:04:05"
|
||||||
|
|
||||||
|
func now() string {
|
||||||
|
return time.Now().Format(timeFmt)
|
||||||
|
}
|
||||||
|
|
||||||
|
func created(username string) string {
|
||||||
|
return username + " " + now()
|
||||||
|
}
|
||||||
|
|
|
||||||
164
pkg/db/page.go
164
pkg/db/page.go
|
|
@ -1,5 +1,11 @@
|
||||||
package db
|
package db
|
||||||
|
|
||||||
|
import (
|
||||||
|
"git.giftfish.de/ston1th/gowiki/pkg/cache"
|
||||||
|
"git.giftfish.de/ston1th/gowiki/pkg/permission"
|
||||||
|
"git.giftfish.de/ston1th/gowiki/pkg/reneder"
|
||||||
|
)
|
||||||
|
|
||||||
type Article struct {
|
type Article struct {
|
||||||
Title string `json:"title"`
|
Title string `json:"title"`
|
||||||
LinkTitle string `json:"-"`
|
LinkTitle string `json:"-"`
|
||||||
|
|
@ -12,31 +18,145 @@ type Article struct {
|
||||||
Public bool `json:"public"`
|
Public bool `json:"public"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type Permission int
|
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"`
|
||||||
|
}
|
||||||
|
|
||||||
const (
|
var (
|
||||||
Public Permission = iota
|
errPageNotFound = dbErr("page not found")
|
||||||
Internal
|
errInvalidPermission = dbErr("invalid permission")
|
||||||
Private
|
errPrivateWikiPage = dbErr("private wiki pages are not allowed")
|
||||||
)
|
)
|
||||||
|
|
||||||
type Page struct {
|
func (bs *BoltStore) GetPage(title, username string) (p *Page, err error) {
|
||||||
Title string `json:"title"`
|
p = bs.cache.Get(title)
|
||||||
Markdown string `json:"markdown"`
|
if p == nil {
|
||||||
Index template.HTML `json:"index"`
|
p = new(Page)
|
||||||
Text template.HTML `json:"text"`
|
err = bs.db.View(func(tx *bolt.Tx) error {
|
||||||
Created string `json:"created"`
|
v := tx.Bucket([]byte(pageTable)).Get([]byte(title))
|
||||||
Updated string `json:"updated"`
|
if v == nil {
|
||||||
Perm Permission `json:"perm"`
|
return errPageNotFound
|
||||||
}
|
}
|
||||||
|
return bs.Unmarshal(v, p)
|
||||||
func (bs *BoltStore) GetPage(title string) (p Page, err error) {
|
})
|
||||||
err = bs.db.View(func(tx *bolt.Tx) error {
|
if err != nil {
|
||||||
v := tx.Bucket([]byte(pageTable)).Get([]byte(username))
|
return err
|
||||||
if v == nil {
|
|
||||||
return errUserNotExists
|
|
||||||
}
|
}
|
||||||
return bs.Unmarshal(v, &u)
|
bs.cache.Add(title, p)
|
||||||
})
|
}
|
||||||
|
if !permission.Read(username, p) {
|
||||||
|
err = errPageNotFound
|
||||||
|
}
|
||||||
return
|
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
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
func (bs *BoltStore) CreatePage(title, section, markdown, username string, perm int) (page *Page, err error) {
|
||||||
|
p := permission.Parse(perm)
|
||||||
|
if p == permission.Invalid {
|
||||||
|
err = errInvalidPermission
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if section == wikiSection && p == permission.Private {
|
||||||
|
err = errPrivateWikiPage
|
||||||
|
return
|
||||||
|
}
|
||||||
|
st := storeTitle(section, title)
|
||||||
|
if _, err = bs.pageExists(st, username); err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
page = &Page{
|
||||||
|
Title: title,
|
||||||
|
Markdown: markdown,
|
||||||
|
Created: created(username),
|
||||||
|
Owner: username,
|
||||||
|
Perm: p,
|
||||||
|
}
|
||||||
|
search := render.Render(page)
|
||||||
|
|
||||||
|
err = bs.db.Update(func(tx *bolt.Tx) error {
|
||||||
|
v, err := bs.Marshal(page)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return tx.Bucket([]byte(pageTable)).Put([]byte(st), v)
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
err = index.Add(title, search)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
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 {
|
||||||
|
err = errInvalidPermission
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if section == wikiSection && p == permission.Private {
|
||||||
|
err = errPrivateWikiPage
|
||||||
|
return
|
||||||
|
}
|
||||||
|
st := storeTitle(section, title)
|
||||||
|
page, err = bs.pageExists(st, username)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
page.Markdown = markdown
|
||||||
|
page.Updated = created(username)
|
||||||
|
page.Perm = p
|
||||||
|
search := render.Render(page)
|
||||||
|
|
||||||
|
err = bs.db.Update(func(tx *bolt.Tx) error {
|
||||||
|
v, err := bs.Marshal(page)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return tx.Bucket([]byte(pageTable)).Put([]byte(st), v)
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
err = index.Add(title, search)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
cache.Add(st, page)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
func (bs *BoltStore) DeletePage(title, section, username string) error {
|
||||||
|
st := storeTitle(section, title)
|
||||||
|
if _, err := bs.pageExists(st, username); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return bs.db.Update(func(tx *bolt.Tx) error {
|
||||||
|
return tx.Bucket([]byte(pageTable)).Delete([]byte(st))
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -5,6 +5,11 @@ import (
|
||||||
"golang.org/x/crypto/bcrypt"
|
"golang.org/x/crypto/bcrypt"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
errUserNotFound = dbErr("user not found")
|
||||||
|
errUserExists = dbErr("user already exist")
|
||||||
|
)
|
||||||
|
|
||||||
func (bs *BoltStore) GetUsers() (users []User, err error) {
|
func (bs *BoltStore) GetUsers() (users []User, err error) {
|
||||||
users, err = bs.DumpUsers()
|
users, err = bs.DumpUsers()
|
||||||
for i := range users {
|
for i := range users {
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,6 @@
|
||||||
// Copyright (C) 2017 Marius Schellenberger
|
// Copyright (C) 2017 Marius Schellenberger
|
||||||
|
|
||||||
package main
|
package index
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
@ -10,7 +10,7 @@ import (
|
||||||
"github.com/blevesearch/bleve/document"
|
"github.com/blevesearch/bleve/document"
|
||||||
)
|
)
|
||||||
|
|
||||||
type IndexPage struct {
|
type indexPage struct {
|
||||||
Title string `json:"title"`
|
Title string `json:"title"`
|
||||||
Search string `json:"search"`
|
Search string `json:"search"`
|
||||||
}
|
}
|
||||||
|
|
@ -50,29 +50,19 @@ func NewIndex(path string) (i *Index, err error) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewDumpIndex(path string) (i *Index, err error) {
|
|
||||||
bi, err := bleve.OpenUsing(path, map[string]interface{}{"read_only": true})
|
|
||||||
if err != nil {
|
|
||||||
err = indexErr(err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
i = &Index{index: bi}
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
func (i *Index) Close() error {
|
func (i *Index) Close() error {
|
||||||
return i.i.Close()
|
return i.i.Close()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (i *Index) Add(ip IndexPage) error {
|
func (i *Index) Add(title, search string) error {
|
||||||
doc, _ := i.i.Document(ip.Title)
|
doc, _ := i.i.Document(title)
|
||||||
if doc != nil {
|
if doc != nil {
|
||||||
err := i.Delete(ip.Title)
|
err := i.Delete(title)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return i.i.Index(ip.Title, ip)
|
return i.i.Index(title, indexPage{title, search})
|
||||||
}
|
}
|
||||||
|
|
||||||
func (i *Index) Delete(title string) error {
|
func (i *Index) Delete(title string) error {
|
||||||
|
|
|
||||||
47
pkg/permission/permission.go
Normal file
47
pkg/permission/permission.go
Normal file
|
|
@ -0,0 +1,47 @@
|
||||||
|
package permission
|
||||||
|
|
||||||
|
type Permission int
|
||||||
|
|
||||||
|
const (
|
||||||
|
Invalid Permission = iota
|
||||||
|
Public
|
||||||
|
Internal
|
||||||
|
Private
|
||||||
|
)
|
||||||
|
|
||||||
|
func Parse(p int) Permission {
|
||||||
|
switch p {
|
||||||
|
case Public:
|
||||||
|
return Public
|
||||||
|
case Internal:
|
||||||
|
return Internal
|
||||||
|
case Private:
|
||||||
|
return Private
|
||||||
|
}
|
||||||
|
return Invalid
|
||||||
|
}
|
||||||
|
|
||||||
|
func Read(username string, p *Page) bool {
|
||||||
|
switch p.Perm {
|
||||||
|
case Public:
|
||||||
|
return true
|
||||||
|
case Internal:
|
||||||
|
return username != ""
|
||||||
|
case Private:
|
||||||
|
return username == p.Owner
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
func Write(username string, p *Page) bool {
|
||||||
|
switch p.Perm {
|
||||||
|
case Public, Internal:
|
||||||
|
if p.Owner == "" {
|
||||||
|
return username != ""
|
||||||
|
}
|
||||||
|
return username == p.Owner
|
||||||
|
case Private:
|
||||||
|
return username == p.Owner
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
@ -1,6 +1,6 @@
|
||||||
// Copyright (C) 2017 Marius Schellenberger
|
// Copyright (C) 2017 Marius Schellenberger
|
||||||
|
|
||||||
package main
|
package render
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"html/template"
|
"html/template"
|
||||||
|
|
@ -28,42 +28,15 @@ var (
|
||||||
titleRe = regexp.MustCompile("[^a-zA-Z0-9 -]+")
|
titleRe = regexp.MustCompile("[^a-zA-Z0-9 -]+")
|
||||||
)
|
)
|
||||||
|
|
||||||
type rendered struct {
|
func Render(p *Page) string {
|
||||||
Index template.HTML
|
rend := blackfriday.MarkdownCommon([]byte(p.Markdown))
|
||||||
Text template.HTML
|
|
||||||
MD string
|
|
||||||
Raw []byte
|
|
||||||
}
|
|
||||||
|
|
||||||
func (r *rendered) Article() *Article {
|
|
||||||
return &Article{
|
|
||||||
Index: r.Index,
|
|
||||||
Text: r.Text,
|
|
||||||
MD: r.MD,
|
|
||||||
Search: whiteSpace.ReplaceAllString(string(filter.Filter(r.Raw)), " "),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (r *rendered) Snippet() *Snippet {
|
|
||||||
return &Snippet{
|
|
||||||
Index: r.Index,
|
|
||||||
Text: r.Text,
|
|
||||||
MD: r.MD,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func render(text string) *rendered {
|
|
||||||
rend := blackfriday.MarkdownCommon([]byte(text))
|
|
||||||
index, html := buildIndex(string(rend))
|
index, html := buildIndex(string(rend))
|
||||||
html = strings.Replace(html, "<table>", `<table class="table">`, -1)
|
html = strings.Replace(html, "<table>", `<table class="table">`, -1)
|
||||||
html = strings.Replace(html, "</h1>", `</h1><hr>`, -1)
|
html = strings.Replace(html, "</h1>", `</h1><hr>`, -1)
|
||||||
html = strings.Replace(html, "<a", `<a target="_blank"`, -1)
|
html = strings.Replace(html, "<a", `<a target="_blank"`, -1)
|
||||||
return &rendered{
|
p.Index = template.HTML(index)
|
||||||
Index: template.HTML(index),
|
p.Text = template.HTML(html)
|
||||||
Text: template.HTML(html),
|
return whiteSpace.ReplaceAllString(string(filter.Filter(rend)), " ")
|
||||||
MD: text,
|
|
||||||
Raw: rend,
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func spaceReplace(s string) string {
|
func spaceReplace(s string) string {
|
||||||
|
|
@ -1,49 +0,0 @@
|
||||||
package main
|
|
||||||
|
|
||||||
import (
|
|
||||||
"sync"
|
|
||||||
"time"
|
|
||||||
)
|
|
||||||
|
|
||||||
type CachedArticle struct {
|
|
||||||
Article *Article
|
|
||||||
CacheTime time.Time
|
|
||||||
}
|
|
||||||
|
|
||||||
type IndexCache struct {
|
|
||||||
sync.RWMutex
|
|
||||||
Cache map[string]*CachedArticle
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewIndexCache() *IndexCache {
|
|
||||||
return &IndexCache{Cache: make(map[string]*CachedArticle)}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (ic IndexCache) Add(title string, a *Article) {
|
|
||||||
now := time.Now()
|
|
||||||
ic.Lock()
|
|
||||||
defer ic.Unlock()
|
|
||||||
c, ok := ic.Cache[title]
|
|
||||||
if ok && now.After(c.CacheTime) {
|
|
||||||
c.Article = a
|
|
||||||
c.CacheTime = now
|
|
||||||
ic.Cache[title] = c
|
|
||||||
return
|
|
||||||
}
|
|
||||||
ic.Cache[title] = &CachedArticle{a, now}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (ic IndexCache) Get(title string) *Article {
|
|
||||||
ic.RLock()
|
|
||||||
defer ic.RUnlock()
|
|
||||||
if a, ok := ic.Cache[title]; ok {
|
|
||||||
return a.Article
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (ic *IndexCache) Delete(title string) {
|
|
||||||
ic.Lock()
|
|
||||||
delete(ic.Cache, title)
|
|
||||||
ic.Unlock()
|
|
||||||
}
|
|
||||||
|
|
@ -3,6 +3,7 @@ package main
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
@ -34,3 +35,13 @@ func since(t time.Time) string {
|
||||||
return fmt.Sprintf("%.fms", f)
|
return fmt.Sprintf("%.fms", f)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
func storeTitle(section, title string) string {
|
||||||
|
return section + "/" + title
|
||||||
|
}
|
||||||
|
|
||||||
|
func unstoreTitle(title string) (section, title string) {
|
||||||
|
if a := strings.Split(title, "/"); len(a) == 2 {
|
||||||
|
return a[0], a[1]
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue