Commit 244e1b98 authored by Jacob Vosmaer's avatar Jacob Vosmaer

Create internal/errorpage package

parent 14cdbd60
package main
import (
"./internal/helper"
"io/ioutil"
"net/http"
"path/filepath"
......@@ -15,7 +16,7 @@ func handleDeployPage(documentRoot *string, handler http.Handler) http.HandlerFu
return
}
setNoCacheHeaders(w.Header())
helper.SetNoCacheHeaders(w.Header())
w.Header().Set("Content-Type", "text/html; charset=utf-8")
w.WriteHeader(http.StatusOK)
w.Write(data)
......
package main
import (
"./internal/errorpage"
"fmt"
"io/ioutil"
"net/http"
......@@ -25,7 +26,7 @@ func TestIfErrorPageIsPresented(t *testing.T) {
w.WriteHeader(404)
fmt.Fprint(w, "Not Found")
})
handleRailsError(&dir, h)(w, nil)
errorpage.Inject(dir, h)(w, nil)
w.Flush()
assertResponseCode(t, w, 404)
......@@ -45,7 +46,7 @@ func TestIfErrorPassedIfNoErrorPageIsFound(t *testing.T) {
w.WriteHeader(404)
fmt.Fprint(w, errorResponse)
})
handleRailsError(&dir, h)(w, nil)
errorpage.Inject(dir, h)(w, nil)
w.Flush()
assertResponseCode(t, w, 404)
......
......@@ -54,12 +54,6 @@ func cleanUpProcessGroup(cmd *exec.Cmd) {
cmd.Wait()
}
func setNoCacheHeaders(header http.Header) {
header.Set("Cache-Control", "no-cache, no-store, max-age=0, must-revalidate")
header.Set("Pragma", "no-cache")
header.Set("Expires", "Fri, 01 Jan 1990 00:00:00 GMT")
}
// Borrowed from: net/http/server.go
// Return the canonical path for p, eliminating . and .. elements.
func cleanURIPath(p string) string {
......
package main
package errorpage
import (
"../helper"
"fmt"
"io/ioutil"
"log"
......@@ -12,7 +13,7 @@ type errorPageResponseWriter struct {
rw http.ResponseWriter
status int
hijacked bool
path *string
path string
}
func (s *errorPageResponseWriter) Header() http.Header {
......@@ -37,14 +38,14 @@ func (s *errorPageResponseWriter) WriteHeader(status int) {
s.status = status
if 400 <= s.status && s.status <= 599 {
errorPageFile := filepath.Join(*s.path, fmt.Sprintf("%d.html", s.status))
errorPageFile := filepath.Join(s.path, fmt.Sprintf("%d.html", s.status))
// check if custom error page exists, serve this page instead
if data, err := ioutil.ReadFile(errorPageFile); err == nil {
s.hijacked = true
log.Printf("ErrorPage: serving predefined error page: %d", s.status)
setNoCacheHeaders(s.rw.Header())
helper.SetNoCacheHeaders(s.rw.Header())
s.rw.Header().Set("Content-Type", "text/html; charset=utf-8")
s.rw.WriteHeader(s.status)
s.rw.Write(data)
......@@ -59,7 +60,7 @@ func (s *errorPageResponseWriter) Flush() {
s.WriteHeader(http.StatusOK)
}
func handleRailsError(documentRoot *string, handler http.Handler) http.HandlerFunc {
func Inject(documentRoot string, handler http.Handler) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
rw := errorPageResponseWriter{
rw: w,
......
......@@ -16,6 +16,12 @@ func LogError(err error) {
log.Printf("error: %v", err)
}
func SetNoCacheHeaders(header http.Header) {
header.Set("Cache-Control", "no-cache, no-store, max-age=0, must-revalidate")
header.Set("Pragma", "no-cache")
header.Set("Expires", "Fri, 01 Jan 1990 00:00:00 GMT")
}
func OpenFile(path string) (file *os.File, fi os.FileInfo, err error) {
file, err = os.Open(path)
if err != nil {
......
......@@ -14,6 +14,7 @@ In this file we start the web server and hand off to the upstream type.
package main
import (
"./internal/errorpage"
"flag"
"fmt"
"log"
......@@ -95,7 +96,7 @@ func compileRoutes(u *upstream) {
u.handleServeFile(documentRoot, CacheExpireMax,
handleDevelopmentMode(developmentMode,
handleDeployPage(documentRoot,
handleRailsError(documentRoot,
errorpage.Inject(*documentRoot,
proxy,
),
),
......@@ -107,7 +108,7 @@ func compileRoutes(u *upstream) {
httpRoute{"", nil,
u.handleServeFile(documentRoot, CacheDisabled,
handleDeployPage(documentRoot,
handleRailsError(documentRoot,
errorpage.Inject(*documentRoot,
proxy,
),
),
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment