Skip to content
This repository has been archived by the owner on Jun 27, 2020. It is now read-only.

Commit

Permalink
Switched to cli library for command line parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
pgaskin committed Aug 30, 2017
1 parent e0c3c77 commit 729153a
Show file tree
Hide file tree
Showing 20 changed files with 5,480 additions and 58 deletions.
133 changes: 77 additions & 56 deletions bookbrowser.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package main

import (
"flag"
"io/ioutil"
"log"
"net"
Expand All @@ -10,11 +9,9 @@ import (
"path/filepath"
"strings"
"syscall"
)

var bookdir *string
var tempdir *string
var addr *string
"github.com/urfave/cli"
)

var curversion = "dev"

Expand All @@ -32,72 +29,96 @@ func GetIP() net.IP {
}

func main() {
wd, err := os.Getwd()
workdir, err := os.Getwd()
if err != nil {
log.Fatalf("Fatal error: %s\n", err)
}

td, err := ioutil.TempDir("", "bookbrowser")
tempdir, err := ioutil.TempDir("", "bookbrowser")
if err != nil {
td = filepath.Join(wd, "_temp")
}

bookdir = flag.String("bookdir", wd, "The directory to get books from. This directory must exist.")
tempdir = flag.String("tempdir", td, "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)
}
tempdir = filepath.Join(workdir, "_temp")
}

*bookdir, err = filepath.Abs(*bookdir)
if err != nil {
log.Fatalf("Fatal error: Could not resolve book directory %s: %s\n", *bookdir, err)
app := cli.NewApp()
app.Name = "BookBrowser"
app.Usage = "Web-based eBook server supporting ePub and PDF."
app.Version = curversion
app.Flags = []cli.Flag{
cli.StringFlag{
Name: "bookdir, b",
Value: workdir,
Usage: "Load books from `DIR`. The directory must exist.",
},
cli.StringFlag{
Name: "tempdir, t",
Value: tempdir,
Usage: "Use `DIR` as the location for storing temporary files such as cover thumbnails. The directory is created on start and deleted on exit.",
},
cli.StringFlag{
Name: "addr, a",
Value: ":8090",
Usage: "`ADDR` is the address to bind the server to. It is in the format IP:PORT. The IP is optional.",
},
}
app.HideHelp = true
app.Action = func(c *cli.Context) {
bookdir := c.String("bookdir")
tempdir := c.String("tempdir")
addr := c.String("addr")

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)
}
}

if _, err := os.Stat(*tempdir); os.IsNotExist(err) {
os.Mkdir(*tempdir, os.ModePerm)
}
bookdir, err = filepath.Abs(bookdir)
if err != nil {
log.Fatalf("Fatal error: Could not resolve book directory %s: %s\n", bookdir, err)
}

*tempdir, err = filepath.Abs(*tempdir)
if err != nil {
log.Fatalf("Fatal error: Could not resolve temp directory %s: %s\n", *tempdir, err)
}
if _, err := os.Stat(tempdir); os.IsNotExist(err) {
os.Mkdir(tempdir, os.ModePerm)
}

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)
}()
tempdir, err = filepath.Abs(tempdir)
if err != nil {
log.Fatalf("Fatal error: Could not resolve temp directory %s: %s\n", tempdir, err)
}

books, err := NewBookListFromDir(*bookdir, *tempdir, true)
if err != nil {
log.Fatalf("Fatal error indexing books: %s\n", 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 := NewBookListFromDir(bookdir, tempdir, true)
if err != nil {
log.Fatalf("Fatal error indexing books: %s\n", err)
}

if len(*books) == 0 {
log.Fatalln("Fatal error: no books found")
}
if len(*books) == 0 {
log.Fatalln("Fatal error: no books found")
}

if !strings.Contains(*addr, ":") {
log.Fatalln("Invalid listening address")
}
if !strings.Contains(addr, ":") {
log.Fatalln("Invalid listening address")
}

sp := strings.SplitN(*addr, ":", 2)
if sp[0] == "" {
ip := GetIP()
if ip != nil {
log.Printf("This server can be accessed at http://%s:%s\n", ip.String(), sp[1])
sp := strings.SplitN(addr, ":", 2)
if sp[0] == "" {
ip := GetIP()
if ip != nil {
log.Printf("This server can be accessed at http://%s:%s\n", ip.String(), sp[1])
}
}
}

runServer(*books, *addr)
runServer(*books, addr, tempdir)
}
app.Run(os.Args)
}
4 changes: 2 additions & 2 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -384,13 +384,13 @@ func RandomHandler(w http.ResponseWriter, r *http.Request, _ httprouter.Params)

var books []Book

func runServer(bks []Book, addr string) {
func runServer(bks []Book, addr, tempdir string) {
books = bks

router := httprouter.New()

router.ServeFiles("/static/*filepath", assetFS())
router.ServeFiles("/covers/*filepath", http.Dir(*tempdir))
router.ServeFiles("/covers/*filepath", http.Dir(tempdir))

router.GET("/download", DownloadListHandler)
router.GET("/download/:filename", DownloadHandler)
Expand Down
Loading

0 comments on commit 729153a

Please sign in to comment.