This repository has been archived by the owner on Jun 27, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 114
/
Copy pathbookbrowser.go
167 lines (141 loc) · 3.92 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
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
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"os/signal"
"path/filepath"
"runtime"
"strings"
"syscall"
"time"
_ "github.com/geek1011/BookBrowser/formats/epub"
_ "github.com/geek1011/BookBrowser/formats/mobi"
_ "github.com/geek1011/BookBrowser/formats/pdf"
"github.com/geek1011/BookBrowser/server"
"github.com/geek1011/BookBrowser/util"
"github.com/geek1011/BookBrowser/util/sigusr"
"github.com/spf13/pflag"
)
var curversion = "dev"
func main() {
workdir, err := os.Getwd()
if err != nil {
log.Fatalf("Fatal error: %s\n", err)
}
deftempdir, err := ioutil.TempDir("", "bookbrowser")
if err != nil {
deftempdir = filepath.Join(workdir, "_temp")
}
bookdir := pflag.StringP("bookdir", "b", workdir, "the directory to load books from (must exist)")
tempdir := pflag.StringP("tempdir", "t", deftempdir, "the directory to store temp files such as cover thumbnails (created on start, deleted on exit unless already exists)")
addr := pflag.StringP("addr", "a", ":8090", "the address to bind the server to ([IP]:PORT)")
nocovers := pflag.BoolP("nocovers", "n", false, "do not index covers")
help := pflag.BoolP("help", "h", false, "Show this help text")
sversion := pflag.Bool("version", false, "Show the version")
pflag.Parse()
if *sversion {
fmt.Printf("BookBrowser %s\n", curversion)
os.Exit(0)
}
if *help || pflag.NArg() != 0 {
fmt.Fprintf(os.Stderr, "Usage: BookBrowser [OPTIONS]\n\nVersion:\n BookBrowser %s\n\nOptions:\n", curversion)
pflag.PrintDefaults()
fmt.Fprintf(os.Stderr, "\n")
if runtime.GOOS == "windows" {
time.Sleep(time.Second * 2)
}
os.Exit(1)
}
noRemoveTempDir := false
log.Printf("BookBrowser %s\n", curversion)
if _, err := os.Stat(*bookdir); err != nil {
if os.IsNotExist(err) {
log.Fatalf("Error: book directory %s does not exist\n", *bookdir)
}
}
if fi, err := os.Stat(*tempdir); err == nil || (fi != nil && fi.IsDir()) {
noRemoveTempDir = true
if *tempdir == deftempdir {
noRemoveTempDir = false
}
}
*bookdir, err = filepath.Abs(*bookdir)
if err != nil {
log.Fatalf("Error: could not resolve book directory %s: %v\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("Error: could not resolve temp directory %s: %v\n", *tempdir, err)
}
sigs := make(chan os.Signal, 1)
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)
go func() {
<-sigs
if noRemoveTempDir {
log.Println("Not removing temp dir because dir already existed at start")
} else {
log.Println("Cleaning up temp dir")
os.RemoveAll(*tempdir)
}
os.Exit(0)
}()
if !strings.Contains(*addr, ":") {
log.Fatalln("Error: invalid listening address")
}
sp := strings.SplitN(*addr, ":", 2)
if sp[0] == "" {
ip := util.GetIP()
if ip != nil {
log.Printf("This server can be accessed at http://%s:%s\n", ip.String(), sp[1])
}
}
s := server.NewServer(*addr, *bookdir, *tempdir, curversion, true, *nocovers)
go func() {
s.RefreshBookIndex()
if len(s.Indexer.BookList()) == 0 {
log.Fatalln("Fatal error: no books found")
}
checkUpdate()
}()
sigusr.Handle(func() {
log.Println("Booklist refresh triggered by SIGUSR1")
s.RefreshBookIndex()
})
err = s.Serve()
if err != nil {
log.Fatalf("Error starting server: %s\n", err)
}
}
func checkUpdate() {
resp, err := http.Get("https://api.github.com/repos/geek1011/BookBrowser/releases/latest")
if err != nil {
return
}
defer resp.Body.Close()
buf, err := ioutil.ReadAll(resp.Body)
if err != nil {
return
}
if resp.StatusCode != 200 {
return
}
var obj struct {
URL string `json:"html_url"`
Tag string `json:"tag_name"`
}
if json.Unmarshal(buf, &obj) != nil {
return
}
if curversion != "dev" {
if !strings.HasPrefix(curversion, obj.Tag) {
log.Printf("Running version %s. Latest version is %s: %s\n", curversion, obj.Tag, obj.URL)
}
}
}