Skip to content

Commit

Permalink
Remove caching in sitehandler
Browse files Browse the repository at this point in the history
  • Loading branch information
toshinari123 committed Feb 5, 2024
1 parent 9c1086c commit b74cf6c
Showing 1 changed file with 2 additions and 21 deletions.
23 changes: 2 additions & 21 deletions internal/handler/site/site_handler.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package site

import (
"bytes"
"context"
"fmt"
"io"
Expand All @@ -11,7 +10,6 @@ import (
"strings"
"time"

"github.com/oursky/pageship/internal/cache"
"github.com/oursky/pageship/internal/httputil"
"github.com/oursky/pageship/internal/site"
)
Expand All @@ -20,19 +18,12 @@ type SiteHandler struct {
desc *site.Descriptor
publicFS site.FS
next http.Handler
cc *cache.ContentCache
}

func NewSiteHandler(desc *site.Descriptor, middlewares []Middleware) *SiteHandler {
cc, err := cache.NewContentCache(1 << 24, false) //16 MiB
if err != nil {
cc = nil
}

h := &SiteHandler{
desc: desc,
publicFS: site.SubFS(desc.FS, path.Clean("/"+desc.Config.Public)),
cc: cc,
}

publicDesc := *desc
Expand Down Expand Up @@ -85,23 +76,13 @@ func (h *SiteHandler) serveFile(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Cache-Control", "public, max-age=31536000, no-cache")
}

lReader := &lazyReader{
reader := &lazyReader{
fs: h.publicFS,
path: r.URL.Path,
ctx: r.Context(),
}
defer lReader.Close()
var reader = io.ReadSeeker(lReader)
defer reader.Close()

if info.Hash != "" {
cell, err := h.cc.GetContent(info.Hash, reader)
if err != nil {
http.Error(w, "internal server error", http.StatusInternalServerError)
return
} else {
reader = bytes.NewReader(cell.Data.Bytes())
}
}
writer := httputil.NewTimeoutResponseWriter(w, 10*time.Second)
http.ServeContent(writer, r, path.Base(r.URL.Path), info.ModTime, reader)
}
Expand Down

0 comments on commit b74cf6c

Please sign in to comment.