src: improving names

This commit is contained in:
Sinuhe 2018-03-03 15:12:41 -05:00
parent 95c2c22d89
commit 8e62288b0b
6 changed files with 49 additions and 36 deletions

View File

@ -19,12 +19,13 @@ func (a authorBuilder) Email(email string) authorBuilder {
return builder.Set(a, "Email", email).(authorBuilder) return builder.Set(a, "Email", email).(authorBuilder)
} }
func (a authorBuilder) InnerXml(inner string) authorBuilder { func (a authorBuilder) InnerXML(inner string) authorBuilder {
return builder.Set(a, "InnerXml", inner).(authorBuilder) return builder.Set(a, "InnerXML", inner).(authorBuilder)
} }
func (a authorBuilder) Build() atom.Person { func (a authorBuilder) Build() atom.Person {
return builder.GetStruct(a).(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) var AuthorBuilder = builder.Register(authorBuilder{}, atom.Person{}).(authorBuilder)

View File

@ -1,9 +1,10 @@
package main package main
import ( import (
"time"
"github.com/lann/builder" "github.com/lann/builder"
"golang.org/x/tools/blog/atom" "golang.org/x/tools/blog/atom"
"time"
) )
type entryBuilder builder.Builder type entryBuilder builder.Builder
@ -12,7 +13,7 @@ func (e entryBuilder) Title(title string) entryBuilder {
return builder.Set(e, "Title", title).(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) return builder.Set(e, "ID", id).(entryBuilder)
} }
@ -44,4 +45,5 @@ func (e entryBuilder) Build() atom.Entry {
return builder.GetStruct(e).(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) var EntryBuilder = builder.Register(entryBuilder{}, atom.Entry{}).(entryBuilder)

View File

@ -1,9 +1,10 @@
package main package main
import ( import (
"time"
"github.com/lann/builder" "github.com/lann/builder"
"golang.org/x/tools/blog/atom" "golang.org/x/tools/blog/atom"
"time"
) )
type feedBuilder builder.Builder type feedBuilder builder.Builder
@ -12,7 +13,7 @@ func (f feedBuilder) Title(title string) feedBuilder {
return builder.Set(f, "Title", title).(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) return builder.Set(f, "ID", id).(feedBuilder)
} }
@ -36,4 +37,5 @@ func (f feedBuilder) Build() atom.Feed {
return builder.GetStruct(f).(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) var FeedBuilder = builder.Register(feedBuilder{}, atom.Feed{}).(feedBuilder)

View File

@ -35,4 +35,5 @@ func (l linkBuilder) Build() atom.Link {
return builder.GetStruct(l).(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) var LinkBuilder = builder.Register(linkBuilder{}, atom.Link{}).(linkBuilder)

66
main.go
View File

@ -37,14 +37,14 @@ var (
port, port,
dirRoot, dirRoot,
author, author,
authorUri, authorURI,
authorEmail string authorEmail string
) )
const acquisitionType = "application/atom+xml;profile=opds-catalog;kind=acquisition" const acquisitionType = "application/atom+xml;profile=opds-catalog;kind=acquisition"
const navegationType = "application/atom+xml;profile=opds-catalog;kind=navigation" const navegationType = "application/atom+xml;profile=opds-catalog;kind=navigation"
type AcquisitionFeed struct { type acquisitionFeed struct {
*atom.Feed *atom.Feed
Dc string `xml:"xmlns:dc,attr"` Dc string `xml:"xmlns:dc,attr"`
Opds string `xml:"xmlns:opds,attr"` Opds string `xml:"xmlns:opds,attr"`
@ -56,32 +56,11 @@ func init() {
mime.AddExtensionType(".fb2", "txt/xml") 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() { func main() {
flag.StringVar(&port, "port", "8080", "The server will listen in this port") flag.StringVar(&port, "port", "8080", "The server will listen in this port")
flag.StringVar(&dirRoot, "dir", "./books", "A directory with books") flag.StringVar(&dirRoot, "dir", "./books", "A directory with books")
flag.StringVar(&author, "serveFeedauthor", "", "The feed's author") 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.StringVar(&authorEmail, "email", "", "The feed's author email")
flag.Parse() flag.Parse()
@ -90,10 +69,32 @@ func main() {
log.Fatal(http.ListenAndServe(":"+port, nil)) 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) { func getContent(req *http.Request, dirpath string) (result []byte, err error) {
feed := makeFeed(dirpath, req) feed := makeFeed(dirpath, req)
if isAcquisition(dirpath) { 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, " ", " ") result, err = xml.MarshalIndent(acFeed, " ", " ")
} else { } 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 { func makeFeed(dirpath string, req *http.Request) atom.Feed {
feedBuilder := FeedBuilder. feedBuilder := FeedBuilder.
Id(req.URL.Path). ID(req.URL.Path).
Title("Catalog in " + 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()). Updated(time.Now()).
AddLink(LinkBuilder.Rel("start").Href("/").Type(navegationType).Build()) 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())) linkIsAcquisition := isAcquisition(filepath.Join(dirpath, fi.Name()))
feedBuilder = feedBuilder. feedBuilder = feedBuilder.
AddEntry(EntryBuilder. AddEntry(EntryBuilder.
Id(req.URL.Path + fi.Name()). ID(req.URL.Path + fi.Name()).
Title(fi.Name()). Title(fi.Name()).
Updated(time.Now()). Updated(time.Now()).
Published(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 { func getHref(req *http.Request, name string) string {
return filepath.Join(req.URL.EscapedPath(), url.PathEscape(name)) return filepath.Join(req.URL.EscapedPath(), url.PathEscape(name))
} }
func isAcquisition(dirpath string) bool { func isAcquisition(dirpath string) bool {
fi, _ := os.Stat(dirpath) fi, _ := os.Stat(dirpath)
if !fi.IsDir() { if isFile(fi) {
return false return false
} }
fis, _ := ioutil.ReadDir(dirpath) fis, _ := ioutil.ReadDir(dirpath)
for _, fi := range fis { for _, fi := range fis {
if !fi.IsDir() { if isFile(fi) {
return true return true
} }
} }
return false return false
} }
func isFile(fi os.FileInfo) bool {
return !fi.IsDir()
}
func errorHandler(f func(http.ResponseWriter, *http.Request) error) http.HandlerFunc { func errorHandler(f func(http.ResponseWriter, *http.Request) error) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) { return func(w http.ResponseWriter, r *http.Request) {
err := f(w, r) err := f(w, r)

View File

@ -19,4 +19,5 @@ func (t textBuilder) Build() atom.Text {
return builder.GetStruct(t).(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) var TextBuilder = builder.Register(textBuilder{}, atom.Text{}).(textBuilder)