Skip to content

Commit

Permalink
Merge pull request #2 from cloverstd/master
Browse files Browse the repository at this point in the history
Unwrap dist wrapper
  • Loading branch information
wangdashuaihenshuai authored Dec 26, 2023
2 parents 7be14b9 + 8a27740 commit 4495fb3
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 98 deletions.
3 changes: 1 addition & 2 deletions config.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
{
"port": "6752",
"publicPath": ""
"port": "6752"
}
2 changes: 0 additions & 2 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
16 changes: 9 additions & 7 deletions container/container.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package container

import (
"io/fs"
"log"
"net/http"

Expand All @@ -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"
)

Expand Down Expand Up @@ -51,19 +50,22 @@ 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 {
e.GET(
"/*",
echo.WrapHandler(
http.FileServer(http.FS(ff))),
middleware.Rewrite(map[string]string{"/*": "/dist/$1"}),
selfMiddleware.Cache(),
)
}
Expand Down
37 changes: 0 additions & 37 deletions static/file_wrapper.go

This file was deleted.

55 changes: 5 additions & 50 deletions static/public.go
Original file line number Diff line number Diff line change
@@ -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)
Expand Down

0 comments on commit 4495fb3

Please sign in to comment.