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 {Network} from "vis-network/peer";
|
||||
// import {DataSet} from "vis-data/peer";
|
||||
// import {Network} from "vis-network/peer";
|
||||
import $ from 'jquery';
|
||||
|
||||
function wikiGraph(selector, options) {
|
||||
$(selector).each(function (i, el) {
|
||||
let $el = $(el)
|
||||
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)
|
||||
Promise.all([
|
||||
import("vis-data/peer"),
|
||||
import("vis-network/peer"),
|
||||
]).then(imports => {
|
||||
const DataSet = imports[0].DataSet
|
||||
const Network = imports[1].Network
|
||||
|
||||
var data = {
|
||||
nodes: nodes,
|
||||
edges: edges
|
||||
};
|
||||
$(selector).each(function (i, el) {
|
||||
let $el = $(el)
|
||||
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 = {
|
||||
edges: {
|
||||
arrows: 'to',
|
||||
color: {
|
||||
highlight: 'green'
|
||||
},
|
||||
chosen: {
|
||||
edge: function (values, id, selected, hovering) {
|
||||
if (this.from === 1) {
|
||||
values.color = 'blue';
|
||||
var data = {
|
||||
nodes: nodes,
|
||||
edges: edges
|
||||
};
|
||||
|
||||
var options = {
|
||||
edges: {
|
||||
arrows: 'to',
|
||||
color: {
|
||||
highlight: 'green'
|
||||
},
|
||||
chosen: {
|
||||
edge: function (values, id, selected, hovering) {
|
||||
if (this.from === 1) {
|
||||
values.color = 'blue';
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
},
|
||||
|
||||
nodes: {
|
||||
shape: 'dot',
|
||||
size: 15,
|
||||
font: {
|
||||
background: 'white'
|
||||
nodes: {
|
||||
shape: 'dot',
|
||||
size: 15,
|
||||
font: {
|
||||
background: 'white'
|
||||
}
|
||||
},
|
||||
layout: {
|
||||
improvedLayout: true
|
||||
}
|
||||
},
|
||||
layout: {
|
||||
improvedLayout: true
|
||||
}
|
||||
};
|
||||
};
|
||||
|
||||
var network = new Network(el, data, options);
|
||||
network.on('doubleClick', function (props) {
|
||||
if (props.nodes.length) {
|
||||
let nodeId = props.nodes[0]
|
||||
let node = nodes.get(nodeId)
|
||||
// TODO: Reload directly in this same page
|
||||
window.location.href = '/edit/' + node.label
|
||||
}
|
||||
var network = new Network(el, data, options);
|
||||
network.on('doubleClick', function (props) {
|
||||
if (props.nodes.length) {
|
||||
let nodeId = props.nodes[0]
|
||||
let node = nodes.get(nodeId)
|
||||
// TODO: Reload directly in this same page
|
||||
window.location.href = '/edit/' + node.label
|
||||
}
|
||||
})
|
||||
})
|
||||
})
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
import MarkdownIt from "markdown-it";
|
||||
import MarkdownItWikilinks from "./wikilinks";
|
||||
import MarkdownItMark from "markdown-it-mark";
|
||||
import MarkdownItKatex from "markdown-it-katex";
|
||||
// import MarkdownItKatex from "markdown-it-katex";
|
||||
|
||||
const MD = new MarkdownIt({
|
||||
linkify: true,
|
||||
|
@ -20,6 +20,7 @@ MD.use(MarkdownItWikilinks({
|
|||
htmlAttributes: {
|
||||
class: 'wiki-link'
|
||||
},
|
||||
})).use(MarkdownItMark).use(MarkdownItKatex)
|
||||
})).use(MarkdownItMark)
|
||||
// .use(MarkdownItKatex)
|
||||
|
||||
export default MD;
|
||||
|
|
122
file.go
122
file.go
|
@ -40,7 +40,6 @@ import (
|
|||
const (
|
||||
DocumentsFile = "_documents.json"
|
||||
LinksFile = "_links.json"
|
||||
BlocksDirectory = "_blocks"
|
||||
)
|
||||
|
||||
var BlockNotFound = errors.New("block not found")
|
||||
|
@ -52,12 +51,6 @@ type saveMessage struct {
|
|||
author string
|
||||
}
|
||||
|
||||
type Block struct {
|
||||
Text string
|
||||
Children []string
|
||||
Parent string
|
||||
}
|
||||
|
||||
// ListItemV2 is way to convert from old structure to new structure
|
||||
type ListItemV2 struct {
|
||||
ID ID
|
||||
|
@ -95,6 +88,8 @@ type FilePages struct {
|
|||
dirname string
|
||||
saveC chan saveMessage
|
||||
index bleve.Index
|
||||
|
||||
blockRepo BlockRepository
|
||||
}
|
||||
|
||||
type BlockResponse struct {
|
||||
|
@ -105,13 +100,18 @@ type BlockResponse struct {
|
|||
Parents []string
|
||||
}
|
||||
|
||||
func NewFilePages(dirname string, index bleve.Index) PagesRepository {
|
||||
err := os.MkdirAll(filepath.Join(dirname, "_blocks"), 0777)
|
||||
func NewFilePages(dirname string, index bleve.Index) *FilePages {
|
||||
blockRepo, err := NewBlockRepo(dirname)
|
||||
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() {
|
||||
for msg := range fp.saveC {
|
||||
|
@ -174,7 +174,7 @@ func (fp *FilePages) Get(name string) Page {
|
|||
}
|
||||
|
||||
for _, name := range names {
|
||||
blocks, err := loadBlocks(fp.dirname, name)
|
||||
blocks, err := fp.blockRepo.GetBlocks(name)
|
||||
if err != nil && errors.Is(err, BlockNotFound) {
|
||||
continue
|
||||
}
|
||||
|
@ -289,7 +289,7 @@ func (fp *FilePages) save(msg saveMessage) error {
|
|||
}
|
||||
|
||||
sw.Start("create blocks")
|
||||
err := saveBlocksFromPage(fp.dirname, page)
|
||||
err := fp.saveBlocksFromPage(fp.dirname, page)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
}
|
||||
|
@ -333,7 +333,7 @@ func (fp *FilePages) save(msg saveMessage) error {
|
|||
sw.Stop()
|
||||
|
||||
sw.Start("index")
|
||||
searchObjects, err := createSearchObjects(page.Name)
|
||||
searchObjects, err := createSearchObjects(fp, page.Name)
|
||||
if err != nil {
|
||||
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
|
||||
}
|
||||
|
||||
func saveBlocksFromPage(dirname string, page Page) error {
|
||||
func (fp* FilePages) saveBlocksFromPage(dirname string, page Page) error {
|
||||
log.Printf("Processing: %q\n", page.Name)
|
||||
var listItems []ListItem
|
||||
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)
|
||||
|
||||
root := "root"
|
||||
parentBlock, err := loadBlock(dirname, page.Name)
|
||||
parentBlock, err := fp.blockRepo.GetBlock(page.Name)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
} else {
|
||||
|
@ -446,7 +446,7 @@ func saveBlocksFromPage(dirname string, page Page) error {
|
|||
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)
|
||||
f, err := os.Open(filepath.Join(dirname, BlocksDirectory, rootParentID))
|
||||
if err == nil {
|
||||
|
@ -467,101 +467,15 @@ func saveBlocksFromPage(dirname string, page Page) error {
|
|||
}
|
||||
|
||||
for id, block := range blocks {
|
||||
log.Printf("Writing to %q\n", id)
|
||||
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)
|
||||
err := fp.blockRepo.SaveBlock(id, block)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
}
|
||||
f.Close()
|
||||
}
|
||||
|
||||
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 {
|
||||
type Document struct {
|
||||
Title string `json:"title"`
|
||||
|
|
14
main.go
14
main.go
|
@ -52,8 +52,14 @@ var (
|
|||
authKey = authorizedKey("authorizedKey")
|
||||
)
|
||||
|
||||
type BlockRepository interface {
|
||||
GetBlock(id string) (Block, error)
|
||||
GetBlocks(rootBlockID string) (BlockResponse, error)
|
||||
SaveBlock(id string, block Block) error
|
||||
}
|
||||
|
||||
var (
|
||||
mp PagesRepository
|
||||
mp PagesRepository
|
||||
|
||||
port = flag.Int("port", 8080, "listen port")
|
||||
baseurl = flag.String("baseurl", "", "baseurl")
|
||||
|
@ -745,6 +751,7 @@ func (h *indexHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|||
|
||||
sess, err := NewSession(w, r)
|
||||
if err != nil {
|
||||
log.Println("NewSession", err)
|
||||
http.Error(w, err.Error(), 500)
|
||||
return
|
||||
}
|
||||
|
@ -805,6 +812,7 @@ func (h *indexHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|||
} else if format == "metakv" {
|
||||
so, err := createStructuredFormat(mpPage)
|
||||
if err != nil {
|
||||
log.Println("createStructuredFormat", err)
|
||||
http.Error(w, err.Error(), 500)
|
||||
return
|
||||
}
|
||||
|
@ -814,6 +822,7 @@ func (h *indexHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|||
enc.SetIndent("", " ")
|
||||
err = enc.Encode(so)
|
||||
if err != nil {
|
||||
log.Println("Encode", err)
|
||||
http.Error(w, err.Error(), 500)
|
||||
}
|
||||
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) {
|
||||
err := r.ParseForm()
|
||||
if err != nil {
|
||||
|
@ -1284,7 +1294,7 @@ func createSearchIndex(dataDir, indexName string) (bleve.Index, error) {
|
|||
}
|
||||
|
||||
for _, page := range pages {
|
||||
searchObjects, err := createSearchObjects(page.Name)
|
||||
searchObjects, err := createSearchObjects(fp, page.Name)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
continue
|
||||
|
|
|
@ -115,7 +115,7 @@ func (s *searchHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|||
}
|
||||
|
||||
for _, page := range pages {
|
||||
err = saveBlocksFromPage("data", page)
|
||||
err = mp.saveBlocksFromPage("data", page)
|
||||
if err != nil {
|
||||
log.Printf("error while processing blocks from page %s: %v", page.Name, err)
|
||||
continue
|
||||
|
@ -168,7 +168,7 @@ func (s *searchHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|||
}
|
||||
|
||||
for _, page := range pages {
|
||||
searchObjects, err := createSearchObjects(page.Name)
|
||||
searchObjects, err := createSearchObjects(mp, page.Name)
|
||||
if err != nil {
|
||||
log.Printf("error while creating search object %s: %v", page.Title, err)
|
||||
continue
|
||||
|
@ -246,8 +246,8 @@ func (p pageBlock) Type() string {
|
|||
return "block"
|
||||
}
|
||||
|
||||
func createSearchObjects(rootBlockID string) ([]pageBlock, error) {
|
||||
blocks, err := loadBlocks("data", rootBlockID)
|
||||
func createSearchObjects(fp *FilePages, rootBlockID string) ([]pageBlock, error) {
|
||||
blocks, err := fp.blockRepo.GetBlocks(rootBlockID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
|
18
session.go
18
session.go
|
@ -24,6 +24,7 @@ import (
|
|||
"fmt"
|
||||
"net/http"
|
||||
"os"
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
|
||||
|
@ -40,12 +41,12 @@ type Session struct {
|
|||
func NewSession(w http.ResponseWriter, r *http.Request) (*Session, error) {
|
||||
sessionID, err := getSessionCookie(w, r)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return nil, fmt.Errorf("getSessionCookie failed: %w", err)
|
||||
}
|
||||
session := &Session{ID: sessionID}
|
||||
err = loadSession(session)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return nil, fmt.Errorf("loadSession failed: %w" , err)
|
||||
}
|
||||
return session, nil
|
||||
}
|
||||
|
@ -54,9 +55,13 @@ func (sess *Session) Flush() error {
|
|||
return saveSession(sess)
|
||||
}
|
||||
|
||||
var fileMutex sync.RWMutex
|
||||
|
||||
func saveSession(sess *Session) error {
|
||||
filename := generateFilename(sess.ID)
|
||||
err := os.Mkdir("session", 0755)
|
||||
fileMutex.Lock()
|
||||
defer fileMutex.Unlock()
|
||||
f, err := os.Create(filename)
|
||||
if err != nil {
|
||||
return err
|
||||
|
@ -69,17 +74,22 @@ func saveSession(sess *Session) error {
|
|||
func loadSession(sess *Session) error {
|
||||
filename := generateFilename(sess.ID)
|
||||
err := os.Mkdir("session", 0755)
|
||||
fileMutex.RLock()
|
||||
defer fileMutex.RUnlock()
|
||||
f, err := os.Open(filename)
|
||||
if err != nil {
|
||||
if os.IsNotExist(err) {
|
||||
// add defaults to session?
|
||||
return nil
|
||||
}
|
||||
return err
|
||||
return fmt.Errorf("while opening file %s: %w", filename, err)
|
||||
}
|
||||
defer f.Close()
|
||||
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 {
|
||||
|
|
Loading…
Reference in New Issue
Block a user