Skip to content

Commit 5b3bb7e

Browse files
author
Luca Buzzi
committed
add configurable static file caching with defaults
- added MaxAge and CacheDuration fields to server configuration - implemented default values (0 for max-age, 10s for cache duration) - integrated caching parameters with fiber's static file handling - applied caching settings to all registered static routes
1 parent 6d66656 commit 5b3bb7e

File tree

2 files changed

+35
-15
lines changed

2 files changed

+35
-15
lines changed

evo.config.go

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,11 @@ var ProcessID int
3636

3737
var DefaultPath = map[string][]string{}
3838

39+
const (
40+
defaultMaxAge = 0
41+
defaultCacheDuration = 10 * time.Second
42+
)
43+
3944
type Configuration struct {
4045
Log struct {
4146
WriteFile bool `yaml:"writefile"`
@@ -61,20 +66,22 @@ type Configuration struct {
6166
} `yaml:"jwt"`
6267

6368
Server struct {
64-
Host string `yaml:"host"`
65-
Port string `yaml:"port"`
66-
Cert string `yaml:"cert"`
67-
Key string `yaml:"key"`
68-
HTTPS bool `yaml:"https"`
69-
Name string `yaml:"name"`
70-
MaxUploadSize string `yaml:"max-upload-size"`
71-
StrictRouting bool `yaml:"strict-routing"`
72-
CaseSensitive bool `yaml:"case-sensitive"`
73-
RequestID bool `yaml:"request-id"`
74-
Debug bool `yaml:"debug"`
75-
Recover bool `yaml:"recover"`
76-
ProxyHeader string `yaml:"proxy-header"`
77-
ReduceMemoryUsage bool `yaml:"reduce-memory-usage"`
69+
Host string `yaml:"host"`
70+
Port string `yaml:"port"`
71+
Cert string `yaml:"cert"`
72+
Key string `yaml:"key"`
73+
HTTPS bool `yaml:"https"`
74+
Name string `yaml:"name"`
75+
MaxUploadSize string `yaml:"max-upload-size"`
76+
StrictRouting bool `yaml:"strict-routing"`
77+
CaseSensitive bool `yaml:"case-sensitive"`
78+
RequestID bool `yaml:"request-id"`
79+
Debug bool `yaml:"debug"`
80+
Recover bool `yaml:"recover"`
81+
ProxyHeader string `yaml:"proxy-header"`
82+
ReduceMemoryUsage bool `yaml:"reduce-memory-usage"`
83+
MaxAge int `yaml:"static-max-age"`
84+
CacheDuration time.Duration `yaml:"static-cache-duration"`
7885
} `yaml:"server"`
7986

8087
Database struct {

evo.go

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,9 +129,22 @@ func Run() {
129129
}
130130
parseArgs(false)
131131

132+
maxAge := config.Server.MaxAge
133+
if maxAge <= 0 {
134+
maxAge = defaultMaxAge
135+
}
136+
137+
cacheDuration := config.Server.CacheDuration
138+
if cacheDuration <= 0 {
139+
cacheDuration = defaultCacheDuration
140+
}
141+
132142
//Static Files
133143
for _, item := range statics {
134-
app.Static(item[0], item[1])
144+
app.Static(item[0], item[1], fiber.Static{
145+
MaxAge: maxAge,
146+
CacheDuration: cacheDuration,
147+
})
135148
}
136149

137150
for _, item := range onReady {

0 commit comments

Comments
 (0)