Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unwrap dist wrapper #2

Merged
merged 1 commit into from
Dec 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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