forked from pgaskin/BookBrowser
/
bookbrowser.go
90 lines (74 loc) · 2.17 KB
/
bookbrowser.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
package main
import (
"flag"
"log"
"os"
"os/signal"
"path/filepath"
"syscall"
)
// Series represents a book series
type Series struct {
Name string `json:"name,omitempty"`
ID string `json:"id,omitempty"`
Index float64 `json:"index,omitempty"`
}
// Book represents a book
type Book struct {
ID string `json:"id"`
Title string `json:"title"`
Author string `json:"author,omitempty"`
AuthorID string `json:"authorid"`
Publisher string `json:"publisher,omitempty"`
Description string `json:"description,omitempty"`
Series Series `json:"series,omitempty"`
Filepath string `json:"filepath"`
HasCover bool `json:"hascover"`
}
var bookdir *string
var tempdir *string
var addr *string
var curversion = "undefined"
func main() {
wd, err := os.Getwd()
if err != nil {
log.Fatalf("Fatal error: %s\n", err)
}
bookdir = flag.String("bookdir", wd, "The directory to get books from. This directory must exist.")
tempdir = flag.String("tempdir", filepath.Join(wd, "_temp"), "The directory to use for storing temporary files such as book cover thumbnails. This directory is create on start and deleted on exit.")
addr = flag.String("addr", ":8090", "The address to bind to.")
flag.Parse()
log.Printf("BookBrowser %s\n", curversion)
if _, err := os.Stat(*bookdir); err != nil {
if os.IsNotExist(err) {
log.Fatalf("Fatal error: book directory %s does not exist\n", *bookdir)
}
}
*bookdir, err = filepath.Abs(*bookdir)
if err != nil {
log.Fatalf("Fatal error: Could not resolve book directory %s: %s\n", *bookdir, err)
}
if _, err := os.Stat(*tempdir); os.IsNotExist(err) {
os.Mkdir(*tempdir, os.ModePerm)
}
*tempdir, err = filepath.Abs(*tempdir)
if err != nil {
log.Fatalf("Fatal error: Could not resolve temp directory %s: %s\n", *tempdir, err)
}
sigs := make(chan os.Signal, 1)
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)
go func() {
<-sigs
log.Println("Cleaning up covers")
os.RemoveAll(*tempdir)
os.Exit(0)
}()
books, err := indexBooks()
if err != nil {
log.Fatalf("Fatal error indexing books: %s\n", err)
}
if len(books) == 0 {
log.Fatalln("Fatal error: no books found")
}
runServer(books, *addr)
}