dir2opds/main.go

71 lines
2.0 KiB
Go
Raw Normal View History

2017-03-04 03:41:48 +00:00
/*
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"
2017-03-04 03:41:48 +00:00
"io/ioutil"
"log"
"net/http"
2018-01-14 03:19:54 +00:00
2021-05-06 06:48:02 +00:00
"github.com/dubyte/dir2opds/internal/service"
2017-03-04 03:41:48 +00:00
)
2017-03-24 00:11:58 +00:00
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")
author = flag.String("author", "", "The server Feed author")
authorURI = flag.String("uri", "", "The feed's author uri")
authorEmail = flag.String("email", "", "The feed's author email")
debug = flag.Bool("debug", false, "If it is set it will log the requests")
2017-03-24 00:11:58 +00:00
)
2017-03-04 03:41:48 +00:00
2018-03-03 20:12:41 +00:00
func main() {
2018-03-03 20:12:41 +00:00
flag.Parse()
2020-05-15 18:00:27 +00:00
if !*debug {
log.SetOutput(ioutil.Discard)
}
fmt.Println(startValues())
2021-05-06 06:48:02 +00:00
s := service.OPDS{DirRoot: *dirRoot, Author: *author, AuthorEmail: *authorEmail, AuthorURI: *authorURI}
http.HandleFunc("/", errorHandler(s.Handler))
2018-03-03 20:12:41 +00:00
log.Fatal(http.ListenAndServe(*host+":"+*port, nil))
}
func startValues() string {
result := fmt.Sprintf("listening in: %s:%s", *host, *port)
return result
2018-03-03 20:12:41 +00:00
}
2017-03-24 00:11:58 +00:00
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)
2017-03-04 03:41:48 +00:00
}
}
}