From e35e34e52d95d4a97c069ea70ac7eda99559837f Mon Sep 17 00:00:00 2001 From: Sinuhe Tellez Rivera Date: Tue, 18 Jun 2024 01:42:33 -0400 Subject: [PATCH] Add no cache argument to app (#25) * chore: adding not cache argument * chore(no-cache): updating readme and changelog for no-cache change --- CHANGELOG.md | 10 ++++++++++ README.md | 2 ++ internal/service/service.go | 6 ++++++ internal/service/service_test.go | 2 +- main.go | 3 ++- 5 files changed, 21 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8108cb3..86e4c09 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,16 @@ 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). +## [1.2.0] - 2024-06-18 + +### Added + +- no-cache argument can be passed to add Cache-Control and expires headers to let the client know we dont want to use cache. + +### Changed + +- make file allow to build for multiple goarch and goos. + ## [1.1.0] - 2024-06-14 ### Changed diff --git a/README.md b/README.md index ebb6300..ae206bb 100644 --- a/README.md +++ b/README.md @@ -38,6 +38,8 @@ Usage of dir2opds: Hide files that starts with dot. -host string The server will listen in this host. (default "0.0.0.0") + -no-cache + adds reponse headers to avoid client from caching. -port string The server will listen in this port. (default "8080") ``` diff --git a/internal/service/service.go b/internal/service/service.go index 9ac3290..8818bda 100644 --- a/internal/service/service.go +++ b/internal/service/service.go @@ -48,6 +48,7 @@ type OPDS struct { TrustedRoot string HideCalibreFiles bool HideDotFiles bool + NoCache bool } type IsDirer interface { @@ -99,6 +100,11 @@ func (s OPDS) Handler(w http.ResponseWriter, req *http.Request) error { return nil } + if s.NoCache { + w.Header().Add("Cache-Control", "no-cache, no-store, must-revalidate") + w.Header().Add("Expires", "0") + } + navFeed := s.makeFeed(fPath, req) var content []byte diff --git a/internal/service/service_test.go b/internal/service/service_test.go index e02f6ce..c5a3b4a 100644 --- a/internal/service/service_test.go +++ b/internal/service/service_test.go @@ -35,7 +35,7 @@ func TestHandler(t *testing.T) { for name, tc := range tests { t.Run(name, func(t *testing.T) { // setup - s := service.OPDS{"testdata", true, true} + s := service.OPDS{"testdata", true, true, true} w := httptest.NewRecorder() req := httptest.NewRequest(http.MethodGet, tc.input, nil) service.TimeNow = func() time.Time { diff --git a/main.go b/main.go index 9cdcdeb..453e9cb 100644 --- a/main.go +++ b/main.go @@ -35,6 +35,7 @@ var ( 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() { @@ -57,7 +58,7 @@ func main() { log.Printf("%q will be used as your trusted root", *dirRoot) - s := service.OPDS{TrustedRoot: *dirRoot, HideCalibreFiles: *calibre, HideDotFiles: *hideDotFiles} + s := service.OPDS{TrustedRoot: *dirRoot, HideCalibreFiles: *calibre, HideDotFiles: *hideDotFiles, NoCache: *noCache} http.HandleFunc("/", errorHandler(s.Handler))