forked from dhutty/golinks
-
Notifications
You must be signed in to change notification settings - Fork 1
/
bookmarks.go
53 lines (44 loc) · 868 Bytes
/
bookmarks.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package main
import (
"fmt"
"log"
"net/http"
"strings"
"github.com/prologic/bitcask"
)
// Bookmark ...
type Bookmark struct {
name string
url string
}
// Name ...
func (b Bookmark) Name() string {
return b.name
}
// URL ...
func (b Bookmark) URL() string {
return b.url
}
// Exec ...
func (b Bookmark) Exec(w http.ResponseWriter, r *http.Request, q string) {
url := b.url
if q != "" {
url = fmt.Sprintf(b.url, q)
}
http.Redirect(w, r, url, http.StatusFound)
}
// LookupBookmark ...
func LookupBookmark(name string) (bookmark Bookmark, ok bool) {
key := fmt.Sprintf("bookmark_%s", strings.ToLower(name))
val, err := db.Get([]byte(key))
if err != nil {
if err == bitcask.ErrKeyNotFound {
return
}
log.Printf("error looking up bookmark for %s: %s", name, err)
}
bookmark.name = name
bookmark.url = string(val)
ok = true
return
}