Skip to content

Commit e6b84bc

Browse files
committed
feat: support encoding fs static serve
1 parent 0fd7cd5 commit e6b84bc

File tree

2 files changed

+37
-11
lines changed

2 files changed

+37
-11
lines changed

middleware/static_embed.go

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import (
2828
"bytes"
2929
"embed"
3030
"io"
31+
"io/fs"
3132
"os"
3233
"path/filepath"
3334
"strings"
@@ -109,20 +110,27 @@ type tarFS struct {
109110
Prefix string
110111
// tar file
111112
File string
113+
// embed fs
114+
Embed *embed.FS
112115
}
113116

114117
var _ StaticFile = (*tarFS)(nil)
115118

116119
// NewTarFS returns a new tar static fs
117-
func NewTarFS(file string, prefix string) *tarFS {
120+
func NewTarFS(file string) *tarFS {
118121
return &tarFS{
119-
Prefix: prefix,
120-
File: file,
122+
File: file,
121123
}
122124
}
123125

124126
func (t *tarFS) get(file string, includeContent bool) (bool, []byte, error) {
125-
f, err := os.Open(t.File)
127+
var f fs.File
128+
var err error
129+
if t.Embed != nil {
130+
f, err = t.Embed.Open(t.File)
131+
} else {
132+
f, err = os.Open(t.File)
133+
}
126134
if err != nil {
127135
return false, nil, err
128136
}

middleware/static_serve.go

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,8 @@ type (
8585
}
8686
)
8787

88+
type EncodingFsSelector func(*elton.Context) (string, StaticFile)
89+
8890
const (
8991
// ErrStaticServeCategory static serve error category
9092
ErrStaticServeCategory = "elton-static-serve"
@@ -93,6 +95,8 @@ const (
9395
var (
9496
// ErrStaticServeNotAllowQueryString not all query string
9597
ErrStaticServeNotAllowQueryString = getStaticServeError("static serve not allow query string", http.StatusBadRequest)
98+
// ErrStaticServeFsIsNil static fs is nil
99+
ErrStaticServeFsIsNil = getStaticServeError("static fs is nil", http.StatusBadRequest)
96100
// ErrStaticServeNotFound static file not found
97101
ErrStaticServeNotFound = getStaticServeError("static file not found", http.StatusNotFound)
98102
// ErrStaticServeOutOfPath file out of path
@@ -149,7 +153,7 @@ func generateETag(buf []byte) string {
149153
return fmt.Sprintf(`"%x-%s"`, size, hash)
150154
}
151155

152-
// NewDefaultStaticServe returns a new default static server milldeware using FS
156+
// NewDefaultStaticServe returns a new default static server middleware using FS
153157
func NewDefaultStaticServe(config StaticServeConfig) elton.Handler {
154158
return NewStaticServe(&FS{}, config)
155159
}
@@ -158,10 +162,7 @@ func toSeconds(d time.Duration) string {
158162
return strconv.Itoa(int(d.Seconds()))
159163
}
160164

161-
// NewStaticServe returns a new static serve middleware, suggest to set the MaxAge and SMaxAge for cache control for better performance.
162-
// It will return an error if DenyDot is true and filename is start with '.'.
163-
// It will return an error if DenyQueryString is true and the querystring is not empty.
164-
func NewStaticServe(staticFile StaticFile, config StaticServeConfig) elton.Handler {
165+
func NewEncodingStaticServe(config StaticServeConfig, selector EncodingFsSelector) elton.Handler {
165166
cacheArr := []string{
166167
"public",
167168
}
@@ -221,6 +222,10 @@ func NewStaticServe(staticFile StaticFile, config StaticServeConfig) elton.Handl
221222
if config.DenyQueryString && url.RawQuery != "" {
222223
return ErrStaticServeNotAllowQueryString
223224
}
225+
encoding, staticFile := selector(c)
226+
if staticFile == nil {
227+
return ErrStaticServeFsIsNil
228+
}
224229
// 如果有配置目录的index文件
225230
if config.IndexFile != "" {
226231
fileInfo := staticFile.Stat(file)
@@ -284,11 +289,15 @@ func NewStaticServe(staticFile StaticFile, config StaticServeConfig) elton.Handl
284289
}
285290
fileBuf = buf
286291
}
292+
// set content encoding
293+
if encoding != "" {
294+
c.SetHeader(elton.HeaderContentEncoding, encoding)
295+
}
287296
for k, v := range config.Header {
288297
c.AddHeader(k, v)
289298
}
290-
// 未设置cache control
291-
// 或文件符合正则
299+
// not set cache control
300+
// or the file match no cache
292301
if cacheControl == "" ||
293302
(noCacheRegexp != nil && noCacheRegexp.MatchString(file)) {
294303
c.NoCache()
@@ -309,3 +318,12 @@ func NewStaticServe(staticFile StaticFile, config StaticServeConfig) elton.Handl
309318
return c.Next()
310319
}
311320
}
321+
322+
// NewStaticServe returns a new static serve middleware, suggest to set the MaxAge and SMaxAge for cache control for better performance.
323+
// It will return an error if DenyDot is true and filename is start with '.'.
324+
// It will return an error if DenyQueryString is true and the query string is not empty.
325+
func NewStaticServe(staticFile StaticFile, config StaticServeConfig) elton.Handler {
326+
return NewEncodingStaticServe(config, func(ctx *elton.Context) (string, StaticFile) {
327+
return "", staticFile
328+
})
329+
}

0 commit comments

Comments
 (0)