-
Notifications
You must be signed in to change notification settings - Fork 1
/
static.go
195 lines (161 loc) · 5.05 KB
/
static.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
package govalin
import (
"errors"
"fmt"
"io"
"io/fs"
"log/slog"
"net/http"
"os"
"path/filepath"
"strings"
)
// StaticConfig contains configuration for a static handler.
type StaticConfig struct {
hostPath string
spaMode bool
staticPath string
fsContent fs.FS
}
func newStaticConfig() *StaticConfig {
return &StaticConfig{
hostPath: "/",
spaMode: false,
staticPath: "static",
fsContent: nil,
}
}
func (config *StaticConfig) serveIndexFS(call *Call) {
// file does not exist, serve index.html
file, openIndexErr := config.fsContent.Open("index.html")
if openIndexErr != nil {
slog.Error(`Failed to open index.html in bundled embedded files.
This might be due to a misconfigured embedded bundle or simply
that the index.html files doesn't exist.`)
call.Error(openIndexErr)
return
}
index, readIndexErr := io.ReadAll(file)
if readIndexErr != nil {
slog.Error(`Failed to read index.html in bundled embedded files.
This might be due to a misconfigured embedded bundle or simply
that the index.html files doesn't exist.`)
call.Error(openIndexErr)
return
}
call.Status(http.StatusOK)
call.HTML(string(index))
}
func (config *StaticConfig) serveIndexStatic(call *Call) {
indexPath := filepath.Join(config.staticPath, "index.html")
if _, indexFileDoesNotExist := os.Stat(
indexPath,
); os.IsNotExist(indexFileDoesNotExist) {
slog.Error(fmt.Sprintf(`Failed to read the index.html file in the given static file folder.
Are you sure it exists on the given path: '%s'`, indexPath))
call.Error(indexFileDoesNotExist)
return
}
// file does not exist, serve index.html
call.Status(http.StatusOK)
http.ServeFile(*call.Raw.W, call.Raw.Req, filepath.Join(config.staticPath, "index.html"))
}
func (config *StaticConfig) handle(call *Call) {
isFS := config.fsContent != nil
isStatic := config.fsContent == nil
path := call.URL().Path
// remove host path
path = strings.TrimPrefix(path, config.hostPath)
if isStatic {
// prepend the path with the path to the static directory
path = filepath.Join(config.staticPath, path)
}
// check whether a file exists at the given path
var statErr error
if isFS {
_, statErr = fs.Stat(config.fsContent, strings.TrimPrefix(path, "/"))
} else {
_, statErr = os.Stat(path)
}
var pathErr *fs.PathError
isNotFoundError := errors.Is(statErr, fs.ErrNotExist) || errors.As(statErr, &pathErr)
// Serve index if:
// 1. If path is empty (slash root)
// 2. if SPA mode is enabled, and if the file doesn't exist
if path == "" || (config.spaMode && isNotFoundError) {
if isFS {
config.serveIndexFS(call)
} else {
config.serveIndexStatic(call)
}
return
}
switch {
case isNotFoundError:
call.Status(http.StatusNotFound)
case statErr != nil:
// if we got an error (that wasn't that the file doesn't exist) stating the
// file, return a 500 internal server error and stop
call.Status(http.StatusInternalServerError)
call.Error(statErr)
return
default:
call.Status(http.StatusOK)
}
// otherwise, use http.FileServer to serve the file system
var hostedFileSystem http.FileSystem
if isFS {
hostedFileSystem = http.FS(config.fsContent)
} else {
hostedFileSystem = http.Dir(config.staticPath)
}
http.StripPrefix(
config.hostPath,
http.FileServer(hostedFileSystem),
).ServeHTTP(*call.Raw.W, call.Raw.Req)
}
// HostPath sets the host path for the static handler. This is trimmed from the
// URL before serving the static files.
func (config *StaticConfig) HostPath(hostPath string) *StaticConfig {
config.hostPath = hostPath
return config
}
// WithStaticPath sets the path to a directory containing static files.
//
// The directory will be served at the given path, relative to where the server
// is started.
func (config *StaticConfig) WithStaticPath(staticPath string) *StaticConfig {
config.staticPath = staticPath
return config
}
// WithFS sets the bundled FS to serve static files from.
func (config *StaticConfig) WithFS(fsContent fs.FS) *StaticConfig {
config.fsContent = fsContent
return config
}
// EnableSPAMode enables SPA mode for the static handler.
//
// SPA mode will serve the index.html file for all requests that doesn't match
// a static file.
func (config *StaticConfig) EnableSPAMode(spaMode bool) *StaticConfig {
config.spaMode = spaMode
return config
}
// Add a Static endpoint
//
// Add a static endpoint which will serve static files from the given path or bundled FS.
func (server *App) Static(path string, staticHandlerFunc StaticHandlerFunc) *App {
// TODO: this doesn't feel right to override the path like this
normalizedPath := strings.TrimRight(path, "/*")
wildcardPath := normalizedPath + "/*"
staticGetHandler := func(call *Call) {
internalConfig := newStaticConfig()
internalConfig.HostPath(normalizedPath)
staticHandlerFunc(call, internalConfig)
internalConfig.handle(call)
}
// TODO: this should be handled by a single handler, not two
server.addMethod(http.MethodGet, server.currentFragment+normalizedPath+"/", staticGetHandler)
server.addMethod(http.MethodGet, server.currentFragment+wildcardPath, staticGetHandler)
return server
}