@@ -23,6 +23,7 @@ import (
23
23
"flag"
24
24
"fmt"
25
25
"io"
26
+ "io/ioutil"
26
27
"log"
27
28
"math/big"
28
29
mrand "math/rand"
52
53
pushManifest = flag .String ("pushmanifest" , "push.json" , "File containing the push manifest" )
53
54
minDelay = flag .Int ("mindelay" , 0 , "Minimum delay before a request in answered in milliseconds (ignored without -maxdelay)" )
54
55
maxDelay = flag .Int ("maxdelay" , 0 , "Maximum delay before a request in answered in milliseconds" )
56
+ spa = flag .String ("spa" , "" , "Page to serve instead of 404" )
55
57
)
56
58
57
59
func init () {
@@ -96,11 +98,25 @@ func main() {
96
98
}
97
99
time .Sleep (time .Duration (delay ) * time .Millisecond )
98
100
99
- defer fs .ServeHTTP (w , r )
100
- if * http1 {
101
- return
101
+ if * spa != "" {
102
+ path := r .URL .Path
103
+ if _ , err := os .Stat ("." + path ); err == nil {
104
+ fs .ServeHTTP (w , r )
105
+ } else {
106
+ spaContents , err := readSPAFile (* spa )
107
+ if err != nil {
108
+ http .Error (w , fmt .Sprintf ("Could not read SPA file: %s" , err ), http .StatusInternalServerError )
109
+ return
110
+ }
111
+ w .Write (spaContents )
112
+ }
113
+ } else {
114
+ fs .ServeHTTP (w , r )
115
+ }
116
+
117
+ if ! * http1 {
118
+ pushResources (w , r )
102
119
}
103
- pushResources (w , r )
104
120
})
105
121
106
122
if err := configureTLS (server ); err != nil {
@@ -150,6 +166,15 @@ func pushResources(w http.ResponseWriter, r *http.Request) {
150
166
}
151
167
}
152
168
169
+ func readSPAFile (path string ) ([]byte , error ) {
170
+ f , err := os .Open (path )
171
+ if err != nil {
172
+ return nil , err
173
+ }
174
+ defer f .Close ()
175
+ return ioutil .ReadAll (f )
176
+ }
177
+
153
178
type GzipResponseWriter struct {
154
179
io.WriteCloser
155
180
http.ResponseWriter
0 commit comments