Skip to content

Commit

Permalink
Add no cache argument to app (#25)
Browse files Browse the repository at this point in the history
* chore: adding not cache argument

* chore(no-cache): updating readme and changelog for no-cache change
  • Loading branch information
dubyte committed Jun 18, 2024
1 parent a42fb81 commit e35e34e
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 2 deletions.
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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")
```
Expand Down
6 changes: 6 additions & 0 deletions internal/service/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ type OPDS struct {
TrustedRoot string
HideCalibreFiles bool
HideDotFiles bool
NoCache bool
}

type IsDirer interface {
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion internal/service/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
3 changes: 2 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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))

Expand Down

0 comments on commit e35e34e

Please sign in to comment.