Skip to content

Commit

Permalink
fix: use custom FS to instead one-time Cache-Control
Browse files Browse the repository at this point in the history
  • Loading branch information
orca-zhang committed Jul 17, 2024
1 parent c31e88c commit 02cc45d
Showing 1 changed file with 75 additions and 26 deletions.
101 changes: 75 additions & 26 deletions route/static_route.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package route

import (
"fmt"
"io"
"io/fs"
"net/http"
"os"
"path/filepath"
"time"

"github.com/IceWhaleTech/CasaOS-Gateway/service"
Expand All @@ -17,41 +19,88 @@ type StaticRoute struct {

var startTime = time.Now()

var RouteCache = make(map[string]string)

func NewStaticRoute(state *service.State) *StaticRoute {
return &StaticRoute{
state: state,
}
}

type CustomFS struct {
base fs.FS
}

func NewCustomFS(prefix string) *CustomFS {
return &CustomFS{
base: fs.FS(os.DirFS(prefix)),
}
}

func (c *CustomFS) Open(name string) (fs.File, error) {
file, err := c.base.Open(name)
if err != nil {
return nil, err
}
return &CustomFile{
File: file,
}, nil
}

func (c *CustomFS) Stat(name string) (fs.FileInfo, error) {
file, err := c.base.Open(name)
if err != nil {
return nil, err
}
info, err := file.Stat()
if err != nil {
return nil, err
}
return &CustomFileInfo{
FileInfo: info,
}, nil
}

type CustomFile struct {
fs.File
}

func (c *CustomFile) Stat() (fs.FileInfo, error) {
info, err := c.File.Stat()
if err != nil {
return nil, err
}
return &CustomFileInfo{
FileInfo: info,
}, nil
}

func (c *CustomFile) Read(p []byte) (int, error) {
if seeker, ok := c.File.(io.Reader); ok {
return seeker.Read(p)
}
return 0, fmt.Errorf("file does not implement io.Reader")
}

func (c *CustomFile) Seek(offset int64, whence int) (int64, error) {
if seeker, ok := c.File.(io.Seeker); ok {
return seeker.Seek(offset, whence)
}
return 0, fmt.Errorf("file does not implement io.Seeker")
}

type CustomFileInfo struct {
fs.FileInfo
}

func (c *CustomFileInfo) ModTime() time.Time {
return startTime
}

func (s *StaticRoute) GetRoute() http.Handler {
e := echo.New()

e.Use(echo_middleware.Gzip())

e.Use(func(next echo.HandlerFunc) echo.HandlerFunc {
return func(ctx echo.Context) error {
if _, ok := RouteCache[ctx.Request().URL.Path]; !ok {
ctx.Response().Writer.Header().Set("Cache-Control", "no-cache, no-store, must-revalidate,proxy-revalidate, max-age=0")
RouteCache[ctx.Request().URL.Path] = ctx.Request().URL.Path
}
return next(ctx)
}
})

// serve /index.html to sovle 304 cache problem by 'If-Modified-Since: Wed, 21 Oct 2015 07:28:00 GMT' from web browser
e.GET("/", func(ctx echo.Context) error {
f, err := os.Open(filepath.Join(s.state.GetWWWPath(), "index.html"))
if err != nil {
return err
}
defer f.Close()
http.ServeContent(ctx.Response(), ctx.Request(), "index.html", startTime, f)
return nil
})

e.Static("/", s.state.GetWWWPath())

// sovle 304 cache problem by 'If-Modified-Since: Wed, 21 Oct 2015 07:28:00 GMT' from web browser
e.StaticFS("/", NewCustomFS(s.state.GetWWWPath()))
return e
}

0 comments on commit 02cc45d

Please sign in to comment.