forked from pgaskin/BookBrowser
/
server.go
322 lines (275 loc) · 8.31 KB
/
server.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
package main
//go:generate go-bindata-assetfs static/...
import (
"bytes"
"encoding/json"
"fmt"
"io"
"log"
"math/rand"
"net/http"
"net/url"
"os"
"path/filepath"
"sort"
"strings"
"time"
)
type nameID struct {
Name string
ID string
}
func sortedBookPropertyList(books []Book, getNameID func(Book) nameID, filterNameID func(nameID) bool, sortNameID func(nameID, nameID) bool) []nameID {
doneItems := map[string]bool{}
items := []nameID{}
for _, b := range books {
nid := getNameID(b)
if doneItems[nid.ID] {
continue
}
doneItems[nid.ID] = true
items = append(items, nameID{
Name: nid.Name,
ID: nid.ID,
})
}
filteredItems := []nameID{}
for _, ni := range items {
if filterNameID(ni) {
filteredItems = append(filteredItems, ni)
}
}
sort.Slice(filteredItems, func(i, j int) bool {
return sortNameID(filteredItems[i], filteredItems[j])
})
return filteredItems
}
func sortedBookList(books []Book, filterBook func(Book) bool, sortBook func(Book, Book) bool) []Book {
filteredItems := []Book{}
for _, book := range books {
if filterBook(book) {
filteredItems = append(filteredItems, book)
}
}
sort.Slice(filteredItems, func(i, j int) bool {
return sortBook(filteredItems[i], filteredItems[j])
})
return filteredItems
}
// DownloadHandler handles file download
func DownloadHandler(w http.ResponseWriter, r *http.Request) {
bid := filepath.Base(r.URL.Path)
bid = strings.Replace(bid, filepath.Ext(bid), "", 1)
if bid == "download" {
w.Header().Set("Content-Type", "text/html")
for _, b := range books {
io.WriteString(w, fmt.Sprintf("<a href=\"/download/%s\">%s</a><br>", b.ID, b.Title))
}
return
}
for _, b := range books {
if b.ID == bid {
rd, err := os.Open(b.Filepath)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
io.WriteString(w, "Error handling request")
log.Printf("Error handling request for %s: %s\n", r.URL.Path, err)
}
w.Header().Set("Content-Disposition", "attachment; filename="+url.PathEscape(b.Title)+".epub")
w.Header().Set("Content-Type", "application/epub+zip")
_, err = io.Copy(w, rd)
rd.Close()
if err != nil {
log.Printf("Error handling request for %s: %s\n", r.URL.Path, err)
}
return
}
}
w.WriteHeader(http.StatusNotFound)
io.WriteString(w, pageHTML("Not Found", "Could not find book with id "+bid))
}
// AuthorsHandler handles the authors page
func AuthorsHandler(w http.ResponseWriter, r *http.Request) {
aid := filepath.Base(r.URL.Path)
if aid == "authors" {
w.Header().Set("Content-Type", "text/html")
var listHTML bytes.Buffer
authors := sortedBookPropertyList(books, func(b Book) nameID {
return nameID{
Name: b.Author,
ID: b.AuthorID,
}
}, func(ni nameID) bool {
return ni.Name != ""
}, func(a nameID, b nameID) bool {
return a.Name < b.Name
})
for _, ni := range authors {
listHTML.WriteString(itemCardHTML(ni.Name, "", "/authors/"+ni.ID))
}
io.WriteString(w, pageHTML("Authors", listHTML.String()))
return
}
w.Header().Set("Content-Type", "text/html")
matched := sortedBookList(books, func(book Book) bool {
return book.AuthorID == aid
}, func(a Book, b Book) bool {
return a.Title < b.Title
})
aname := ""
if len(matched) != 0 {
aname = matched[0].Author
}
html, notfound := bookListPageHTML(matched, aname, "Author not found")
if notfound {
w.WriteHeader(http.StatusNotFound)
}
io.WriteString(w, html)
}
// SeriesHandler handles the series page
func SeriesHandler(w http.ResponseWriter, r *http.Request) {
sid := filepath.Base(r.URL.Path)
if sid == "series" {
w.Header().Set("Content-Type", "text/html")
var listHTML bytes.Buffer
series := sortedBookPropertyList(books, func(b Book) nameID {
return nameID{
Name: b.Series.Name,
ID: b.Series.ID,
}
}, func(ni nameID) bool {
return ni.Name != ""
}, func(a nameID, b nameID) bool {
return a.Name < b.Name
})
for _, ni := range series {
listHTML.WriteString(itemCardHTML(ni.Name, "", "/series/"+ni.ID))
}
if len(series) == 0 {
io.WriteString(w, pageHTML("Series", "No series have been found."))
return
}
io.WriteString(w, pageHTML("Series", listHTML.String()))
return
}
w.Header().Set("Content-Type", "text/html")
matched := sortedBookList(books, func(book Book) bool {
return book.Series.ID == sid
}, func(a Book, b Book) bool {
return a.Series.Index < b.Series.Index
})
sname := ""
if len(matched) != 0 {
sname = matched[0].Series.Name
}
html, notfound := bookListPageHTML(matched, sname, "Series not found")
if notfound {
w.WriteHeader(http.StatusNotFound)
}
io.WriteString(w, html)
}
// BooksHandler handles the books page
func BooksHandler(w http.ResponseWriter, r *http.Request) {
bid := filepath.Base(r.URL.Path)
if bid == "books" {
w.Header().Set("Content-Type", "text/html")
matched := sortedBookList(books, func(book Book) bool {
return true
}, func(a Book, b Book) bool {
return a.ModTime.Unix() > b.ModTime.Unix()
})
html, notfound := bookListPageHTML(matched, "Books", "There are no books in your library.")
if notfound {
w.WriteHeader(http.StatusNotFound)
}
io.WriteString(w, html)
return
}
w.Header().Set("Content-Type", "text/html")
for _, b := range books {
if b.ID == bid {
io.WriteString(w, pageHTML(b.Title, bookHTML(&b, false)))
return
}
}
w.WriteHeader(http.StatusNotFound)
io.WriteString(w, pageHTML("Not Found", "Could not find book with id "+bid))
}
// SearchHandler handles the search page
func SearchHandler(w http.ResponseWriter, r *http.Request) {
q := r.URL.Query().Get("q")
ql := strings.ToLower(q)
if len(q) != 0 {
w.Header().Set("Content-Type", "text/html")
var booksHTML bytes.Buffer
booksHTML.WriteString(`<form role="search" method="GET" action="/search/">
<div class="input-group">
<input type="text" class="form-control" placeholder="Search" name="q" id="q" value="` + strings.Replace(q, `"`, """, -1) + `">
<div class="input-group-btn">
<button class="btn btn-default" type="submit"><i class="glyphicon glyphicon-search"></i></button>
</div>
</div>
</form><br>`)
booksHTML.WriteString(`<div class="books cards">`)
matched := false
for _, b := range books {
matches := false
matches = matches || strings.Contains(strings.ToLower(b.Author), ql)
matches = matches || strings.Contains(strings.ToLower(b.Title), ql)
matches = matches || strings.Contains(strings.ToLower(b.Series.Name), ql)
if matches {
booksHTML.WriteString(bookHTML(&b, true))
matched = true
}
}
booksHTML.WriteString(`</div>`)
if !matched {
booksHTML.WriteString("No books matching your query have been found.")
return
}
io.WriteString(w, pageHTML("Search Results: "+q, booksHTML.String()))
} else {
w.Header().Set("Content-Type", "text/html")
io.WriteString(w, pageHTML("Search", `<form role="search" method="GET" action="/search/">
<div class="input-group">
<input type="text" class="form-control" placeholder="Search" name="q" id="q">
<div class="input-group-btn">
<button class="btn btn-default" type="submit"><i class="glyphicon glyphicon-search"></i></button>
</div>
</div>
</form>`))
}
}
// JSONHandler handles the books.json file
func JSONHandler(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
b, _ := json.Marshal(books)
w.Write(b)
}
var books []Book
func runServer(bks []Book, addr string) {
books = bks
StaticHandler := http.StripPrefix("/static", http.FileServer(assetFS()))
CoversHandler := http.StripPrefix("/covers", http.FileServer(http.Dir(*tempdir)))
http.Handle("/static/", StaticHandler)
http.Handle("/covers/", CoversHandler)
http.HandleFunc("/download/", DownloadHandler)
http.HandleFunc("/authors/", AuthorsHandler)
http.HandleFunc("/series/", SeriesHandler)
http.HandleFunc("/books/", BooksHandler)
http.HandleFunc("/search/", SearchHandler)
http.HandleFunc("/books.json", JSONHandler)
http.HandleFunc("/random/", func(w http.ResponseWriter, r *http.Request) {
rand.Seed(time.Now().Unix())
n := rand.Int() % len(books)
http.Redirect(w, r, "/books/"+books[n].ID, http.StatusTemporaryRedirect)
})
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
http.Redirect(w, r, "/books/", http.StatusTemporaryRedirect)
})
log.Printf("Serving on %s\n", addr)
err := http.ListenAndServe(addr, nil)
if err != nil {
log.Fatalf("Error starting server: %s\n", err)
}
}