-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.go
206 lines (175 loc) · 5.53 KB
/
utils.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
package lib
import (
"bytes"
"errors"
"io"
"io/fs"
"net/http"
"os"
"path/filepath"
"runtime"
"strings"
"syscall"
)
type neuteredFileSystem struct {
fs http.FileSystem
}
func (nfs neuteredFileSystem) Open(path string) (http.File, error) {
f, err := nfs.fs.Open(path)
if err != nil {
return nil, err
}
s, err := f.Stat()
if err != nil {
return nil, err
}
if s.IsDir() {
index := filepath.Join(path, "index.html")
if _, err := nfs.fs.Open(index); err != nil {
closeErr := f.Close()
if closeErr != nil {
return nil, closeErr
}
return nil, err
}
}
return f, nil
}
// NotFoundRedirectRespWr response for neutered fs
type NotFoundRedirectRespWr struct {
http.ResponseWriter // We embed http.ResponseWriter
status int
}
func (w *NotFoundRedirectRespWr) WriteHeader(status int) {
w.status = status // Store the status for our own use
if status != http.StatusNotFound {
w.ResponseWriter.WriteHeader(status)
}
}
func (w *NotFoundRedirectRespWr) Write(p []byte) (int, error) {
if w.status != http.StatusNotFound {
return w.ResponseWriter.Write(p)
}
return len(p), nil // Lie that we successfully written it
}
// WrappedFileServer wraps your http.FileServer with neutered fs to remove dir browsing and redirects 404 to index.html
func WrappedFileServer(baseDir string) http.HandlerFunc {
h := http.FileServer(neuteredFileSystem{http.Dir(baseDir)})
return func(w http.ResponseWriter, r *http.Request) {
nfrw := &NotFoundRedirectRespWr{ResponseWriter: w}
h.ServeHTTP(nfrw, r)
if nfrw.status == 404 {
w.Header().Set("Content-Type", "text/html; charset=utf-8")
http.ServeFile(w, r, filepath.Join(baseDir, "index.html"))
}
}
}
// PipedFileServer wraps your http.FileServer with neutered fs to remove dir browsing and pipes 404 to next handler
func PipedFileServer(baseDir string, pipe http.HandlerFunc) http.HandlerFunc {
h := http.FileServer(neuteredFileSystem{http.Dir(baseDir)})
return func(w http.ResponseWriter, r *http.Request) {
nfrw := &NotFoundRedirectRespWr{ResponseWriter: w}
h.ServeHTTP(nfrw, r)
if nfrw.status == 404 {
pipe(w, r)
}
}
}
// PipedFileServerWithMiddleware wraps PipedFileServer with middleware
func PipedFileServerWithMiddleware(baseDir string, pipe http.HandlerFunc, middleware func(http.HandlerFunc) http.HandlerFunc) http.HandlerFunc {
return middleware(PipedFileServer(baseDir, pipe))
}
// CopyDir copies the content of src to dst. src should be a full path.
func CopyDir(dst, src string) error {
return filepath.Walk(src, func(path string, info fs.FileInfo, err error) error {
if err != nil {
return err
}
// copy to this path
outpath := filepath.Join(dst, strings.TrimPrefix(path, src))
if info.IsDir() {
_ = os.MkdirAll(outpath, info.Mode())
return nil // means recursive
}
// handle irregular files
if !info.Mode().IsRegular() {
switch info.Mode().Type() & os.ModeType {
case os.ModeSymlink:
link, err := os.Readlink(path)
if err != nil {
return err
}
return os.Symlink(link, outpath)
}
return nil
}
// copy contents of regular file efficiently
// open input
in, _ := os.Open(path)
if err != nil {
return err
}
defer func(in *os.File) {
_ = in.Close()
}(in)
// create output
fh, err := os.Create(outpath)
if err != nil {
return err
}
defer func(fh *os.File) {
_ = fh.Close()
}(fh)
// make it the same
_ = fh.Chmod(info.Mode())
// copy content
_, err = io.Copy(fh, in)
return err
})
}
// FileExists does?
func FileExists(filename string) bool {
_, err := os.Stat(filename)
return err == nil
}
// InjectVarsIntoIndex injects js/css import to index.html content, returns bool if injected into content
func InjectVarsIntoIndex(indexFile []byte, entryFileName, assetsDir, publicUrl string) ([]byte, bool) {
indexFileName := strings.TrimSuffix(filepath.Base(entryFileName), filepath.Ext(entryFileName))
publicUrl = strings.TrimSuffix(publicUrl, "/")
changed := false
//inject main js/css if not already in index.html
if !bytes.Contains(indexFile, []byte("/"+assetsDir+"/"+indexFileName+".css")) {
changed = true
indexFile = bytes.Replace(indexFile, []byte("</head>"), []byte("<link rel=\"preload\" href=\""+publicUrl+"/"+assetsDir+"/"+indexFileName+".css\" as=\"style\">\n<link rel=\"stylesheet\" href=\""+publicUrl+"/"+assetsDir+"/"+indexFileName+".css\">\n</head>"), 1)
}
if !bytes.Contains(indexFile, []byte("/"+assetsDir+"/"+indexFileName+".js")) {
changed = true
indexFile = bytes.Replace(indexFile, []byte("</body>"), []byte("<script type=\"module\" src=\""+publicUrl+"/"+assetsDir+"/"+indexFileName+".js\"></script>\n</body>"), 1)
indexFile = bytes.Replace(indexFile, []byte("</head>"), []byte("<link rel=\"modulepreload\" href=\""+publicUrl+"/"+assetsDir+"/"+indexFileName+".js\">\n</head>"), 1)
}
// replace %PUBLIC_URL%
if bytes.Contains(indexFile, []byte("%PUBLIC_URL%")) {
changed = true
indexFile = bytes.ReplaceAll(indexFile, []byte("%PUBLIC_URL%"), []byte(publicUrl))
}
return indexFile, changed
}
// IsErrorAddressAlreadyInUse checks if the error is bind: address already in use OR alternative
func IsErrorAddressAlreadyInUse(err error) bool {
var eOsSyscall *os.SyscallError
if !errors.As(err, &eOsSyscall) {
return false
}
var errErrno syscall.Errno // doesn't need a "*" (ptr) because it's already a ptr (uintptr)
if !errors.As(eOsSyscall, &errErrno) {
return false
}
if errors.Is(errErrno, syscall.EADDRINUSE) {
return true
}
const WSAEADDRINUSE = 10048
if runtime.GOOS == "windows" && errErrno == WSAEADDRINUSE {
return true
}
return false
}