Compare commits

..

No commits in common. "da11811e2dfd789519532f0f953e05412f0d84d3" and "e7d2c0e1a931c20bd73279dddd3296ebaf3978be" have entirely different histories.

4 changed files with 22 additions and 107 deletions

View File

@ -25,56 +25,23 @@ import (
)
type BlockRepository interface {
Save(id string, block Block) error
Save(block Block) error
Load(id string) (Block, error)
}
type blockSaveMessage struct {
id string
block Block
}
type blockRepo struct {
dirname string
saveC chan blockSaveMessage
errC chan error
}
func saveBlock(dirname, id string, block Block) error {
f, err := os.OpenFile(filepath.Join(dirname, BlocksDirectory, id), os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0644)
if err != nil {
return err
}
defer f.Close()
enc := json.NewEncoder(f)
enc.SetIndent("", " ")
err = enc.Encode(&block)
if err != nil {
return err
}
return nil
}
func NewBlockRepo(dirname string) BlockRepository {
saveC := make(chan blockSaveMessage, 1)
errC := make(chan error)
go func() {
for msg := range saveC {
err := saveBlock(dirname, msg.id, msg.block)
errC <- err
}
}()
return &blockRepo{
dirname: dirname,
saveC: saveC,
errC: errC,
}
}
func (br *blockRepo) Save(id string, block Block) error {
br.saveC <- blockSaveMessage{id, block}
err := <-br.errC
return err
f, err := os.OpenFile(filepath.Join(br.dirname, BlocksDirectory, id), os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0644)
defer f.Close()
if err != nil {
return err
}
enc := json.NewEncoder(f)
enc.SetIndent("", " ")
return enc.Encode(&block)
}
func (br *blockRepo) Load(id string) (Block, error) {

20
file.go
View File

@ -337,19 +337,14 @@ func (fp *FilePages) save(msg saveMessage) error {
if err != nil {
return fmt.Errorf("while creating search object %s: %w", page.Name, err)
}
batch := fp.index.NewBatch()
for _, so := range searchObjects {
if fp.index != nil {
err = batch.Index(so.ID, so)
err = fp.index.Index(so.ID, so)
if err != nil {
return fmt.Errorf("while indexing %s: %w", page.Name, err)
}
}
}
err = fp.index.Batch(batch)
if err != nil {
return fmt.Errorf("while indexing %s: %w", page.Name, err)
}
sw.Stop()
sw.Start("links")
err = saveLinksIncremental(fp.dirname, page.Title)
@ -704,9 +699,6 @@ func (fp *FilePages) PageHistory(p string) ([]Revision, error) {
commitId := line[0:start]
rest := line[start+1:]
pageText := gitRevision(fp.dirname, page, commitId)
if pageText == "" {
return nil, errors.New("git revision failed")
}
revisions = append(revisions, Revision{
Version: commitId,
Page: DiffPage{Content: pageText},
@ -735,14 +727,8 @@ func gitRevision(dirname, page, version string) string {
cmd.Dir = dirname
buf := bytes.Buffer{}
cmd.Stdout = &buf
err := cmd.Start()
if err != nil {
return ""
}
err = cmd.Wait()
if err != nil {
return ""
}
cmd.Start()
cmd.Wait()
return buf.String()
}

View File

@ -203,10 +203,10 @@ function Store(inputData) {
function updateFold(id, open) {
let closedFolds = JSON.parse(localStorage.getItem("closed-folds") || '{}') || {}
if (open) {
delete closedFolds[id]
} else {
if (!open) {
closedFolds[id] = true
} else {
delete closedFolds[id]
}
localStorage.setItem('closed-folds', JSON.stringify(closedFolds))
}

52
main.go
View File

@ -37,6 +37,7 @@ import (
"strings"
"time"
"github.com/davecgh/go-spew/spew"
"p83.nl/go/ekster/pkg/util"
"p83.nl/go/indieauth"
"p83.nl/go/wiki/link"
@ -1063,39 +1064,8 @@ func main() {
}
mp = NewFilePages(dataDir, searchIndex)
repo := NewBlockRepo("data")
http.Handle("/auth/", &authHandler{})
http.HandleFunc("/api/block/view", wrapAuth(func(w http.ResponseWriter, r *http.Request) {
defer r.Body.Close()
if !r.Context().Value(authKey).(bool) {
http.Error(w, "Unauthorized", 401)
return
}
if r.Method != "GET" {
http.Error(w, "Method Not Allowed", http.StatusMethodNotAllowed)
return
}
id := r.URL.Query().Get("id")
block, err := repo.Load(id)
if err != nil {
http.Error(w, err.Error(), 500)
return
}
w.Header().Set("Content-Type", "application/json")
w.Header().Set("Cache-Control", "no-store")
enc := json.NewEncoder(w)
enc.SetIndent("", " ")
err = enc.Encode(block)
if err != nil {
http.Error(w, err.Error(), 500)
}
}))
http.HandleFunc("/api/block/", wrapAuth(func(w http.ResponseWriter, r *http.Request) {
defer r.Body.Close()
@ -1164,6 +1134,7 @@ func main() {
http.Error(w, "missing id", 400)
return
}
repo := blockRepo{dirname: "data"}
block, err := repo.Load(id)
block.Text = r.Form.Get("text")
err = repo.Save(id, block)
@ -1194,6 +1165,9 @@ func main() {
http.Error(w, "missing id", 400)
return
}
repo := blockRepo{
dirname: "data",
}
newBlock := Block{
Text: r.Form.Get("text"),
Children: []string{},
@ -1207,25 +1181,13 @@ func main() {
err = repo.Save(id, block)
// update search index
sw := stopwatch{}
sw.Start("createSearchObjects")
searchObjects, err := createSearchObjects(id)
batch := searchIndex.NewBatch()
spew.Dump("searchObjects", searchObjects)
for _, so := range searchObjects {
err = batch.Index(so.ID, so)
if err != nil {
log.Println(err)
}
searchIndex.Index(so.ID, so)
}
err = searchIndex.Batch(batch)
if err != nil {
log.Println(err)
}
sw.Stop()
return
}))
http.HandleFunc("/links.json", func(w http.ResponseWriter, r *http.Request) {