Compare commits
5 Commits
master
...
single-blo
Author | SHA1 | Date | |
---|---|---|---|
0b7c2323ad | |||
5bed4d4652 | |||
9184de2d1a | |||
764c67a707 | |||
f086d9d9f9 |
158
blocks.go
Normal file
158
blocks.go
Normal file
|
@ -0,0 +1,158 @@
|
||||||
|
/*
|
||||||
|
* Wiki - A wiki with editor
|
||||||
|
* Copyright (c) 2021 Peter Stuifzand
|
||||||
|
*
|
||||||
|
* This program is free software: you can redistribute it and/or modify
|
||||||
|
* it under the terms of the GNU General Public License as published by
|
||||||
|
* the Free Software Foundation, either version 3 of the License, or
|
||||||
|
* (at your option) any later version.
|
||||||
|
*
|
||||||
|
* This program is distributed in the hope that it will be useful,
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
* GNU General Public License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU General Public License
|
||||||
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
|
"log"
|
||||||
|
"os"
|
||||||
|
"path/filepath"
|
||||||
|
"sync"
|
||||||
|
)
|
||||||
|
|
||||||
|
const BlocksDirectory = "_blocks"
|
||||||
|
|
||||||
|
type FileBlockRepository struct {
|
||||||
|
dirname string
|
||||||
|
|
||||||
|
rwLock sync.RWMutex
|
||||||
|
}
|
||||||
|
|
||||||
|
type Block struct {
|
||||||
|
Text string
|
||||||
|
Children []string
|
||||||
|
Parent string
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewBlockRepo(dirname string) (*FileBlockRepository, error) {
|
||||||
|
err := os.MkdirAll(filepath.Join(dirname, BlocksDirectory), 0777)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return &FileBlockRepository{
|
||||||
|
dirname: dirname,
|
||||||
|
}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (blockRepo *FileBlockRepository) GetBlock(blockID string) (Block, error) {
|
||||||
|
blockRepo.rwLock.RLock()
|
||||||
|
defer blockRepo.rwLock.RUnlock()
|
||||||
|
|
||||||
|
return loadBlock(blockRepo.dirname, blockID)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (blockRepo *FileBlockRepository) SaveBlock(blockID string, block Block) error {
|
||||||
|
blockRepo.rwLock.Lock()
|
||||||
|
defer blockRepo.rwLock.Unlock()
|
||||||
|
return saveBlock(blockRepo.dirname, blockID, block)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (blockRepo *FileBlockRepository) GetBlocks(rootBlockID string) (BlockResponse, error) {
|
||||||
|
resp := BlockResponse{
|
||||||
|
rootBlockID,
|
||||||
|
"",
|
||||||
|
nil,
|
||||||
|
nil,
|
||||||
|
nil,
|
||||||
|
}
|
||||||
|
|
||||||
|
resp.Texts = make(map[string]string)
|
||||||
|
resp.Children = make(map[string][]string)
|
||||||
|
|
||||||
|
queue := []string{rootBlockID}
|
||||||
|
|
||||||
|
block, err := blockRepo.GetBlock(rootBlockID)
|
||||||
|
if err != nil {
|
||||||
|
return BlockResponse{}, err
|
||||||
|
}
|
||||||
|
// NOTE: what does this do?
|
||||||
|
if rootBlockID[0] != '_' && block.Children == nil {
|
||||||
|
return BlockResponse{}, fmt.Errorf("not a block and has no children: %w", BlockNotFound)
|
||||||
|
}
|
||||||
|
|
||||||
|
prevID := rootBlockID
|
||||||
|
parentID := block.Parent
|
||||||
|
|
||||||
|
for parentID != "root" {
|
||||||
|
parent, err := blockRepo.GetBlock(parentID)
|
||||||
|
if err != nil {
|
||||||
|
return BlockResponse{}, fmt.Errorf("while loading current parent block (%s->%s): %w", prevID, parentID, err)
|
||||||
|
}
|
||||||
|
|
||||||
|
resp.Texts[parentID] = parent.Text
|
||||||
|
resp.Children[parentID] = parent.Children
|
||||||
|
resp.ParentID = parentID
|
||||||
|
|
||||||
|
resp.Parents = append(resp.Parents, parentID)
|
||||||
|
|
||||||
|
prevID = parentID
|
||||||
|
parentID = parent.Parent
|
||||||
|
}
|
||||||
|
if parentID == "root" {
|
||||||
|
resp.ParentID = "root"
|
||||||
|
}
|
||||||
|
|
||||||
|
for {
|
||||||
|
if len(queue) == 0 {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
current := queue[0]
|
||||||
|
queue = queue[1:]
|
||||||
|
block, err := blockRepo.GetBlock(current)
|
||||||
|
if err != nil {
|
||||||
|
return BlockResponse{}, err
|
||||||
|
}
|
||||||
|
resp.Texts[current] = block.Text
|
||||||
|
resp.Children[current] = block.Children
|
||||||
|
queue = append(queue, block.Children...)
|
||||||
|
}
|
||||||
|
|
||||||
|
return resp, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func loadBlock(dirname, blockID string) (Block, error) {
|
||||||
|
f, err := os.Open(filepath.Join(dirname, BlocksDirectory, blockID))
|
||||||
|
if err != nil {
|
||||||
|
return Block{}, fmt.Errorf("%q: %w", blockID, BlockNotFound)
|
||||||
|
}
|
||||||
|
defer f.Close()
|
||||||
|
var block Block
|
||||||
|
err = json.NewDecoder(f).Decode(&block)
|
||||||
|
if err != nil {
|
||||||
|
return Block{}, fmt.Errorf("%q: %v", blockID, err)
|
||||||
|
}
|
||||||
|
return block, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func saveBlock(dirname, blockID string, block Block) error {
|
||||||
|
log.Printf("Writing to %q\n", blockID)
|
||||||
|
f, err := os.OpenFile(filepath.Join(dirname, BlocksDirectory, blockID), os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0644)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("while saving block %s: %w", blockID, err)
|
||||||
|
}
|
||||||
|
defer f.Close()
|
||||||
|
enc := json.NewEncoder(f)
|
||||||
|
enc.SetIndent("", " ")
|
||||||
|
err = enc.Encode(&block)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("while encoding block %s: %w", blockID, err)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
|
@ -1,59 +1,67 @@
|
||||||
import {DataSet} from "vis-data/peer";
|
// import {DataSet} from "vis-data/peer";
|
||||||
import {Network} from "vis-network/peer";
|
// import {Network} from "vis-network/peer";
|
||||||
import $ from 'jquery';
|
import $ from 'jquery';
|
||||||
|
|
||||||
function wikiGraph(selector, options) {
|
function wikiGraph(selector, options) {
|
||||||
$(selector).each(function (i, el) {
|
Promise.all([
|
||||||
let $el = $(el)
|
import("vis-data/peer"),
|
||||||
var nodeName = $el.data('name')
|
import("vis-network/peer"),
|
||||||
fetch('/api/graph?name=' + nodeName)
|
]).then(imports => {
|
||||||
.then(res => res.json())
|
const DataSet = imports[0].DataSet
|
||||||
.then(graph => {
|
const Network = imports[1].Network
|
||||||
var nodes = new DataSet(graph.nodes)
|
|
||||||
var edges = new DataSet(graph.edges)
|
|
||||||
|
|
||||||
var data = {
|
$(selector).each(function (i, el) {
|
||||||
nodes: nodes,
|
let $el = $(el)
|
||||||
edges: edges
|
var nodeName = $el.data('name')
|
||||||
};
|
fetch('/api/graph?name=' + nodeName)
|
||||||
|
.then(res => res.json())
|
||||||
|
.then(graph => {
|
||||||
|
var nodes = new DataSet(graph.nodes)
|
||||||
|
var edges = new DataSet(graph.edges)
|
||||||
|
|
||||||
var options = {
|
var data = {
|
||||||
edges: {
|
nodes: nodes,
|
||||||
arrows: 'to',
|
edges: edges
|
||||||
color: {
|
};
|
||||||
highlight: 'green'
|
|
||||||
},
|
var options = {
|
||||||
chosen: {
|
edges: {
|
||||||
edge: function (values, id, selected, hovering) {
|
arrows: 'to',
|
||||||
if (this.from === 1) {
|
color: {
|
||||||
values.color = 'blue';
|
highlight: 'green'
|
||||||
|
},
|
||||||
|
chosen: {
|
||||||
|
edge: function (values, id, selected, hovering) {
|
||||||
|
if (this.from === 1) {
|
||||||
|
values.color = 'blue';
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
},
|
||||||
},
|
|
||||||
|
|
||||||
nodes: {
|
nodes: {
|
||||||
shape: 'dot',
|
shape: 'dot',
|
||||||
size: 15,
|
size: 15,
|
||||||
font: {
|
font: {
|
||||||
background: 'white'
|
background: 'white'
|
||||||
|
}
|
||||||
|
},
|
||||||
|
layout: {
|
||||||
|
improvedLayout: true
|
||||||
}
|
}
|
||||||
},
|
};
|
||||||
layout: {
|
|
||||||
improvedLayout: true
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
var network = new Network(el, data, options);
|
var network = new Network(el, data, options);
|
||||||
network.on('doubleClick', function (props) {
|
network.on('doubleClick', function (props) {
|
||||||
if (props.nodes.length) {
|
if (props.nodes.length) {
|
||||||
let nodeId = props.nodes[0]
|
let nodeId = props.nodes[0]
|
||||||
let node = nodes.get(nodeId)
|
let node = nodes.get(nodeId)
|
||||||
// TODO: Reload directly in this same page
|
// TODO: Reload directly in this same page
|
||||||
window.location.href = '/edit/' + node.label
|
window.location.href = '/edit/' + node.label
|
||||||
}
|
}
|
||||||
|
})
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
import MarkdownIt from "markdown-it";
|
import MarkdownIt from "markdown-it";
|
||||||
import MarkdownItWikilinks from "./wikilinks";
|
import MarkdownItWikilinks from "./wikilinks";
|
||||||
import MarkdownItMark from "markdown-it-mark";
|
import MarkdownItMark from "markdown-it-mark";
|
||||||
import MarkdownItKatex from "markdown-it-katex";
|
// import MarkdownItKatex from "markdown-it-katex";
|
||||||
|
|
||||||
const MD = new MarkdownIt({
|
const MD = new MarkdownIt({
|
||||||
linkify: true,
|
linkify: true,
|
||||||
|
@ -20,6 +20,7 @@ MD.use(MarkdownItWikilinks({
|
||||||
htmlAttributes: {
|
htmlAttributes: {
|
||||||
class: 'wiki-link'
|
class: 'wiki-link'
|
||||||
},
|
},
|
||||||
})).use(MarkdownItMark).use(MarkdownItKatex)
|
})).use(MarkdownItMark)
|
||||||
|
// .use(MarkdownItKatex)
|
||||||
|
|
||||||
export default MD;
|
export default MD;
|
||||||
|
|
122
file.go
122
file.go
|
@ -40,7 +40,6 @@ import (
|
||||||
const (
|
const (
|
||||||
DocumentsFile = "_documents.json"
|
DocumentsFile = "_documents.json"
|
||||||
LinksFile = "_links.json"
|
LinksFile = "_links.json"
|
||||||
BlocksDirectory = "_blocks"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
var BlockNotFound = errors.New("block not found")
|
var BlockNotFound = errors.New("block not found")
|
||||||
|
@ -52,12 +51,6 @@ type saveMessage struct {
|
||||||
author string
|
author string
|
||||||
}
|
}
|
||||||
|
|
||||||
type Block struct {
|
|
||||||
Text string
|
|
||||||
Children []string
|
|
||||||
Parent string
|
|
||||||
}
|
|
||||||
|
|
||||||
// ListItemV2 is way to convert from old structure to new structure
|
// ListItemV2 is way to convert from old structure to new structure
|
||||||
type ListItemV2 struct {
|
type ListItemV2 struct {
|
||||||
ID ID
|
ID ID
|
||||||
|
@ -95,6 +88,8 @@ type FilePages struct {
|
||||||
dirname string
|
dirname string
|
||||||
saveC chan saveMessage
|
saveC chan saveMessage
|
||||||
index bleve.Index
|
index bleve.Index
|
||||||
|
|
||||||
|
blockRepo BlockRepository
|
||||||
}
|
}
|
||||||
|
|
||||||
type BlockResponse struct {
|
type BlockResponse struct {
|
||||||
|
@ -105,13 +100,18 @@ type BlockResponse struct {
|
||||||
Parents []string
|
Parents []string
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewFilePages(dirname string, index bleve.Index) PagesRepository {
|
func NewFilePages(dirname string, index bleve.Index) *FilePages {
|
||||||
err := os.MkdirAll(filepath.Join(dirname, "_blocks"), 0777)
|
blockRepo, err := NewBlockRepo(dirname)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalln(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
fp := &FilePages{dirname, make(chan saveMessage), index}
|
fp := &FilePages{
|
||||||
|
dirname: dirname,
|
||||||
|
saveC: make(chan saveMessage),
|
||||||
|
index: index,
|
||||||
|
blockRepo: blockRepo,
|
||||||
|
}
|
||||||
|
|
||||||
go func() {
|
go func() {
|
||||||
for msg := range fp.saveC {
|
for msg := range fp.saveC {
|
||||||
|
@ -174,7 +174,7 @@ func (fp *FilePages) Get(name string) Page {
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, name := range names {
|
for _, name := range names {
|
||||||
blocks, err := loadBlocks(fp.dirname, name)
|
blocks, err := fp.blockRepo.GetBlocks(name)
|
||||||
if err != nil && errors.Is(err, BlockNotFound) {
|
if err != nil && errors.Is(err, BlockNotFound) {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
@ -289,7 +289,7 @@ func (fp *FilePages) save(msg saveMessage) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
sw.Start("create blocks")
|
sw.Start("create blocks")
|
||||||
err := saveBlocksFromPage(fp.dirname, page)
|
err := fp.saveBlocksFromPage(fp.dirname, page)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println(err)
|
log.Println(err)
|
||||||
}
|
}
|
||||||
|
@ -333,7 +333,7 @@ func (fp *FilePages) save(msg saveMessage) error {
|
||||||
sw.Stop()
|
sw.Stop()
|
||||||
|
|
||||||
sw.Start("index")
|
sw.Start("index")
|
||||||
searchObjects, err := createSearchObjects(page.Name)
|
searchObjects, err := createSearchObjects(fp, page.Name)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("while creating search object %s: %w", page.Name, err)
|
return fmt.Errorf("while creating search object %s: %w", page.Name, err)
|
||||||
}
|
}
|
||||||
|
@ -368,7 +368,7 @@ func saveWithNewIDs(dirname string, listItems []ListItemV2, pageName string) ([]
|
||||||
return newListItems, nil
|
return newListItems, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func saveBlocksFromPage(dirname string, page Page) error {
|
func (fp* FilePages) saveBlocksFromPage(dirname string, page Page) error {
|
||||||
log.Printf("Processing: %q\n", page.Name)
|
log.Printf("Processing: %q\n", page.Name)
|
||||||
var listItems []ListItem
|
var listItems []ListItem
|
||||||
err := json.NewDecoder(strings.NewReader(page.Content)).Decode(&listItems)
|
err := json.NewDecoder(strings.NewReader(page.Content)).Decode(&listItems)
|
||||||
|
@ -386,7 +386,7 @@ func saveBlocksFromPage(dirname string, page Page) error {
|
||||||
prevList := make(map[string]ListItem)
|
prevList := make(map[string]ListItem)
|
||||||
|
|
||||||
root := "root"
|
root := "root"
|
||||||
parentBlock, err := loadBlock(dirname, page.Name)
|
parentBlock, err := fp.blockRepo.GetBlock(page.Name)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println(err)
|
log.Println(err)
|
||||||
} else {
|
} else {
|
||||||
|
@ -446,7 +446,7 @@ func saveBlocksFromPage(dirname string, page Page) error {
|
||||||
prev = &listItems[i]
|
prev = &listItems[i]
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: found out if this is still necessary
|
// TODO: find out if this is still necessary
|
||||||
log.Printf("Loading parent block: %q", rootParentID)
|
log.Printf("Loading parent block: %q", rootParentID)
|
||||||
f, err := os.Open(filepath.Join(dirname, BlocksDirectory, rootParentID))
|
f, err := os.Open(filepath.Join(dirname, BlocksDirectory, rootParentID))
|
||||||
if err == nil {
|
if err == nil {
|
||||||
|
@ -467,101 +467,15 @@ func saveBlocksFromPage(dirname string, page Page) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
for id, block := range blocks {
|
for id, block := range blocks {
|
||||||
log.Printf("Writing to %q\n", id)
|
err := fp.blockRepo.SaveBlock(id, block)
|
||||||
f, err := os.OpenFile(filepath.Join(dirname, BlocksDirectory, id), os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0644)
|
|
||||||
if err != nil {
|
|
||||||
log.Println(err)
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
enc := json.NewEncoder(f)
|
|
||||||
enc.SetIndent("", " ")
|
|
||||||
err = enc.Encode(&block)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println(err)
|
log.Println(err)
|
||||||
}
|
}
|
||||||
f.Close()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
func loadBlocks(dirname, rootBlockID string) (BlockResponse, error) {
|
|
||||||
resp := BlockResponse{
|
|
||||||
rootBlockID,
|
|
||||||
"",
|
|
||||||
nil,
|
|
||||||
nil,
|
|
||||||
nil,
|
|
||||||
}
|
|
||||||
|
|
||||||
resp.Texts = make(map[string]string)
|
|
||||||
resp.Children = make(map[string][]string)
|
|
||||||
|
|
||||||
queue := []string{rootBlockID}
|
|
||||||
|
|
||||||
block, err := loadBlock(dirname, rootBlockID)
|
|
||||||
if err != nil {
|
|
||||||
return BlockResponse{}, err
|
|
||||||
}
|
|
||||||
// NOTE: what does this do?
|
|
||||||
if rootBlockID[0] != '_' && block.Children == nil {
|
|
||||||
return BlockResponse{}, fmt.Errorf("not a block and has no children: %w", BlockNotFound)
|
|
||||||
}
|
|
||||||
|
|
||||||
prevID := rootBlockID
|
|
||||||
parentID := block.Parent
|
|
||||||
|
|
||||||
for parentID != "root" {
|
|
||||||
parent, err := loadBlock(dirname, parentID)
|
|
||||||
if err != nil {
|
|
||||||
return BlockResponse{}, fmt.Errorf("while loading current parent block (%s->%s): %w", prevID, parentID, err)
|
|
||||||
}
|
|
||||||
|
|
||||||
resp.Texts[parentID] = parent.Text
|
|
||||||
resp.Children[parentID] = parent.Children
|
|
||||||
resp.ParentID = parentID
|
|
||||||
|
|
||||||
resp.Parents = append(resp.Parents, parentID)
|
|
||||||
|
|
||||||
prevID = parentID
|
|
||||||
parentID = parent.Parent
|
|
||||||
}
|
|
||||||
if parentID == "root" {
|
|
||||||
resp.ParentID = "root"
|
|
||||||
}
|
|
||||||
|
|
||||||
for {
|
|
||||||
if len(queue) == 0 {
|
|
||||||
break
|
|
||||||
}
|
|
||||||
current := queue[0]
|
|
||||||
queue = queue[1:]
|
|
||||||
block, err := loadBlock(dirname, current)
|
|
||||||
if err != nil {
|
|
||||||
return BlockResponse{}, err
|
|
||||||
}
|
|
||||||
resp.Texts[current] = block.Text
|
|
||||||
resp.Children[current] = block.Children
|
|
||||||
queue = append(queue, block.Children...)
|
|
||||||
}
|
|
||||||
|
|
||||||
return resp, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func loadBlock(dirname, blockID string) (Block, error) {
|
|
||||||
f, err := os.Open(filepath.Join(dirname, BlocksDirectory, blockID))
|
|
||||||
if err != nil {
|
|
||||||
return Block{}, fmt.Errorf("%q: %w", blockID, BlockNotFound)
|
|
||||||
}
|
|
||||||
defer f.Close()
|
|
||||||
var block Block
|
|
||||||
err = json.NewDecoder(f).Decode(&block)
|
|
||||||
if err != nil {
|
|
||||||
return Block{}, fmt.Errorf("%q: %v", blockID, err)
|
|
||||||
}
|
|
||||||
return block, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func saveLinksIncremental(dirname, title string) error {
|
func saveLinksIncremental(dirname, title string) error {
|
||||||
type Document struct {
|
type Document struct {
|
||||||
Title string `json:"title"`
|
Title string `json:"title"`
|
||||||
|
|
14
main.go
14
main.go
|
@ -52,8 +52,14 @@ var (
|
||||||
authKey = authorizedKey("authorizedKey")
|
authKey = authorizedKey("authorizedKey")
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type BlockRepository interface {
|
||||||
|
GetBlock(id string) (Block, error)
|
||||||
|
GetBlocks(rootBlockID string) (BlockResponse, error)
|
||||||
|
SaveBlock(id string, block Block) error
|
||||||
|
}
|
||||||
|
|
||||||
var (
|
var (
|
||||||
mp PagesRepository
|
mp PagesRepository
|
||||||
|
|
||||||
port = flag.Int("port", 8080, "listen port")
|
port = flag.Int("port", 8080, "listen port")
|
||||||
baseurl = flag.String("baseurl", "", "baseurl")
|
baseurl = flag.String("baseurl", "", "baseurl")
|
||||||
|
@ -745,6 +751,7 @@ func (h *indexHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||||
|
|
||||||
sess, err := NewSession(w, r)
|
sess, err := NewSession(w, r)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
log.Println("NewSession", err)
|
||||||
http.Error(w, err.Error(), 500)
|
http.Error(w, err.Error(), 500)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
@ -805,6 +812,7 @@ func (h *indexHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||||
} else if format == "metakv" {
|
} else if format == "metakv" {
|
||||||
so, err := createStructuredFormat(mpPage)
|
so, err := createStructuredFormat(mpPage)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
log.Println("createStructuredFormat", err)
|
||||||
http.Error(w, err.Error(), 500)
|
http.Error(w, err.Error(), 500)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
@ -814,6 +822,7 @@ func (h *indexHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||||
enc.SetIndent("", " ")
|
enc.SetIndent("", " ")
|
||||||
err = enc.Encode(so)
|
err = enc.Encode(so)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
log.Println("Encode", err)
|
||||||
http.Error(w, err.Error(), 500)
|
http.Error(w, err.Error(), 500)
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
|
@ -1103,6 +1112,7 @@ func main() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}))
|
}))
|
||||||
|
http.HandleFunc("/api/block/update", wrapAuth(func(w http.ResponseWriter, r *http.Request) {}))
|
||||||
http.HandleFunc("/api/block/append", wrapAuth(func(w http.ResponseWriter, r *http.Request) {
|
http.HandleFunc("/api/block/append", wrapAuth(func(w http.ResponseWriter, r *http.Request) {
|
||||||
err := r.ParseForm()
|
err := r.ParseForm()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -1284,7 +1294,7 @@ func createSearchIndex(dataDir, indexName string) (bleve.Index, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, page := range pages {
|
for _, page := range pages {
|
||||||
searchObjects, err := createSearchObjects(page.Name)
|
searchObjects, err := createSearchObjects(fp, page.Name)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println(err)
|
log.Println(err)
|
||||||
continue
|
continue
|
||||||
|
|
|
@ -115,7 +115,7 @@ func (s *searchHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, page := range pages {
|
for _, page := range pages {
|
||||||
err = saveBlocksFromPage("data", page)
|
err = mp.saveBlocksFromPage("data", page)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("error while processing blocks from page %s: %v", page.Name, err)
|
log.Printf("error while processing blocks from page %s: %v", page.Name, err)
|
||||||
continue
|
continue
|
||||||
|
@ -168,7 +168,7 @@ func (s *searchHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, page := range pages {
|
for _, page := range pages {
|
||||||
searchObjects, err := createSearchObjects(page.Name)
|
searchObjects, err := createSearchObjects(mp, page.Name)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Printf("error while creating search object %s: %v", page.Title, err)
|
log.Printf("error while creating search object %s: %v", page.Title, err)
|
||||||
continue
|
continue
|
||||||
|
@ -246,8 +246,8 @@ func (p pageBlock) Type() string {
|
||||||
return "block"
|
return "block"
|
||||||
}
|
}
|
||||||
|
|
||||||
func createSearchObjects(rootBlockID string) ([]pageBlock, error) {
|
func createSearchObjects(fp *FilePages, rootBlockID string) ([]pageBlock, error) {
|
||||||
blocks, err := loadBlocks("data", rootBlockID)
|
blocks, err := fp.blockRepo.GetBlocks(rootBlockID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
18
session.go
18
session.go
|
@ -24,6 +24,7 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -40,12 +41,12 @@ type Session struct {
|
||||||
func NewSession(w http.ResponseWriter, r *http.Request) (*Session, error) {
|
func NewSession(w http.ResponseWriter, r *http.Request) (*Session, error) {
|
||||||
sessionID, err := getSessionCookie(w, r)
|
sessionID, err := getSessionCookie(w, r)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, fmt.Errorf("getSessionCookie failed: %w", err)
|
||||||
}
|
}
|
||||||
session := &Session{ID: sessionID}
|
session := &Session{ID: sessionID}
|
||||||
err = loadSession(session)
|
err = loadSession(session)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, fmt.Errorf("loadSession failed: %w" , err)
|
||||||
}
|
}
|
||||||
return session, nil
|
return session, nil
|
||||||
}
|
}
|
||||||
|
@ -54,9 +55,13 @@ func (sess *Session) Flush() error {
|
||||||
return saveSession(sess)
|
return saveSession(sess)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var fileMutex sync.RWMutex
|
||||||
|
|
||||||
func saveSession(sess *Session) error {
|
func saveSession(sess *Session) error {
|
||||||
filename := generateFilename(sess.ID)
|
filename := generateFilename(sess.ID)
|
||||||
err := os.Mkdir("session", 0755)
|
err := os.Mkdir("session", 0755)
|
||||||
|
fileMutex.Lock()
|
||||||
|
defer fileMutex.Unlock()
|
||||||
f, err := os.Create(filename)
|
f, err := os.Create(filename)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
@ -69,17 +74,22 @@ func saveSession(sess *Session) error {
|
||||||
func loadSession(sess *Session) error {
|
func loadSession(sess *Session) error {
|
||||||
filename := generateFilename(sess.ID)
|
filename := generateFilename(sess.ID)
|
||||||
err := os.Mkdir("session", 0755)
|
err := os.Mkdir("session", 0755)
|
||||||
|
fileMutex.RLock()
|
||||||
|
defer fileMutex.RUnlock()
|
||||||
f, err := os.Open(filename)
|
f, err := os.Open(filename)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if os.IsNotExist(err) {
|
if os.IsNotExist(err) {
|
||||||
// add defaults to session?
|
// add defaults to session?
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
return err
|
return fmt.Errorf("while opening file %s: %w", filename, err)
|
||||||
}
|
}
|
||||||
defer f.Close()
|
defer f.Close()
|
||||||
err = json.NewDecoder(f).Decode(sess)
|
err = json.NewDecoder(f).Decode(sess)
|
||||||
return err
|
if err != nil {
|
||||||
|
return fmt.Errorf("while decoding json from file %s: %w", filename, err)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func generateFilename(id string) string {
|
func generateFilename(id string) string {
|
||||||
|
|
Loading…
Reference in New Issue
Block a user