From 3d8dd95c809e42b0d8ed6cda31391dada16289b6 Mon Sep 17 00:00:00 2001 From: Sinuhe Tellez Date: Thu, 10 Jun 2021 01:00:52 -0400 Subject: [PATCH] time now i called only when you run the service --- CHANGELOG.md | 4 ++++ internal/service/service.go | 17 +++++++++-------- internal/service/service_test.go | 6 +++--- 3 files changed, 16 insertions(+), 11 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 59e7a0d..e934b31 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,10 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). +## [0.1.0] - 2021-06-10 +### Changed +- time is calculated only once + ## [0.0.11] - 2021-06-05 ### Changed - return to filepath as the best way to handle paths for different platforms. diff --git a/internal/service/service.go b/internal/service/service.go index 54c2c9b..4e43c33 100644 --- a/internal/service/service.go +++ b/internal/service/service.go @@ -34,7 +34,7 @@ type OPDS struct { AuthorURI string } -var TimeNowFunc = timeNow +var TimeNow = timeNowFunc() func (s OPDS) Handler(w http.ResponseWriter, req *http.Request) error { fPath := filepath.Join(s.DirRoot, req.URL.Path) @@ -57,14 +57,14 @@ func (s OPDS) Handler(w http.ResponseWriter, req *http.Request) error { } content = append([]byte(xml.Header), content...) - http.ServeContent(w, req, "feed.xml", TimeNowFunc(), bytes.NewReader(content)) + http.ServeContent(w, req, "feed.xml", TimeNow(), bytes.NewReader(content)) return nil } func (s OPDS) getContent(req *http.Request, dirpath string) (result []byte, err error) { feed := s.makeFeed(dirpath, req) if getPathType(dirpath) == pathTypeDirOfFiles { - acFeed := &opds.AcquisitionFeed{&feed, "http://purl.org/dc/terms/", "http://opds-spec.org/2010/catalog"} + acFeed := &opds.AcquisitionFeed{Feed: &feed, Dc: "http://purl.org/dc/terms/", Opds: "http://opds-spec.org/2010/catalog"} result, err = xml.MarshalIndent(acFeed, " ", " ") } else { result, err = xml.MarshalIndent(feed, " ", " ") @@ -79,7 +79,7 @@ func (s OPDS) makeFeed(dirpath string, req *http.Request) atom.Feed { ID(req.URL.Path). Title("Catalog in " + req.URL.Path). Author(opds.AuthorBuilder.Name(s.Author).Email(s.AuthorEmail).URI(s.AuthorURI).Build()). - Updated(TimeNowFunc()). + Updated(TimeNow()). AddLink(opds.LinkBuilder.Rel("start").Href("/").Type(navigationType).Build()) fis, _ := ioutil.ReadDir(dirpath) @@ -89,8 +89,8 @@ func (s OPDS) makeFeed(dirpath string, req *http.Request) atom.Feed { AddEntry(opds.EntryBuilder. ID(req.URL.Path + fi.Name()). Title(fi.Name()). - Updated(TimeNowFunc()). - Published(TimeNowFunc()). + Updated(TimeNow()). + Published(TimeNow()). AddLink(opds.LinkBuilder. Rel(getRel(fi.Name(), pathType)). Title(fi.Name()). @@ -153,6 +153,7 @@ func isFile(fi os.FileInfo) bool { return !fi.IsDir() } -func timeNow() time.Time { - return time.Now() +func timeNowFunc() func() time.Time { + t := time.Now() + return func() time.Time { return t } } diff --git a/internal/service/service_test.go b/internal/service/service_test.go index 33d321b..e1dcbfc 100644 --- a/internal/service/service_test.go +++ b/internal/service/service_test.go @@ -14,9 +14,9 @@ import ( func TestHandler(t *testing.T) { // pre-setup - nowFn := service.TimeNowFunc + nowFn := service.TimeNow defer func() { - service.TimeNowFunc = nowFn + service.TimeNow = nowFn }() tests := map[string]struct { @@ -36,7 +36,7 @@ func TestHandler(t *testing.T) { s := service.OPDS{"testdata", "", "", ""} w := httptest.NewRecorder() req := httptest.NewRequest(http.MethodGet, tc.input, nil) - service.TimeNowFunc = func() time.Time { + service.TimeNow = func() time.Time { return time.Date(2020, 05, 25, 00, 00, 00, 0, time.UTC) }