-
Notifications
You must be signed in to change notification settings - Fork 38
/
Copy pathmain.go
128 lines (111 loc) · 2.87 KB
/
main.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
package main
import (
"fmt"
"log"
"net/http"
"strings"
"github.com/GeertJohan/go.rice"
"github.com/boltdb/bolt"
"github.com/namsral/flag"
"go.iondynamics.net/templice"
)
var (
db *bolt.DB
cfg Config
templates = templice.New(rice.MustFindBox("templates"))
)
const openSearchTemplate string = `<?xml version="1.0" encoding="UTF-8"?>
<OpenSearchDescription xmlns="http://a9.com/-/spec/opensearch/1.1/">
<ShortName>%s</ShortName>
<Description>Smart bookmarks</Description>
<Tags>search</Tags>
<Contact>admin@localhost</Contact>
<Url type="text/html" method="get" template="http://%s/?q={searchTerms}"/>
</OpenSearchDescription>
`
func render(w http.ResponseWriter, tmpl string, data interface{}) {
err := templates.ExecuteTemplate(w, tmpl+".html", data)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
}
// QueryHandler ...
func QueryHandler() http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
var (
cmd string
args []string
)
q := r.URL.Query().Get("q")
tokens := strings.Split(q, " ")
if len(tokens) > 0 {
cmd, args = tokens[0], tokens[1:]
}
if cmd == "" {
render(w, "index", nil)
} else {
if command := LookupCommand(cmd); command != nil {
err := command.Exec(w, args)
if err != nil {
http.Error(
w,
fmt.Sprintf(
"Error processing command %s: %s",
command.Name(), err,
),
http.StatusInternalServerError,
)
}
} else if bookmark, ok := LookupBookmark(cmd); ok {
q := strings.Join(args, " ")
bookmark.Exec(w, r, q)
} else {
http.Error(
w,
fmt.Sprintf("Invalid Command: %v", cmd),
http.StatusBadRequest,
)
}
}
})
}
// OpenSearchHandler ...
func OpenSearchHandler() http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/xml")
// TODO: Abstract the Config and Handlers better
w.Write([]byte(fmt.Sprintf(openSearchTemplate, cfg.Title, cfg.FQDN)))
})
}
func main() {
var (
config string
dbpath string
title string
bind string
fqdn string
)
flag.StringVar(&config, "config", "", "config file")
flag.StringVar(&dbpath, "dbpth", "search.db", "Database path")
flag.StringVar(&title, "title", "Search", "OpenSearch Title")
flag.StringVar(&bind, "bind", "0.0.0.0:80", "[int]:<port> to bind to")
flag.StringVar(&fqdn, "fqdn", "localhost", "FQDN for public access")
flag.Parse()
// TODO: Abstract the Config and Handlers better
cfg.Title = title
cfg.FQDN = fqdn
var err error
db, err = bolt.Open(dbpath, 0600, nil)
if err != nil {
log.Fatal(err)
}
defer db.Close()
templates.Load()
err = EnsureDefaultBookmarks()
if err != nil {
log.Fatal(err)
}
http.Handle("/", QueryHandler())
http.Handle("/opensearch.xml", OpenSearchHandler())
log.Fatal(http.ListenAndServe(bind, nil))
}