-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
154 lines (132 loc) · 3.2 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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
package main
import (
"database/sql"
_ "embed"
"fafi2/bookmark"
"fafi2/integration"
"fafi2/sander"
"github.com/joho/godotenv"
_ "github.com/mattn/go-sqlite3"
"html/template"
"log"
"net/http"
"net/url"
"path/filepath"
)
type TemplateData struct {
Bookmarks []bookmark.Bookmark
Query string
}
//go:embed pub/index.html
var EmbedHtmlIndex string
func main() {
bootEnvironment()
sander.UpdateDebugState()
db := bootDatabase()
defer func(db *sql.DB) {
_ = db.Close()
}(db)
// Check if --firefox argument was present
firefoxProfilePath := sander.GetArgFromEnvWithDefault("FAFI_FIREFOX", "")
go func() {
if firefoxProfilePath != "" {
integration.ImportFirefoxProfile(firefoxProfilePath)
}
enableIndexing := sander.GetArgFromEnvWithDefault("FAFI_ENABLE_INDEXING", "1")
if enableIndexing == "1" {
bootIndexer()
} else {
log.Println("Indexing skipped")
}
log.Println("Ready")
}()
bootServer()
}
// Index any bookmarks without text
func bootIndexer() {
for {
queue, err := bookmark.BmDb.SelectQueue()
log.Printf("Indexing queue has %d bookmarks\n", len(queue))
if err != nil {
log.Println("Queue init error:", err)
return
}
for _, bm := range queue {
bookmark.Index(bm)
if err != nil {
log.Println("Indexer error:", err)
continue
}
log.Println("Indexed " + bm.URL)
}
return
}
}
// bootServer Starts Web Server
func bootServer() {
port := sander.GetArgFromEnvWithDefault("FAFI_PORT", "8000")
http.HandleFunc("/", handleIndex)
log.Println("Server starting on http://localhost:" + port)
err := http.ListenAndServe(":"+port, nil)
if err != nil {
log.Fatal("BootServer error: ", err)
}
}
// Database access
func bootDatabase() *sql.DB {
dbPath := sander.GetArgFromEnvWithDefault("FAFI_DB_FILEPATH", "fafi.sqlite3")
fn := filepath.Clean(dbPath)
log.Println("Using " + fn)
db, err := sql.Open("sqlite3", "file:"+fn)
if err != nil {
log.Fatal("DB Opening error:", err)
}
var version string
err = db.QueryRow("SELECT SQLITE_VERSION()").Scan(&version)
if err != nil {
log.Fatal("SQLite error:", err)
}
log.Println("SQLite3 version", version)
bookmark.BmDb = bookmark.NewDatabase(db)
if err := bookmark.BmDb.CreateTable(); err != nil {
log.Fatal("Migration error:", err)
}
bookmark.CreateSampleBookmarks(bookmark.BmDb)
return db
}
// Environment variables
func bootEnvironment() {
err := godotenv.Load()
if err != nil {
log.Fatal("Environment error:", err)
}
}
func handleIndex(w http.ResponseWriter, r *http.Request) {
keywords := getSearchQuery(w, r)
bookmarks, err := bookmark.BmDb.All(keywords)
if err != nil {
log.Fatal("Bookmark error:", err)
}
var tpl = template.Must(template.New("EmbedHtmlIndex").Parse(EmbedHtmlIndex))
data := TemplateData{
Bookmarks: bookmarks,
Query: keywords,
}
err = tpl.Execute(w, data)
if err != nil {
log.Fatal("Error (execute):", err)
}
}
func getSearchQuery(w http.ResponseWriter, r *http.Request) string {
u, err := url.Parse(r.URL.String())
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return ""
}
params := u.Query()
searchQuery := params.Get("q")
if searchQuery != "" {
log.Println("Searched for: ", searchQuery)
}
return searchQuery
}