diff --git a/helperfuncs.go b/helperfuncs.go
index 0696847ad34dc8837cd7d93f93c5efa522228c29..15df9ea8c326f9d2334c38837d7a7bb86e50f7ab 100644
--- a/helperfuncs.go
+++ b/helperfuncs.go
@@ -8,6 +8,8 @@ import (
"errors"
"fmt"
"log"
+ "net/url"
+ "sort"
"strings"
"text/template"
@@ -136,7 +138,7 @@ func unauthenticated() string {
}
// authenticated serves the authenticated dashboard to the user
-func (m model) authenticated() string {
+func (m model) authenticated(links map[string]string) string {
dash, err := templates.ReadFile("templates/home_authenticated.html")
if err != nil {
log.Fatalln(err)
@@ -145,8 +147,28 @@ func (m model) authenticated() string {
if err != nil {
log.Println(err)
}
+
+ // Maps have no order so we're making an array containing the keys
+ linksArray := make([]string, 0, len(links))
+ for k := range links {
+ linksArray = append(linksArray, k)
+ }
+ // Then sorting them
+ sort.Sort(sort.StringSlice(linksArray))
+
+ // And ranging through the keys to pull ordered values from the map
+ var table string
+ for _, k := range linksArray {
+ v := links[k]
+ table = table + fmt.Sprintf(`
+ %s |
+ %s |
+ EditDelete |
+
`, k, v, k, url.QueryEscape(string(v)), k)
+ }
+
page := new(strings.Builder)
- err = tmpl.Execute(page, m.genTable())
+ err = tmpl.Execute(page, table)
if err != nil {
log.Println(err)
}
diff --git a/root.go b/root.go
index 9f3f0c72c902eea7d1c7b6331d7d0f0b48748d45..dd38a56d53f090b7215aad26dfcc0818e7b0b1c0 100644
--- a/root.go
+++ b/root.go
@@ -5,11 +5,11 @@
package main
import (
- "fmt"
"io"
"log"
"net/http"
"net/url"
+ "strconv"
"strings"
"github.com/dchest/uniuri"
@@ -54,7 +54,32 @@ func (m model) root(writer http.ResponseWriter, request *http.Request) {
action := query.Get("action")
if len(action) == 0 {
- _, err = io.WriteString(writer, m.authenticated())
+ start := query.Get("start")
+ end := query.Get("end")
+
+ if len(start) > 0 && len(end) > 0 {
+ http.Error(writer, "Submit either start OR end parameter, not both", 400)
+ }
+
+ countQuery := query.Get("count")
+ var count int
+ if len(countQuery) == 0 {
+ count = 20
+ } else {
+ count, err = strconv.Atoi(countQuery)
+ if err != nil {
+ http.Error(writer, err.Error(), 400)
+ return
+ }
+ }
+
+ links, err := m.read(start, end, count)
+ if err != nil {
+ http.Error(writer, err.Error(), 400)
+ return
+ }
+
+ _, err = io.WriteString(writer, m.authenticated(links))
if err != nil {
log.Println(err)
}
@@ -139,33 +164,3 @@ func (m model) root(writer http.ResponseWriter, request *http.Request) {
http.Redirect(writer, request, "/?message="+message, 302)
}
}
-
-func (m model) genTable() string {
- var table string
- err := m.database.View(func(txn *badger.Txn) error {
- opts := badger.DefaultIteratorOptions
- opts.PrefetchSize = 10
- iterator := txn.NewIterator(opts)
- defer iterator.Close()
- for iterator.Rewind(); iterator.Valid(); iterator.Next() {
- item := iterator.Item()
- k := item.Key()
- err := item.Value(func(v []byte) error {
- table = table + fmt.Sprintf(`
- %s |
- %s |
- EditDelete |
-
`, k, v, k, url.QueryEscape(string(v)), k)
- return nil
- })
- if err != nil {
- return err
- }
- }
- return nil
- })
- if err != nil {
- return err.Error()
- }
- return table
-}