-
Notifications
You must be signed in to change notification settings - Fork 4
/
customResponseWriter.go
45 lines (38 loc) · 1 KB
/
customResponseWriter.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
package main
import (
"bytes"
"io/ioutil"
"log"
"net/http"
"time"
)
//CustomResponseWriter ...
type CustomResponseWriter struct {
http.ResponseWriter
status int
}
//WriteHeader ...
func (w *CustomResponseWriter) WriteHeader(status int) {
w.status = status
if status != http.StatusNotFound {
w.ResponseWriter.WriteHeader(status)
}
}
func (w *CustomResponseWriter) Write(data []byte) (int, error) {
if w.status != http.StatusNotFound {
return w.ResponseWriter.Write(data)
}
return len(data), nil
}
func getFileHandler(h http.Handler) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
customResponseWriter := &CustomResponseWriter{ResponseWriter: w}
h.ServeHTTP(customResponseWriter, r)
if customResponseWriter.status == 404 {
log.Println("[REDIRECT] - To index.html, from:", r.RequestURI)
data, _ := ioutil.ReadFile("dist/index.html")
w.Header().Set("Content-Type", "text/html")
http.ServeContent(w, r, "index.html", time.Now(), bytes.NewReader(data))
}
}
}