12 Commits

12 changed files with 140 additions and 53 deletions

View File

@@ -30,12 +30,12 @@ func main() {
go func() {
for {
version.UpdateVersionInfo()
version.VersionInfo.UpdateVersionInfo()
time.Sleep(time.Hour * 6)
}
}()
log.Printf("linkwallet version %s starting", version.Is())
log.Printf("linkwallet version %s starting", version.VersionInfo.Local.Tag)
server := web.Create(bmm, cmm)
go bmm.RunQueue()

View File

@@ -1,9 +1,11 @@
package db
import (
"errors"
"fmt"
"io"
"log"
"strings"
"sync"
"time"
@@ -32,6 +34,12 @@ func NewBookmarkManager(db *DB) *BookmarkManager {
// if this bookmark already exists (based on URL match).
// The entity.Bookmark ID field will be updated.
func (m *BookmarkManager) AddBookmark(bm *entity.Bookmark) error {
if strings.Index(bm.URL, "https://") != 0 &&
strings.Index(bm.URL, "http://") != 0 {
return errors.New("URL must begin with http:// or https://")
}
existing := entity.Bookmark{}
err := m.db.store.FindOne(&existing, bolthold.Where("URL").Eq(bm.URL))
if err != bolthold.ErrNotFound {

1
go.mod
View File

@@ -37,6 +37,7 @@ require (
github.com/gobwas/glob v0.2.3 // indirect
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/gomarkdown/markdown v0.0.0-20220627144906-e9a81102ebeb
github.com/google/go-github/v44 v44.1.0
github.com/hako/durafmt v0.0.0-20210608085754-5c1018a4e16b
github.com/kennygrant/sanitize v1.2.4 // indirect

2
go.sum
View File

@@ -38,6 +38,8 @@ github.com/golang/protobuf v1.3.3/go.mod h1:vzj43D7+SQXF/4pzW/hwtAqwc6iTitCiVSaW
github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk=
github.com/golang/protobuf v1.5.2 h1:ROPKBNFfQgOUMifHyP+KYbvpjbdoFNs+aK7DXlji0Tw=
github.com/golang/protobuf v1.5.2/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiuN0vRsmY=
github.com/gomarkdown/markdown v0.0.0-20220627144906-e9a81102ebeb h1:5b/eFaSaKPFG9ygDBaPKkydKU5nFJYk08g9jPIVogMg=
github.com/gomarkdown/markdown v0.0.0-20220627144906-e9a81102ebeb/go.mod h1:JDGcbDT52eL4fju3sZ4TeHGsQwhG9nbDV21aMyhwPoA=
github.com/google/go-cmp v0.5.2/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.8 h1:e6P7q2lk1O+qJJb4BtCQXlK8vWEO8V1ZeuEdJNOqZyg=

14
meta/meta.go Normal file
View File

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

View File

@@ -2,15 +2,17 @@ package version
import (
"context"
"fmt"
"strings"
"sync"
"github.com/google/go-github/v44/github"
"golang.org/x/mod/semver"
)
const Tag = "v0.0.24"
const Tag = "v0.0.29"
var versionInfo struct {
type Info struct {
Local struct {
Tag string
}
@@ -18,51 +20,58 @@ var versionInfo struct {
Valid bool
Tag string
}
m sync.Mutex
UpgradeReleaseNotes string
m sync.Mutex
}
var VersionInfo Info
func init() {
versionInfo.Remote.Valid = false
versionInfo.Local.Tag = Tag
VersionInfo.Remote.Valid = false
VersionInfo.Local.Tag = Tag
}
func Is() string {
return versionInfo.Local.Tag
}
func UpgradeAvailable() (bool, string) {
versionInfo.m.Lock()
defer versionInfo.m.Unlock()
if !versionInfo.Remote.Valid {
return false, ""
func (vi *Info) UpgradeAvailable() bool {
vi.m.Lock()
defer vi.m.Unlock()
if !vi.Remote.Valid {
return false
}
if semver.Compare(versionInfo.Local.Tag, versionInfo.Remote.Tag) < 0 {
return true, versionInfo.Remote.Tag
if semver.Compare(vi.Local.Tag, vi.Remote.Tag) < 0 {
return true
}
return false, ""
return false
}
func UpgradeAvailableString() string {
upgrade, ver := UpgradeAvailable()
if upgrade {
return ver
}
return ""
}
func UpdateVersionInfo() {
func (vi *Info) UpdateVersionInfo() {
client := github.NewClient(nil)
rels, _, err := client.Repositories.ListReleases(context.Background(), "tardisx", "linkwallet", nil)
if err != nil {
panic(err)
return
}
if len(rels) == 0 {
return
}
versionInfo.m.Lock()
versionInfo.Remote.Tag = *rels[0].TagName
versionInfo.Remote.Valid = true
versionInfo.m.Unlock()
vi.m.Lock()
vi.Remote.Tag = *rels[0].TagName
vi.Remote.Valid = true
vi.UpgradeReleaseNotes = ""
for _, r := range rels {
if semver.Compare(VersionInfo.Local.Tag, *r.TagName) < 0 {
vi.UpgradeReleaseNotes += fmt.Sprintf("*Version %s*\n\n", *r.TagName)
bodyLines := strings.Split(*r.Body, "\n")
for _, l := range bodyLines {
if strings.Index(l, "#") == 0 && strings.Contains(l, "Changelog") {
// do nothing, ignore the changelog heading
} else {
vi.UpgradeReleaseNotes += l + "\n"
}
}
}
}
vi.m.Unlock()
}

View File

@@ -34,13 +34,12 @@
</div>
<div class="top-bar-right">
<ul class="menu">
{{ if newVersion }}
<li>
<div><a href="https://github.com/tardisx/linkwallet/releases/tag/{{ newVersion }}">{{ newVersion }} available</a></div>
</li>
{{ end }}
<li class="menu-text">
{{ version }}
<a href="/releaseinfo">{{ version.Local.Tag }}
{{ if version.UpgradeAvailable }}
{{ end }}
</a>
</li>
<li>
<a href="https://github.com/tardisx/linkwallet">
@@ -64,6 +63,8 @@
{{ template "config.html" . }}
{{ else if eq .page "edit" }}
{{ template "edit.html" . }}
{{ else if eq .page "releaseinfo" }}
{{ template "release_info.html" . }}
{{ end }}
{{/* template "foundation_sample.html" . */}}
</div>

View File

@@ -1,24 +1,24 @@
<div class="large-8 medium-8 cell" id="add-url-form" >
<div>
<h5 style="display:inline-block;">Add a new URL</h5>
<p style="display:inline-block;">[<a hx-get="/bulk_add" hx-target="#add-url-form" href="#">bulk add</a>]</h5>
<div style="display:inline-block;">[<a hx-get="/bulk_add" hx-swap="outerHTML" hx-target="#add-url-form" href="#">bulk add</a>]</div></h5>
</div>
<form onsubmit="return false">
<div class="grid-x grid-padding-x">
<div class="large-6 cell">
<div class="medium-6 cell">
<label>URL</label>
<input type="text" name="url" value="{{ .url }}"
hx-trigger=""
/>
</div>
<div class="large-6 cell">
<div class="medium-6 cell">
{{ template "tags_widget.html" . }}
</div>
</div>
<div class="grid-x grid-padding-x">
<div class="medium-6 cell">
<a href="#" class="button" hx-post="/add"
<a href="#" class="button" hx-post="/add"
hx-target="#add-url-form">add</a>
</div>
</div>

View File

@@ -1,14 +1,13 @@
<div class="large-8 medium-8 cell" id="add-url-form" >
<div>
<h5 style="display:inline-block;">Add bulk URLs</h5>
<p style="display:inline-block;">[<a hx-get="/single_add" hx-target="#add-url-form" href="#">single add</a>]</h5>
</div> <form onsubmit="return false">
<p style="display:inline-block;">[<a hx-get="/single_add" hx-swap="outerHTML" hx-target="#add-url-form" href="#">single add</a>]</h5>
</div>
<form onsubmit="return false">
<div class="grid-x grid-padding-x">
<div class="large-12 cell">
<label>Paste URL's, one per line</label>
<textarea type="text" name="urls" rows="10"
></textarea>
<textarea type="text" name="urls" rows="10"></textarea>
</div>
</div>
<button

View File

@@ -0,0 +1,26 @@
<div class="grid-x grid-padding-x">
<div class="large-12 cell">
<h5>Memory Usage</h5>
<p>{{ meminfo }}</p>
<h5>Release info</h5>
{{ if not version.Remote.Valid }}
<p>GitHub version information not yet fetched.</p>
{{ else }}
{{ if version.UpgradeAvailable }}
<p>
A new version is available:
<a href="https://github.com/tardisx/linkwallet/releases/tag/{{ version.Remote.Tag }}">
{{ version.Remote.Tag }}
</a>
(you have {{ version.Local.Tag }}).
</p>
{{ markdown version.UpgradeReleaseNotes }}
{{ else }}
<p>You are currently running the most recent version.</p>
{{ end }}
{{ end }}
</div>

View File

@@ -1,15 +1,15 @@
<div id="label-widget" >
<div class="grid-x grid-padding-x">
<div class="small-9 medium-10 large-5 cell"
<div class="small-6 cell"
hx-post="/tags"
hx-target="#label-widget"
hx-trigger="change">
hx-trigger="change queue:first">
<label for="tag-entry"
class="">Tags</label>
<input id="tag-entry" type="text" name="tag" placeholder="enter tags" />
</div>
<div class="small-12 large-6 cell" id="tags-list">
<div class="small-6 cell" id="tags-list">
{{ range .tags }}
<a href="#"
class=""

View File

@@ -14,8 +14,10 @@ import (
"github.com/tardisx/linkwallet/db"
"github.com/tardisx/linkwallet/entity"
"github.com/tardisx/linkwallet/meta"
"github.com/tardisx/linkwallet/version"
"github.com/gomarkdown/markdown"
"github.com/hako/durafmt"
"github.com/gin-contrib/gzip"
@@ -75,7 +77,15 @@ 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": version.Is, "newVersion": version.UpgradeAvailableString}).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))) },
}).ParseFS(templateFiles, "templates/*.html"))
config, err := cmm.LoadConfig()
if err != nil {
@@ -205,6 +215,13 @@ func Create(bmm *db.BookmarkManager, cmm *db.ConfigManager) *Server {
"bm": bm,
"error": err,
}
if err != nil {
data["url"] = url
data["tags"] = tags
data["tags_hidden"] = c.PostForm("tags_hidden")
}
c.HTML(http.StatusOK, "add_url_form.html", data)
})
r.POST("/add_bulk", func(c *gin.Context) {
@@ -379,6 +396,13 @@ func Create(bmm *db.BookmarkManager, cmm *db.ConfigManager) *Server {
)
})
r.GET("/releaseinfo", func(c *gin.Context) {
meta := gin.H{"page": "releaseinfo", "config": config}
c.HTML(http.StatusOK,
"_layout.html", meta,
)
})
return server
}
@@ -402,7 +426,10 @@ func cleanupTags(tags []string) []string {
keys := make(map[string]struct{})
for _, k := range tags {
if k != "" && k != "|" {
keys[strings.ToLower(k)] = struct{}{}
for _, subKey := range strings.Split(k, ",") {
subKey := strings.Trim(subKey, " ")
keys[strings.ToLower(subKey)] = struct{}{}
}
}
}
out := []string{}