Skip to content

Commit 4495fb3

Browse files
Merge pull request #2 from cloverstd/master
Unwrap dist wrapper
2 parents 7be14b9 + 8a27740 commit 4495fb3

File tree

5 files changed

+15
-98
lines changed

5 files changed

+15
-98
lines changed

config.json

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
11
{
2-
"port": "6752",
3-
"publicPath": ""
2+
"port": "6752"
43
}

config/config.go

-2
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@ type Config struct {
1515
Port string `json:"port"`
1616
RpcAddress []*Address `json:"rpcAddress"`
1717
CorsConfig *CorsConfig `json:"corsConfig"`
18-
PublicPath string `json:"publicPath"`
19-
BaseAPIURL string `json:"baseAPIURL"`
2018
}
2119

2220
type Address struct {

container/container.go

+9-7
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package container
22

33
import (
4+
"io/fs"
45
"log"
56
"net/http"
67

@@ -9,8 +10,6 @@ import (
910
"github.com/HuolalaTech/page-spy-api/serve/socket"
1011
"github.com/HuolalaTech/page-spy-api/static"
1112
"github.com/labstack/echo/v4"
12-
"github.com/labstack/echo/v4/middleware"
13-
1413
"go.uber.org/dig"
1514
)
1615

@@ -51,19 +50,22 @@ func initContainer() (*dig.Container, error) {
5150
return nil
5251
})
5352

53+
dist, err := fs.Sub(staticConfig.Files, "dist")
54+
if err != nil {
55+
// it will never be here
56+
panic(err)
57+
}
58+
5459
ff := static.NewFallbackFS(
55-
staticConfig.Files,
56-
staticConfig.DirName+"/index.html",
57-
config.PublicPath,
58-
config.BaseAPIURL,
60+
dist,
61+
"index.html",
5962
)
6063

6164
if staticConfig != nil {
6265
e.GET(
6366
"/*",
6467
echo.WrapHandler(
6568
http.FileServer(http.FS(ff))),
66-
middleware.Rewrite(map[string]string{"/*": "/dist/$1"}),
6769
selfMiddleware.Cache(),
6870
)
6971
}

static/file_wrapper.go

-37
This file was deleted.

static/public.go

+5-50
Original file line numberDiff line numberDiff line change
@@ -1,68 +1,23 @@
11
package static
22

33
import (
4-
"bytes"
5-
"io"
64
"io/fs"
75
"os"
86
)
97

108
type FallbackFS struct {
11-
FS fs.FS
12-
Fallback string
13-
PublicPath string
14-
BaseAPIURL string
9+
FS fs.FS
10+
Fallback string
1511
}
1612

17-
var (
18-
publicPathPlaceholder = []byte("__PAGE_SPY_PUBLIC_PATH__")
19-
baseAPIURLPlaceholder = []byte("__PAGE_SPY_BASE_API_URL__")
20-
)
21-
22-
func NewFallbackFS(fs fs.FS, fallback string, publicPath, baseAPIURL string) *FallbackFS {
13+
func NewFallbackFS(fs fs.FS, fallback string) *FallbackFS {
2314
return &FallbackFS{
24-
FS: fs,
25-
Fallback: fallback,
26-
PublicPath: publicPath,
27-
BaseAPIURL: baseAPIURL,
15+
FS: fs,
16+
Fallback: fallback,
2817
}
2918
}
3019

3120
func (f *FallbackFS) Open(name string) (fs.File, error) {
32-
file, err := f.open(name)
33-
if err != nil {
34-
return nil, err
35-
}
36-
fi, err := file.Stat()
37-
if err != nil {
38-
return nil, err
39-
}
40-
41-
if fi.IsDir() {
42-
return file, nil
43-
}
44-
45-
content, err := io.ReadAll(file)
46-
if err != nil {
47-
return nil, err
48-
}
49-
err = file.Close()
50-
if err != nil {
51-
return nil, err
52-
}
53-
54-
content = bytes.ReplaceAll(content, publicPathPlaceholder, []byte(f.PublicPath))
55-
content = bytes.ReplaceAll(content, baseAPIURLPlaceholder, []byte(f.BaseAPIURL))
56-
return &fileWrapper{
57-
buf: bytes.NewReader(content),
58-
fi: fileInfoWrapper{
59-
FileInfo: fi,
60-
size: len(content),
61-
},
62-
}, nil
63-
}
64-
65-
func (f *FallbackFS) open(name string) (fs.File, error) {
6621
file, err := f.FS.Open(name)
6722
if os.IsNotExist(err) {
6823
return f.FS.Open(f.Fallback)

0 commit comments

Comments
 (0)