Skip to content

Commit

Permalink
Add contentcachemaxsize to pageship serve
Browse files Browse the repository at this point in the history
  • Loading branch information
toshinari123 committed Feb 19, 2024
1 parent 868056c commit 84d1c11
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 20 deletions.
13 changes: 9 additions & 4 deletions cmd/pageship/app/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ func init() {

serveCmd.PersistentFlags().String("default-site", config.DefaultSite, "default site")
serveCmd.PersistentFlags().String("host-pattern", config.DefaultHostPattern, "host match pattern")

serveCmd.PersistentFlags().Int64("content-cache-max-size", int64(1)<<24, "maximum size of server-side content cache in bytes, default is 16MB")
}

func loadSitesConfig(fsys fs.FS) (*config.SitesConfig, error) {
Expand All @@ -47,7 +49,7 @@ func loadSitesConfig(fsys fs.FS) (*config.SitesConfig, error) {
return conf, nil
}

func makeHandler(prefix string, defaultSite string, hostPattern string) (*handler.Handler, error) {
func makeHandler(prefix string, defaultSite string, hostPattern string, contentCacheMaxSize int64) (*handler.Handler, error) {
dir, err := filepath.Abs(prefix)
if err != nil {
return nil, err
Expand Down Expand Up @@ -90,8 +92,9 @@ func makeHandler(prefix string, defaultSite string, hostPattern string) (*handle
handler, err := handler.NewHandler(context.Background(), zapLogger,
domainResolver, siteResolver,
handler.HandlerConfig{
HostPattern: hostPattern,
MiddlewaresFunc: middleware.Default,
HostPattern: hostPattern,
MiddlewaresFunc: middleware.Default,
ContentCacheMaxSize: contentCacheMaxSize,
})
if err != nil {
return nil, err
Expand All @@ -115,12 +118,14 @@ var serveCmd = &cobra.Command{
defaultSite := viper.GetString("default-site")
hostPattern := viper.GetString("host-pattern")

contentCacheMaxSize := viper.GetInt64("content-cache-max-size")

dir := "."
if len(args) > 0 {
dir = args[0]
}

handler, err := makeHandler(dir, defaultSite, hostPattern)
handler, err := makeHandler(dir, defaultSite, hostPattern, contentCacheMaxSize)
if err != nil {
return fmt.Errorf("failed to setup server: %w", err)
}
Expand Down
32 changes: 16 additions & 16 deletions internal/handler/site/middleware/cache_test.go
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
package middleware_test //black box testing

import (
"bytes"
"context"
"io"
"net/http"
"net/http/httptest"
"testing"
"io"
"time"
"context"
"bytes"
"net/http/httptest"

"github.com/stretchr/testify/assert"
"github.com/oursky/pageship/internal/handler/site/middleware"
"github.com/oursky/pageship/internal/cache"
"github.com/oursky/pageship/internal/site"
"github.com/oursky/pageship/internal/config"
"github.com/oursky/pageship/internal/handler/site/middleware"
"github.com/oursky/pageship/internal/site"
"github.com/stretchr/testify/assert"
)

type mockHandler struct {
Expand All @@ -27,16 +27,16 @@ func (mh *mockHandler) serve(w http.ResponseWriter, r *http.Request) {
type mockFS struct{}

func (m mockFS) Stat(path string) (*site.FileInfo, error) {
return &site.FileInfo {
IsDir: false,
ModTime: time.Now(),
Size: 0,
return &site.FileInfo{
IsDir: false,
ModTime: time.Now(),
Size: 0,
ContentType: "",
Hash: "",
Hash: "",
}, nil
}

type mockRSCloser struct{
type mockRSCloser struct {
io.ReadSeeker
}

Expand All @@ -57,18 +57,18 @@ func TestCache(t *testing.T) {
if err != nil {
return nb, 0, err
}
return nb, int64(nb.Len()), nil
return nb, int64(nb.Len()), nil
}
contentCache, err := cache.NewContentCache[middleware.ContentCacheKey](1<<24, true, load)
assert.Empty(t, err)
cacheContext := middleware.NewCacheContext(contentCache)

sc := config.DefaultSiteConfig()
mockSiteDescriptor := site.Descriptor{
ID: "",
ID: "",
Domain: "",
Config: &sc,
FS: mockFS{},
FS: mockFS{},
}
h := cacheContext.Cache(&mockSiteDescriptor, http.HandlerFunc(mh.serve))

Expand Down

0 comments on commit 84d1c11

Please sign in to comment.