-
Notifications
You must be signed in to change notification settings - Fork 12
/
main.go
98 lines (77 loc) · 2.87 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
/*
Copyright (C) 2017 Sinuhé Téllez Rivera
dir2opds is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
dir2opds is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with dir2opds. If not, see <http://www.gnu.org/licenses/>.
*/
package main
import (
"flag"
"fmt"
"io"
"log"
"net/http"
"path/filepath"
"github.com/dubyte/dir2opds/internal/service"
)
var (
port = flag.String("port", "8080", "The server will listen in this port.")
host = flag.String("host", "0.0.0.0", "The server will listen in this host.")
dirRoot = flag.String("dir", "./books", "A directory with books.")
debug = flag.Bool("debug", false, "If it is set it will log the requests.")
calibre = flag.Bool("calibre", false, "Hide files stored by calibre.")
hideDotFiles = flag.Bool("hide-dot-files", false, "Hide files that starts with dot.")
noCache = flag.Bool("no-cache", false, "adds reponse headers to avoid client from caching.")
)
func main() {
flag.Parse()
if !*debug {
log.SetOutput(io.Discard)
}
fmt.Println(startValues())
var err error
// Use the absoluteCanonical path of the dir parm as the trustedRoot.
// helpfull avoid http trasversal. https://github.com/dubyte/dir2opds/issues/17
*dirRoot, err = absoluteCanonicalPath(*dirRoot)
if err != nil {
log.Fatal(err)
}
log.Printf("%q will be used as your trusted root", *dirRoot)
s := service.OPDS{TrustedRoot: *dirRoot, HideCalibreFiles: *calibre, HideDotFiles: *hideDotFiles, NoCache: *noCache}
http.HandleFunc("/", errorHandler(s.Handler))
log.Fatal(http.ListenAndServe(*host+":"+*port, nil))
}
func startValues() string {
result := fmt.Sprintf("listening in: %s:%s", *host, *port)
return result
}
func errorHandler(f func(http.ResponseWriter, *http.Request) error) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
err := f(w, r)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
log.Printf("handling %q: %v", r.RequestURI, err)
}
}
}
// absoluteCanonicalPath returns the canonical path of the absolute path that was passed
func absoluteCanonicalPath(aPath string) (string, error) {
// get absolute path
aPath, err := filepath.Abs(aPath)
if err != nil {
return "", fmt.Errorf("get absolute path %s: %w", aPath, err)
}
// get canonical path
aPath, err = filepath.EvalSymlinks(aPath)
if err != nil {
return "", fmt.Errorf("get connonical path from absolute path %s: %w", aPath, err)
}
return aPath, nil
}