From 8e62288b0ba9bf2dba39475597864e8353d24e67 Mon Sep 17 00:00:00 2001 From: Sinuhe Date: Sat, 3 Mar 2018 15:12:41 -0500 Subject: [PATCH] src: improving names --- author_builder.go | 5 ++-- entry_builder.go | 6 +++-- feed_builder.go | 6 +++-- link_builder.go | 1 + main.go | 66 ++++++++++++++++++++++++++--------------------- text_builder.go | 1 + 6 files changed, 49 insertions(+), 36 deletions(-) diff --git a/author_builder.go b/author_builder.go index b8a476e..de57373 100644 --- a/author_builder.go +++ b/author_builder.go @@ -19,12 +19,13 @@ func (a authorBuilder) Email(email string) authorBuilder { return builder.Set(a, "Email", email).(authorBuilder) } -func (a authorBuilder) InnerXml(inner string) authorBuilder { - return builder.Set(a, "InnerXml", inner).(authorBuilder) +func (a authorBuilder) InnerXML(inner string) authorBuilder { + return builder.Set(a, "InnerXML", inner).(authorBuilder) } func (a authorBuilder) Build() atom.Person { return builder.GetStruct(a).(atom.Person) } +// AuthorBuilder is a fluent immutable builder to build OPDS Authors var AuthorBuilder = builder.Register(authorBuilder{}, atom.Person{}).(authorBuilder) diff --git a/entry_builder.go b/entry_builder.go index 3363f6f..ed5d339 100644 --- a/entry_builder.go +++ b/entry_builder.go @@ -1,9 +1,10 @@ package main import ( + "time" + "github.com/lann/builder" "golang.org/x/tools/blog/atom" - "time" ) type entryBuilder builder.Builder @@ -12,7 +13,7 @@ func (e entryBuilder) Title(title string) entryBuilder { return builder.Set(e, "Title", title).(entryBuilder) } -func (e entryBuilder) Id(id string) entryBuilder { +func (e entryBuilder) ID(id string) entryBuilder { return builder.Set(e, "ID", id).(entryBuilder) } @@ -44,4 +45,5 @@ func (e entryBuilder) Build() atom.Entry { return builder.GetStruct(e).(atom.Entry) } +// EntryBuilder is a fluent immutable builder to build OPDS entries var EntryBuilder = builder.Register(entryBuilder{}, atom.Entry{}).(entryBuilder) diff --git a/feed_builder.go b/feed_builder.go index d161b8a..1b5ae72 100644 --- a/feed_builder.go +++ b/feed_builder.go @@ -1,9 +1,10 @@ package main import ( + "time" + "github.com/lann/builder" "golang.org/x/tools/blog/atom" - "time" ) type feedBuilder builder.Builder @@ -12,7 +13,7 @@ func (f feedBuilder) Title(title string) feedBuilder { return builder.Set(f, "Title", title).(feedBuilder) } -func (f feedBuilder) Id(id string) feedBuilder { +func (f feedBuilder) ID(id string) feedBuilder { return builder.Set(f, "ID", id).(feedBuilder) } @@ -36,4 +37,5 @@ func (f feedBuilder) Build() atom.Feed { return builder.GetStruct(f).(atom.Feed) } +// FeedBuilder is a fluent immutable builder to build OPDS Feeds var FeedBuilder = builder.Register(feedBuilder{}, atom.Feed{}).(feedBuilder) diff --git a/link_builder.go b/link_builder.go index 7c83e87..d5ea0f0 100644 --- a/link_builder.go +++ b/link_builder.go @@ -35,4 +35,5 @@ func (l linkBuilder) Build() atom.Link { return builder.GetStruct(l).(atom.Link) } +// LinkBuilder is a fluent immutable builder to build OPDS Links var LinkBuilder = builder.Register(linkBuilder{}, atom.Link{}).(linkBuilder) diff --git a/main.go b/main.go index cae1815..9d55698 100644 --- a/main.go +++ b/main.go @@ -37,14 +37,14 @@ var ( port, dirRoot, author, - authorUri, + authorURI, authorEmail string ) const acquisitionType = "application/atom+xml;profile=opds-catalog;kind=acquisition" const navegationType = "application/atom+xml;profile=opds-catalog;kind=navigation" -type AcquisitionFeed struct { +type acquisitionFeed struct { *atom.Feed Dc string `xml:"xmlns:dc,attr"` Opds string `xml:"xmlns:opds,attr"` @@ -56,32 +56,11 @@ func init() { mime.AddExtensionType(".fb2", "txt/xml") } -func handler(w http.ResponseWriter, req *http.Request) error { - fpath := filepath.Join(dirRoot, req.URL.Path) - - fi, err := os.Stat(fpath) - if err != nil { - return err - } - - if fi.IsDir() { - content, err := getContent(req, fpath) - if err != nil { - return err - } - http.ServeContent(w, req, "feed.xml", time.Now(), bytes.NewReader(content)) - } else { - http.ServeFile(w, req, fpath) - } - - return nil -} - func main() { flag.StringVar(&port, "port", "8080", "The server will listen in this port") flag.StringVar(&dirRoot, "dir", "./books", "A directory with books") flag.StringVar(&author, "serveFeedauthor", "", "The feed's author") - flag.StringVar(&authorUri, "uri", "", "The feed's author uri") + flag.StringVar(&authorURI, "uri", "", "The feed's author uri") flag.StringVar(&authorEmail, "email", "", "The feed's author email") flag.Parse() @@ -90,10 +69,32 @@ func main() { log.Fatal(http.ListenAndServe(":"+port, nil)) } +func handler(w http.ResponseWriter, req *http.Request) error { + fpath := filepath.Join(dirRoot, req.URL.Path) + + fi, err := os.Stat(fpath) + if err != nil { + return err + } + + if isFile(fi) { + http.ServeFile(w, req, fpath) + return nil + } + + content, err := getContent(req, fpath) + if err != nil { + return err + } + + http.ServeContent(w, req, "feed.xml", time.Now(), bytes.NewReader(content)) + return nil +} + func getContent(req *http.Request, dirpath string) (result []byte, err error) { feed := makeFeed(dirpath, req) if isAcquisition(dirpath) { - acFeed := &AcquisitionFeed{&feed, "http://purl.org/dc/terms/", "http://opds-spec.org/2010/catalog"} + acFeed := &acquisitionFeed{&feed, "http://purl.org/dc/terms/", "http://opds-spec.org/2010/catalog"} result, err = xml.MarshalIndent(acFeed, " ", " ") } else { @@ -104,9 +105,9 @@ func getContent(req *http.Request, dirpath string) (result []byte, err error) { func makeFeed(dirpath string, req *http.Request) atom.Feed { feedBuilder := FeedBuilder. - Id(req.URL.Path). + ID(req.URL.Path). Title("Catalog in " + req.URL.Path). - Author(AuthorBuilder.Name(author).Email(authorEmail).URI(authorUri).Build()). + Author(AuthorBuilder.Name(author).Email(authorEmail).URI(authorURI).Build()). Updated(time.Now()). AddLink(LinkBuilder.Rel("start").Href("/").Type(navegationType).Build()) @@ -115,7 +116,7 @@ func makeFeed(dirpath string, req *http.Request) atom.Feed { linkIsAcquisition := isAcquisition(filepath.Join(dirpath, fi.Name())) feedBuilder = feedBuilder. AddEntry(EntryBuilder. - Id(req.URL.Path + fi.Name()). + ID(req.URL.Path + fi.Name()). Title(fi.Name()). Updated(time.Now()). Published(time.Now()). @@ -155,22 +156,27 @@ func getType(name string, acquisition bool) (linkType string) { func getHref(req *http.Request, name string) string { return filepath.Join(req.URL.EscapedPath(), url.PathEscape(name)) } + func isAcquisition(dirpath string) bool { fi, _ := os.Stat(dirpath) - if !fi.IsDir() { + if isFile(fi) { return false } fis, _ := ioutil.ReadDir(dirpath) for _, fi := range fis { - if !fi.IsDir() { + if isFile(fi) { return true } } return false } +func isFile(fi os.FileInfo) bool { + return !fi.IsDir() +} + func errorHandler(f func(http.ResponseWriter, *http.Request) error) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { err := f(w, r) diff --git a/text_builder.go b/text_builder.go index 6a1a0c8..dddd1e6 100644 --- a/text_builder.go +++ b/text_builder.go @@ -19,4 +19,5 @@ func (t textBuilder) Build() atom.Text { return builder.GetStruct(t).(atom.Text) } +// TextBuilder is a fluent immutable builder to build OPDS texts var TextBuilder = builder.Register(textBuilder{}, atom.Text{}).(textBuilder)