-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
properly intercept not found requests for single page app
When a request comes in for an unknown path looking for an HTML content type, we need to render the index.html page so that the single page app can do its thing.
- Loading branch information
Showing
2 changed files
with
77 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"net/http" | ||
"strings" | ||
) | ||
|
||
func intercept404(handler, on404 http.Handler) http.Handler { | ||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
hookedWriter := &spaResponseWriter{ResponseWriter: w} | ||
handler.ServeHTTP(hookedWriter, r) | ||
|
||
if hookedWriter.got404 { | ||
on404.ServeHTTP(w, r) | ||
} | ||
}) | ||
} | ||
|
||
func serveFileContents(file string, files http.FileSystem) http.HandlerFunc { | ||
return func(w http.ResponseWriter, r *http.Request) { | ||
// Restrict only to instances where the browser is looking for an HTML file | ||
if !strings.Contains(r.Header.Get("Accept"), "text/html") { | ||
w.WriteHeader(http.StatusNotFound) | ||
fmt.Fprint(w, "404 not found") | ||
|
||
return | ||
} | ||
|
||
// Open the file and return its contents using http.ServeContent | ||
index, err := files.Open(file) | ||
if err != nil { | ||
w.WriteHeader(http.StatusNotFound) | ||
fmt.Fprintf(w, "%s not found", file) | ||
|
||
return | ||
} | ||
|
||
fi, err := index.Stat() | ||
if err != nil { | ||
w.WriteHeader(http.StatusNotFound) | ||
fmt.Fprintf(w, "%s not found", file) | ||
|
||
return | ||
} | ||
|
||
w.Header().Set("Content-Type", "text/html; charset=utf-8") | ||
http.ServeContent(w, r, fi.Name(), fi.ModTime(), index) | ||
} | ||
} | ||
|
||
type spaResponseWriter struct { | ||
http.ResponseWriter | ||
got404 bool | ||
} | ||
|
||
func (srw *spaResponseWriter) WriteHeader(status int) { | ||
if status == http.StatusNotFound { | ||
// Don't actually write the 404 header, just set a flag. | ||
srw.got404 = true | ||
} else { | ||
srw.ResponseWriter.WriteHeader(status) | ||
} | ||
} | ||
|
||
func (hrw *spaResponseWriter) Write(p []byte) (int, error) { | ||
if hrw.got404 { | ||
// No-op, but pretend that we wrote len(p) bytes to the writer. | ||
return len(p), nil | ||
} | ||
|
||
return hrw.ResponseWriter.Write(p) | ||
} |