From 8a27740f78d234e499017a40b8493219d25b7f71 Mon Sep 17 00:00:00 2001 From: "yi.yang" Date: Fri, 22 Dec 2023 18:07:08 +0800 Subject: [PATCH] Unwrap dist wrapper --- config.json | 3 +-- config/config.go | 2 -- container/container.go | 16 ++++++------ static/file_wrapper.go | 37 ---------------------------- static/public.go | 55 ++++-------------------------------------- 5 files changed, 15 insertions(+), 98 deletions(-) delete mode 100644 static/file_wrapper.go diff --git a/config.json b/config.json index 365e7bf..53628fc 100644 --- a/config.json +++ b/config.json @@ -1,4 +1,3 @@ { - "port": "6752", - "publicPath": "" + "port": "6752" } \ No newline at end of file diff --git a/config/config.go b/config/config.go index be922a1..129aeda 100644 --- a/config/config.go +++ b/config/config.go @@ -15,8 +15,6 @@ type Config struct { Port string `json:"port"` RpcAddress []*Address `json:"rpcAddress"` CorsConfig *CorsConfig `json:"corsConfig"` - PublicPath string `json:"publicPath"` - BaseAPIURL string `json:"baseAPIURL"` } type Address struct { diff --git a/container/container.go b/container/container.go index 6c786d6..1cc7c67 100644 --- a/container/container.go +++ b/container/container.go @@ -1,6 +1,7 @@ package container import ( + "io/fs" "log" "net/http" @@ -9,8 +10,6 @@ import ( "github.com/HuolalaTech/page-spy-api/serve/socket" "github.com/HuolalaTech/page-spy-api/static" "github.com/labstack/echo/v4" - "github.com/labstack/echo/v4/middleware" - "go.uber.org/dig" ) @@ -51,11 +50,15 @@ func initContainer() (*dig.Container, error) { return nil }) + dist, err := fs.Sub(staticConfig.Files, "dist") + if err != nil { + // it will never be here + panic(err) + } + ff := static.NewFallbackFS( - staticConfig.Files, - staticConfig.DirName+"/index.html", - config.PublicPath, - config.BaseAPIURL, + dist, + "index.html", ) if staticConfig != nil { @@ -63,7 +66,6 @@ func initContainer() (*dig.Container, error) { "/*", echo.WrapHandler( http.FileServer(http.FS(ff))), - middleware.Rewrite(map[string]string{"/*": "/dist/$1"}), selfMiddleware.Cache(), ) } diff --git a/static/file_wrapper.go b/static/file_wrapper.go deleted file mode 100644 index 4832900..0000000 --- a/static/file_wrapper.go +++ /dev/null @@ -1,37 +0,0 @@ -package static - -import ( - "io" - "io/fs" -) - -var ( - _ fs.File = (*fileWrapper)(nil) - _ fs.FileInfo = (*fileInfoWrapper)(nil) -) - -type fileWrapper struct { - buf io.Reader - fi fs.FileInfo -} - -func (f *fileWrapper) Stat() (fs.FileInfo, error) { - return f.fi, nil -} - -func (f *fileWrapper) Read(bytes []byte) (int, error) { - return f.buf.Read(bytes) -} - -func (f *fileWrapper) Close() error { - return nil -} - -type fileInfoWrapper struct { - fs.FileInfo - size int -} - -func (f fileInfoWrapper) Size() int64 { - return int64(f.size) -} diff --git a/static/public.go b/static/public.go index a47239a..da3f4d2 100644 --- a/static/public.go +++ b/static/public.go @@ -1,68 +1,23 @@ package static import ( - "bytes" - "io" "io/fs" "os" ) type FallbackFS struct { - FS fs.FS - Fallback string - PublicPath string - BaseAPIURL string + FS fs.FS + Fallback string } -var ( - publicPathPlaceholder = []byte("__PAGE_SPY_PUBLIC_PATH__") - baseAPIURLPlaceholder = []byte("__PAGE_SPY_BASE_API_URL__") -) - -func NewFallbackFS(fs fs.FS, fallback string, publicPath, baseAPIURL string) *FallbackFS { +func NewFallbackFS(fs fs.FS, fallback string) *FallbackFS { return &FallbackFS{ - FS: fs, - Fallback: fallback, - PublicPath: publicPath, - BaseAPIURL: baseAPIURL, + FS: fs, + Fallback: fallback, } } func (f *FallbackFS) Open(name string) (fs.File, error) { - file, err := f.open(name) - if err != nil { - return nil, err - } - fi, err := file.Stat() - if err != nil { - return nil, err - } - - if fi.IsDir() { - return file, nil - } - - content, err := io.ReadAll(file) - if err != nil { - return nil, err - } - err = file.Close() - if err != nil { - return nil, err - } - - content = bytes.ReplaceAll(content, publicPathPlaceholder, []byte(f.PublicPath)) - content = bytes.ReplaceAll(content, baseAPIURLPlaceholder, []byte(f.BaseAPIURL)) - return &fileWrapper{ - buf: bytes.NewReader(content), - fi: fileInfoWrapper{ - FileInfo: fi, - size: len(content), - }, - }, nil -} - -func (f *FallbackFS) open(name string) (fs.File, error) { file, err := f.FS.Open(name) if os.IsNotExist(err) { return f.FS.Open(f.Fallback)