forked from gohugoio/hugo
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a HTTP cache for remote resources.
Fixes gohugoio#12502
- Loading branch information
Showing
27 changed files
with
982 additions
and
198 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,169 @@ | ||
// Copyright 2024 The Hugo Authors. All rights reserved. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package httpcache | ||
|
||
import ( | ||
"time" | ||
|
||
"github.com/gobwas/glob" | ||
"github.com/gohugoio/hugo/common/predicate" | ||
"github.com/gohugoio/hugo/config" | ||
"github.com/mitchellh/mapstructure" | ||
) | ||
|
||
// DefaultConfig holds the default configuration for the HTTP cache. | ||
var DefaultConfig = Config{ | ||
For: GlobMatcher{ | ||
Includes: []string{"**"}, | ||
Excludes: []string{}, | ||
}, | ||
PollConfigs: []PollConfig{ | ||
{ | ||
For: GlobMatcher{ | ||
Includes: []string{"**"}, | ||
Excludes: []string{"**{.jpg,.jpeg,.png,.webp,.gif}"}, | ||
}, | ||
IntervalLow: 1 * time.Second, | ||
IntervalHigh: 30 * time.Second, | ||
}, | ||
}, | ||
} | ||
|
||
// Config holds the configuration for the HTTP cache. | ||
type Config struct { | ||
// Enable HTTP cache behaviour (RFC 9111) for these rsources. | ||
For GlobMatcher | ||
|
||
// PollConfigs holds a list of configurations for polling remote resources to detect changes in watch mode. | ||
PollConfigs []PollConfig | ||
} | ||
|
||
func (c *Config) Compile() (ConfigCompiled, error) { | ||
var cc ConfigCompiled | ||
|
||
p, err := c.For.CompilePredicate() | ||
if err != nil { | ||
return cc, err | ||
} | ||
|
||
cc.For = p | ||
|
||
for _, pc := range c.PollConfigs { | ||
p, err := pc.For.CompilePredicate() | ||
if err != nil { | ||
return cc, err | ||
} | ||
|
||
cc.PollConfigs = append(cc.PollConfigs, PollConfigCompiled{ | ||
For: p, | ||
IntervalLow: pc.IntervalLow, | ||
IntervalHigh: pc.IntervalHigh, | ||
}) | ||
} | ||
|
||
return cc, nil | ||
} | ||
|
||
// PollConfig holds the configuration for polling remote resources to detect changes in watch mode. | ||
type PollConfig struct { | ||
// What remote resources to apply this configuration to. | ||
For GlobMatcher | ||
// IntervalLow is the lower bound for the polling interval. | ||
// This is the starting point when the resource has recently changed. | ||
IntervalLow time.Duration | ||
|
||
// IntervalHigh is the upper bound for the polling interval. | ||
// This is the interval used when the resource is stable. | ||
IntervalHigh time.Duration | ||
} | ||
|
||
type GlobMatcher struct { | ||
// Excludes holds a list of glob patterns that will be excluded. | ||
Excludes []string | ||
|
||
// Includes holds a list of glob patterns that will be included. | ||
Includes []string | ||
} | ||
|
||
type ConfigCompiled struct { | ||
For predicate.P[string] | ||
PollConfigs []PollConfigCompiled | ||
} | ||
|
||
func (c *ConfigCompiled) PollConfigFor(s string) PollConfigCompiled { | ||
for _, pc := range c.PollConfigs { | ||
if pc.For(s) { | ||
return pc | ||
} | ||
} | ||
return PollConfigCompiled{} | ||
} | ||
|
||
type PollConfigCompiled struct { | ||
For predicate.P[string] | ||
IntervalLow time.Duration | ||
IntervalHigh time.Duration | ||
} | ||
|
||
func (p PollConfigCompiled) IsZero() bool { | ||
return p.For == nil | ||
} | ||
|
||
func (gm *GlobMatcher) CompilePredicate() (func(string) bool, error) { | ||
var p predicate.P[string] | ||
for _, include := range gm.Includes { | ||
g, err := glob.Compile(include, '/') | ||
if err != nil { | ||
return nil, err | ||
} | ||
fn := func(s string) bool { | ||
return g.Match(s) | ||
} | ||
p = p.Or(fn) | ||
} | ||
|
||
for _, exclude := range gm.Excludes { | ||
g, err := glob.Compile(exclude, '/') | ||
if err != nil { | ||
return nil, err | ||
} | ||
fn := func(s string) bool { | ||
return !g.Match(s) | ||
} | ||
p = p.And(fn) | ||
} | ||
|
||
return p, nil | ||
} | ||
|
||
func DecodeConfig(bcfg config.BaseConfig, m map[string]any) (Config, error) { | ||
var c Config = DefaultConfig | ||
|
||
dc := &mapstructure.DecoderConfig{ | ||
Result: &c, | ||
DecodeHook: mapstructure.StringToTimeDurationHookFunc(), | ||
WeaklyTypedInput: true, | ||
} | ||
|
||
decoder, err := mapstructure.NewDecoder(dc) | ||
if err != nil { | ||
return c, err | ||
} | ||
|
||
if err := decoder.Decode(m); err != nil { | ||
return c, err | ||
} | ||
|
||
return c, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
// Copyright 2024 The Hugo Authors. All rights reserved. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package httpcache_test | ||
|
||
import ( | ||
"testing" | ||
"time" | ||
|
||
qt "github.com/frankban/quicktest" | ||
"github.com/gohugoio/hugo/hugolib" | ||
) | ||
|
||
func TestConfigCustom(t *testing.T) { | ||
files := ` | ||
-- hugo.toml -- | ||
[httpcache] | ||
[httpcache.for] | ||
includes = ["**gohugo.io**"] | ||
excludes = [] | ||
[[httpcache.pollconfigs]] | ||
intervalLow = "5s" | ||
intervalHigh = "32s" | ||
[httpcache.pollconfigs.for] | ||
includes = ["**gohugo.io**"] | ||
excludes = [] | ||
` | ||
b := hugolib.Test(t, files) | ||
|
||
httpcacheConf := b.H.Configs.Base.HTTPCache | ||
compiled := b.H.Configs.Base.C.HTTPCache | ||
|
||
b.Assert(httpcacheConf.For.Includes, qt.DeepEquals, []string{"**gohugo.io**"}) | ||
b.Assert(httpcacheConf.For.Excludes, qt.DeepEquals, []string{}) | ||
|
||
pc := compiled.PollConfigFor("https://gohugo.io/foo.jpg") | ||
b.Assert(pc.IntervalLow, qt.Equals, 5*time.Second) | ||
b.Assert(pc.IntervalHigh, qt.Equals, 32*time.Second) | ||
b.Assert(compiled.PollConfigFor("https://example.com/foo.jpg").IsZero(), qt.IsTrue) | ||
} | ||
|
||
func TestConfigDefault(t *testing.T) { | ||
files := ` | ||
-- hugo.toml -- | ||
` | ||
b := hugolib.Test(t, files) | ||
|
||
compiled := b.H.Configs.Base.C.HTTPCache | ||
|
||
b.Assert(compiled.For("https://gohugo.io/posts.json"), qt.IsTrue) | ||
b.Assert(compiled.For("https://gohugo.io/foo.jpg"), qt.IsTrue) | ||
b.Assert(compiled.PollConfigFor("https://gohugo.io/foo.jpg").IsZero(), qt.IsTrue) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
// Copyright 2024 The Hugo Authors. All rights reserved. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package httpcache | ||
|
||
import ( | ||
"testing" | ||
|
||
qt "github.com/frankban/quicktest" | ||
) | ||
|
||
func TestGlobMatcher(t *testing.T) { | ||
c := qt.New(t) | ||
|
||
g := GlobMatcher{ | ||
Includes: []string{"**/*.jpg", "**.png", "**/bar/**"}, | ||
Excludes: []string{"**/foo.jpg", "**.css"}, | ||
} | ||
|
||
p, err := g.CompilePredicate() | ||
c.Assert(err, qt.IsNil) | ||
|
||
c.Assert(p("foo.jpg"), qt.IsFalse) | ||
c.Assert(p("foo.png"), qt.IsTrue) | ||
c.Assert(p("foo/bar.jpg"), qt.IsTrue) | ||
c.Assert(p("foo/bar.png"), qt.IsTrue) | ||
c.Assert(p("foo/bar/foo.jpg"), qt.IsFalse) | ||
c.Assert(p("foo/bar/foo.css"), qt.IsFalse) | ||
c.Assert(p("foo.css"), qt.IsFalse) | ||
c.Assert(p("foo/bar/foo.css"), qt.IsFalse) | ||
c.Assert(p("foo/bar/foo.xml"), qt.IsTrue) | ||
} |
Oops, something went wrong.