Add config page, and configuration for the base URL, so the bookmarklet will work on remote machines.
This commit is contained in:
parent
0fc2a597cd
commit
cbe455b566
@ -13,12 +13,13 @@ func main() {
|
||||
dbh := db.DB{}
|
||||
dbh.Open("badger")
|
||||
bmm := db.NewBookmarkManager(&dbh)
|
||||
cmm := db.NewConfigManager(&dbh)
|
||||
|
||||
go func() { version.UpdateVersionInfo() }()
|
||||
|
||||
log.Printf("linkallet verison %s starting", version.Is())
|
||||
|
||||
server := web.Create(bmm)
|
||||
server := web.Create(bmm, cmm)
|
||||
go bmm.RunQueue()
|
||||
go bmm.UpdateContent()
|
||||
server.Start()
|
||||
|
49
db/config.go
Normal file
49
db/config.go
Normal file
@ -0,0 +1,49 @@
|
||||
package db
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
|
||||
"github.com/tardisx/linkwallet/entity"
|
||||
"github.com/timshannon/badgerhold/v4"
|
||||
)
|
||||
|
||||
type ConfigManager struct {
|
||||
db *DB
|
||||
}
|
||||
|
||||
func NewConfigManager(db *DB) *ConfigManager {
|
||||
return &ConfigManager{db: db}
|
||||
}
|
||||
|
||||
func (cmm *ConfigManager) LoadConfig() (entity.Config, error) {
|
||||
config := entity.Config{}
|
||||
err := cmm.db.store.FindOne(&config, &badgerhold.Query{})
|
||||
if err == nil {
|
||||
if config.Version == 1 {
|
||||
return config, nil
|
||||
} else {
|
||||
return entity.Config{}, fmt.Errorf("failed to load config - wrong version %d", config.Version)
|
||||
}
|
||||
} else if err == badgerhold.ErrNotFound {
|
||||
log.Printf("using default config")
|
||||
return cmm.DefaultConfig(), nil
|
||||
} else {
|
||||
return entity.Config{}, fmt.Errorf("failed to load config: %w", err)
|
||||
}
|
||||
}
|
||||
|
||||
func (cmm *ConfigManager) DefaultConfig() entity.Config {
|
||||
return entity.Config{
|
||||
BaseURL: "http://localhost:8080",
|
||||
Version: 1,
|
||||
}
|
||||
}
|
||||
|
||||
func (cmm *ConfigManager) SaveConfig(conf *entity.Config) error {
|
||||
err := cmm.db.store.Upsert("config", conf)
|
||||
if err != nil {
|
||||
return fmt.Errorf("could not save config: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
@ -23,7 +23,7 @@ func (db *DB) UpdateIndexForWordsByID(words []string, id uint64) {
|
||||
delete(wi.Bitmap, id)
|
||||
})
|
||||
|
||||
// addiing
|
||||
// adding
|
||||
var find, store time.Duration
|
||||
for i, word := range words {
|
||||
// log.Printf("indexing %s", word)
|
||||
|
6
entity/config.go
Normal file
6
entity/config.go
Normal file
@ -0,0 +1,6 @@
|
||||
package entity
|
||||
|
||||
type Config struct {
|
||||
BaseURL string
|
||||
Version int
|
||||
}
|
@ -8,7 +8,7 @@ import (
|
||||
"golang.org/x/mod/semver"
|
||||
)
|
||||
|
||||
const Tag = "v0.0.8"
|
||||
const Tag = "v0.0.9"
|
||||
|
||||
var versionInfo struct {
|
||||
Local struct {
|
||||
|
@ -21,11 +21,12 @@
|
||||
<li>
|
||||
<a href="#">Admin</a>
|
||||
<ul class="menu vertical">
|
||||
<li><a href="/config">Configuration</a></li>
|
||||
<li><a href="/manage">Manage links</a></li>
|
||||
<li><a href="/export">Export all URLs</a></li>
|
||||
</ul>
|
||||
</li>
|
||||
<li><a href="javascript:void(window.open('http://localhost:8080/bookmarklet?url=' +encodeURIComponent(window.location), 'windowName', 'width=640,height=480'))">Bookmarklet</a></li>
|
||||
<li><a href="javascript:void(window.open('{{ .config.BaseURL }}/bookmarklet?url=' +encodeURIComponent(window.location), 'windowName', 'width=640,height=480'))">Bookmarklet</a></li>
|
||||
|
||||
</ul>
|
||||
</div>
|
||||
@ -50,6 +51,8 @@
|
||||
{{ template "bookmarklet.html" . }}
|
||||
{{ else if eq .page "manage" }}
|
||||
{{ template "manage.html" . }}
|
||||
{{ else if eq .page "config" }}
|
||||
{{ template "config.html" . }}
|
||||
{{ end }}
|
||||
{{/* template "foundation_sample.html" . */}}
|
||||
</div>
|
||||
|
7
web/templates/config.html
Normal file
7
web/templates/config.html
Normal file
@ -0,0 +1,7 @@
|
||||
<div class="grid-x grid-padding-x">
|
||||
<div class="large-12 cell">
|
||||
|
||||
<h5>Configuration</h5>
|
||||
{{ template "config_form.html" . }}
|
||||
</div>
|
||||
</div>
|
12
web/templates/config_form.html
Normal file
12
web/templates/config_form.html
Normal file
@ -0,0 +1,12 @@
|
||||
|
||||
<form onsubmit="false;" id="config-form" hx-target="#config-form">
|
||||
<table>
|
||||
<tr>
|
||||
<th>Base URL</th>
|
||||
<td>
|
||||
<input type="text" name="baseurl" value="{{ .config.BaseURL }}">
|
||||
</td>
|
||||
</tr>
|
||||
</table>
|
||||
<p><a class="button" hx-post="/config">save</a></p>
|
||||
</form>
|
30
web/web.go
30
web/web.go
@ -42,7 +42,7 @@ type Server struct {
|
||||
}
|
||||
|
||||
// Create creates a new web server instance and sets up routing.
|
||||
func Create(bmm *db.BookmarkManager) *Server {
|
||||
func Create(bmm *db.BookmarkManager, cmm *db.ConfigManager) *Server {
|
||||
|
||||
// setup routes for the static assets (vendor includes)
|
||||
staticFS, err := fs.Sub(staticFiles, "static")
|
||||
@ -53,6 +53,11 @@ func Create(bmm *db.BookmarkManager) *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}).ParseFS(templateFiles, "templates/*.html"))
|
||||
|
||||
config, err := cmm.LoadConfig()
|
||||
if err != nil {
|
||||
log.Fatalf("could not start server - failed to load config: %s", err)
|
||||
}
|
||||
|
||||
r := gin.Default()
|
||||
|
||||
server := &Server{
|
||||
@ -67,7 +72,7 @@ func Create(bmm *db.BookmarkManager) *Server {
|
||||
r.StaticFS("/assets", http.FS(staticFS))
|
||||
|
||||
r.GET("/", func(c *gin.Context) {
|
||||
meta := gin.H{"page": "root"}
|
||||
meta := gin.H{"page": "root", "config": config}
|
||||
c.HTML(http.StatusOK,
|
||||
"_layout.html", meta,
|
||||
)
|
||||
@ -75,12 +80,27 @@ func Create(bmm *db.BookmarkManager) *Server {
|
||||
|
||||
r.GET("/manage", func(c *gin.Context) {
|
||||
allBookmarks, _ := bmm.ListBookmarks()
|
||||
meta := gin.H{"page": "manage", "bookmarks": allBookmarks}
|
||||
meta := gin.H{"page": "manage", "config": config, "bookmarks": allBookmarks}
|
||||
c.HTML(http.StatusOK,
|
||||
"_layout.html", meta,
|
||||
)
|
||||
})
|
||||
|
||||
r.GET("/config", func(c *gin.Context) {
|
||||
meta := gin.H{"page": "config", "config": config}
|
||||
c.HTML(http.StatusOK,
|
||||
"_layout.html", meta,
|
||||
)
|
||||
})
|
||||
|
||||
r.POST("/config", func(c *gin.Context) {
|
||||
config.BaseURL = c.PostForm("baseurl")
|
||||
cmm.SaveConfig(&config)
|
||||
meta := gin.H{"config": config}
|
||||
|
||||
c.HTML(http.StatusOK, "config_form.html", meta)
|
||||
})
|
||||
|
||||
r.POST("/search", func(c *gin.Context) {
|
||||
query := c.PostForm("query")
|
||||
|
||||
@ -152,8 +172,6 @@ func Create(bmm *db.BookmarkManager) *Server {
|
||||
|
||||
r.POST("/tags", func(c *gin.Context) {
|
||||
|
||||
log.Printf("POST: tag '%s' tags_hidden '%s'", c.PostForm("tag"), c.PostForm("tags_hidden"))
|
||||
|
||||
newTag := c.PostForm("tag")
|
||||
oldTags := strings.Split(c.PostForm("tags_hidden"), "|")
|
||||
|
||||
@ -203,7 +221,7 @@ func Create(bmm *db.BookmarkManager) *Server {
|
||||
r.GET("/bookmarklet", func(c *gin.Context) {
|
||||
url := c.Query("url")
|
||||
log.Printf(url)
|
||||
meta := gin.H{"page": "bookmarklet_click", "url": url}
|
||||
meta := gin.H{"page": "bookmarklet_click", "config": config, "url": url}
|
||||
c.HTML(http.StatusOK,
|
||||
"_layout.html", meta,
|
||||
)
|
||||
|
Loading…
x
Reference in New Issue
Block a user