10 Commits

11 changed files with 80 additions and 22 deletions

View File

@@ -90,3 +90,4 @@ changelog:
- '^docs:'
- '^test:'
- '^[Bb]ump'
- '^[Cc]lean'

View File

@@ -32,6 +32,11 @@ A self-hosted bookmark database with full-text page content search.
mountpoint.
* Run `docker-compose up -d`
To upgrade:
* `docker-compose pull`
* `docker-compose up -d`
## Packages (deb/rpm)
* Download the .deb or .rpm from the releases

View File

@@ -5,6 +5,7 @@ import (
"fmt"
"io"
"log"
"os"
"strings"
"sync"
"time"
@@ -100,7 +101,6 @@ func (m *BookmarkManager) SaveBookmark(bm *entity.Bookmark) error {
func (m *BookmarkManager) LoadBookmarkByID(id uint64) entity.Bookmark {
// log.Printf("loading %v", ids)
ret := entity.Bookmark{}
log.Printf("loading id %d", id)
err := m.db.store.Get(id, &ret)
if err != nil {
panic(err)
@@ -207,7 +207,6 @@ func (m *BookmarkManager) ScrapeAndIndex(bm *entity.Bookmark) error {
func (m *BookmarkManager) UpdateIndexForBookmark(bm *entity.Bookmark) {
words := content.Words(bm)
words = append(words, bm.Tags...)
log.Printf("index for %d %s (%d words)", bm.ID, bm.URL, len(words))
m.db.UpdateIndexForWordsByID(words, bm.ID)
}
@@ -281,3 +280,18 @@ func (m *BookmarkManager) UpdateContent() {
time.Sleep(time.Second * 5)
}
}
func (m *BookmarkManager) Stats() (entity.DBStats, error) {
stats := entity.DBStats{}
err := m.db.store.Get("stats", &stats)
if err != nil && err != bolthold.ErrNotFound {
return stats, fmt.Errorf("could not load stats: %s", err)
}
// get the DB size
fi, err := os.Stat(m.db.file)
if err != nil {
return stats, fmt.Errorf("could not load db file size: %s", err)
}
stats.FileSize = int(fi.Size())
return stats, nil
}

View File

@@ -10,6 +10,7 @@ import (
type DB struct {
store *bolthold.Store
file string
}
func (db *DB) Open(path string) error {
@@ -21,6 +22,7 @@ func (db *DB) Open(path string) error {
return fmt.Errorf("cannot open '%s' - %s", path, err)
}
db.store = store
db.file = path
return nil
}

View File

@@ -8,6 +8,7 @@ import (
type DBStats struct {
History map[time.Time]BookmarkInfo
FileSize int
Searches int
}
@@ -32,3 +33,16 @@ func (stats DBStats) String() string {
}
return out
}
func (stats DBStats) MostRecentBookmarkInfo() BookmarkInfo {
mostRecent := time.Time{}
for k := range stats.History {
if k.After(mostRecent) {
mostRecent = k
}
}
if !mostRecent.IsZero() {
return stats.History[mostRecent]
}
return BookmarkInfo{}
}

View File

@@ -9,6 +9,6 @@ func MemInfo() string {
stats := runtime.MemStats{}
runtime.ReadMemStats(&stats)
return fmt.Sprintf("%.3fMb", float64(stats.Alloc)/1024.0/1024.0)
return fmt.Sprintf("%.1fMb", float64(stats.Alloc)/1024.0/1024.0)
}

View File

@@ -10,7 +10,7 @@ import (
"golang.org/x/mod/semver"
)
const Tag = "v0.0.30"
const Tag = "v0.0.33"
type Info struct {
Local struct {

View File

@@ -35,7 +35,7 @@
<div class="top-bar-right">
<ul class="menu">
<li>
<a href="/releaseinfo">{{ version.Local.Tag }}
<a href="/info">{{ version.Local.Tag }}
{{ if version.UpgradeAvailable }}
{{ end }}
@@ -63,8 +63,8 @@
{{ template "config.html" . }}
{{ else if eq .page "edit" }}
{{ template "edit.html" . }}
{{ else if eq .page "releaseinfo" }}
{{ template "release_info.html" . }}
{{ else if eq .page "info" }}
{{ template "info.html" . }}
{{ end }}
{{/* template "foundation_sample.html" . */}}
</div>

View File

@@ -30,7 +30,10 @@
</tr>
</table>
<p>
<button type="button" hx-confirm="Delete this bookmark permanently?" hx-delete="/edit/{{.bookmark.ID}}" class="alert button">delete</button>
<button type="button" class="button" hx-post="/edit/{{.bookmark.ID}}">save</button>
<button type="button" hx-confirm="Delete this bookmark permanently?" hx-delete="/edit/{{.bookmark.ID}}" class="alert button">Delete</button>
<button type="button" class="button" hx-indicator="#saving" hx-post="/edit/{{.bookmark.ID}}"> {{ if .saved }} Saved {{ else }} Save {{ end }}</button>
<span id="saving" class="htmx-indicator">
<img style="height:1em;" src="/assets/image/beating.gif" /> Saving...
</span>
</p>
</form>

View File

@@ -1,8 +1,15 @@
<div class="grid-x grid-padding-x">
<div class="large-12 cell">
<h5>Memory Usage</h5>
<p>{{ meminfo }}</p>
<div class="large-6 medium-12 cell">
<h5>System information</h5>
<table>
<tr><th>Memory in use</th><td>{{ meminfo }}</td></tr>
<tr><th>Database disk size</th><td>{{ niceSizeMB .stats.FileSize }}Mb</td></tr>
<tr><th>Bookmarks</th><td>{{ .stats.MostRecentBookmarkInfo.Bookmarks }}</td></tr>
<tr><th>Words in index</th><td>{{ .stats.MostRecentBookmarkInfo.IndexedWords }}</td></tr>
</table>
</div>
<div class="large-6 medium-12 cell">
<h5>Release info</h5>
{{ if not version.Remote.Valid }}

View File

@@ -79,12 +79,13 @@ func Create(bmm *db.BookmarkManager, cmm *db.ConfigManager) *Server {
// templ := template.Must(template.New("").Funcs(template.FuncMap{"dict": dictHelper}).ParseFS(templateFiles, "templates/*.html"))
templ := template.Must(template.New("").Funcs(
template.FuncMap{
"nicetime": niceTime,
"niceURL": niceURL,
"join": strings.Join,
"version": func() *version.Info { return &version.VersionInfo },
"meminfo": meta.MemInfo,
"markdown": func(s string) template.HTML { return template.HTML(string(markdown.ToHTML([]byte(s), nil, nil))) },
"nicetime": niceTime,
"niceURL": niceURL,
"niceSizeMB": func(s int) string { return fmt.Sprintf("%.1f", float32(s)/1024/1024) },
"join": strings.Join,
"version": func() *version.Info { return &version.VersionInfo },
"meminfo": meta.MemInfo,
"markdown": func(s string) template.HTML { return template.HTML(string(markdown.ToHTML([]byte(s), nil, nil))) },
}).ParseFS(templateFiles, "templates/*.html"))
config, err := cmm.LoadConfig()
@@ -187,6 +188,13 @@ func Create(bmm *db.BookmarkManager, cmm *db.ConfigManager) *Server {
r.POST("/search", func(c *gin.Context) {
query := c.PostForm("query")
// no query, return an empty response
if len(query) == 0 {
c.Status(http.StatusNoContent)
c.Writer.Write([]byte{})
return
}
sr, err := bmm.Search(db.SearchOptions{Query: query})
data := gin.H{
"results": sr,
@@ -371,7 +379,7 @@ func Create(bmm *db.BookmarkManager, cmm *db.ConfigManager) *Server {
bmm.SaveBookmark(&bookmark)
bmm.UpdateIndexForBookmark(&bookmark) // because title may have changed
meta := gin.H{"page": "edit", "bookmark": bookmark, "tw": gin.H{"tags": bookmark.Tags, "tags_hidden": strings.Join(bookmark.Tags, "|")}}
meta := gin.H{"page": "edit", "bookmark": bookmark, "saved": true, "tw": gin.H{"tags": bookmark.Tags, "tags_hidden": strings.Join(bookmark.Tags, "|")}}
c.HTML(http.StatusOK,
"edit_form.html", meta,
@@ -396,8 +404,12 @@ func Create(bmm *db.BookmarkManager, cmm *db.ConfigManager) *Server {
)
})
r.GET("/releaseinfo", func(c *gin.Context) {
meta := gin.H{"page": "releaseinfo", "config": config}
r.GET("/info", func(c *gin.Context) {
dbStats, err := bmm.Stats()
if err != nil {
panic("could not load stats for info page")
}
meta := gin.H{"page": "info", "stats": dbStats, "config": config}
c.HTML(http.StatusOK,
"_layout.html", meta,
)