diff options
author | Nick Thomas <nick@gitlab.com> | 2017-11-23 20:05:41 +0300 |
---|---|---|
committer | Nick Thomas <nick@gitlab.com> | 2017-11-23 20:05:41 +0300 |
commit | a57de7adc1288ceafb3e6dcd50a3f0be1cec0028 (patch) | |
tree | 0511d284185f68eef3cd7fa3eb2121d20c99ffb5 | |
parent | 15c938cafbd95064bbc4be34cd72091f9a61edaa (diff) | |
parent | 04b1d52ca05f268a63a72181600aa09251b6297a (diff) |
Merge branch '503-until-pages-are-loaded' into 'master'
Return 503 until pages are loaded for the first time
See merge request gitlab-org/gitlab-pages!49
-rw-r--r-- | acceptance_test.go | 22 | ||||
-rw-r--r-- | app.go | 19 | ||||
-rw-r--r-- | internal/httperrors/httperrors.go | 14 | ||||
-rw-r--r-- | shared/invalid-pages/.update/.gitkeep | 0 |
4 files changed, 54 insertions, 1 deletions
diff --git a/acceptance_test.go b/acceptance_test.go index f4e7e007..058b4994 100644 --- a/acceptance_test.go +++ b/acceptance_test.go @@ -212,6 +212,28 @@ func TestStatusPage(t *testing.T) { assert.Equal(t, http.StatusOK, rsp.StatusCode) } +func TestStatusNotYetReady(t *testing.T) { + skipUnlessEnabled(t) + teardown := RunPagesProcess(t, *pagesBinary, listeners, "", "-redirect-http=false", "-pages-status=/@statuscheck", "-pages-root=shared/invalid-pages") + defer teardown() + + rsp, err := GetPageFromListener(t, httpListener, "group.gitlab-example.com", "@statuscheck") + assert.NoError(t, err) + defer rsp.Body.Close() + assert.Equal(t, http.StatusServiceUnavailable, rsp.StatusCode) +} + +func TestPageNotAvailableIfNotLoaded(t *testing.T) { + skipUnlessEnabled(t) + teardown := RunPagesProcess(t, *pagesBinary, listeners, "", "-redirect-http=false", "-pages-root=shared/invalid-pages") + defer teardown() + + rsp, err := GetPageFromListener(t, httpListener, "group.gitlab-example.com", "index.html") + assert.NoError(t, err) + defer rsp.Body.Close() + assert.Equal(t, http.StatusServiceUnavailable, rsp.StatusCode) +} + func TestArtifactProxyRequest(t *testing.T) { skipUnlessEnabled(t) content := "<!DOCTYPE html><html><head><title>Title of the document</title></head><body></body></html>" @@ -33,6 +33,10 @@ type theApp struct { Artifact *artifact.Artifact } +func (a *theApp) isReady() bool { + return a.domains != nil +} + func (a *theApp) domain(host string) *domain { host = strings.ToLower(host) a.lock.RLock() @@ -54,6 +58,14 @@ func (a *theApp) ServeTLS(ch *tls.ClientHelloInfo) (*tls.Certificate, error) { return nil, nil } +func (a *theApp) healthCheck(w http.ResponseWriter, r *http.Request, https bool) { + if a.isReady() { + w.Write([]byte("success")) + } else { + http.Error(w, "not yet ready", http.StatusServiceUnavailable) + } +} + func (a *theApp) serveContent(ww http.ResponseWriter, r *http.Request, https bool) { w := newLoggingResponseWriter(ww) defer w.Log(r) @@ -63,7 +75,7 @@ func (a *theApp) serveContent(ww http.ResponseWriter, r *http.Request, https boo // short circuit content serving to check for a status page if r.RequestURI == a.appConfig.StatusPath { - w.Write([]byte("success")) + a.healthCheck(&w, r, https) return } @@ -89,6 +101,11 @@ func (a *theApp) serveContent(ww http.ResponseWriter, r *http.Request, https boo return } + if !a.isReady() { + httperrors.Serve503(&w) + return + } + domain := a.domain(host) if domain == nil { httperrors.Serve404(&w) diff --git a/internal/httperrors/httperrors.go b/internal/httperrors/httperrors.go index 82d43fc5..92413e07 100644 --- a/internal/httperrors/httperrors.go +++ b/internal/httperrors/httperrors.go @@ -40,6 +40,15 @@ var ( `<p>Try refreshing the page, or going back and attempting the action again.</p> <p>Please contact your GitLab administrator if this problem persists.</p>`, } + + content503 = content{ + http.StatusServiceUnavailable, + "Service Unavailable (503)", + "503", + "Whoops, something went wrong on our end.", + `<p>Try refreshing the page, or going back and attempting the action again.</p> + <p>Please contact your GitLab administrator if this problem persists.</p>`, + } ) const predefinedErrorPage = ` @@ -160,3 +169,8 @@ func Serve500(w http.ResponseWriter) { func Serve502(w http.ResponseWriter) { serveErrorPage(w, content502) } + +// Serve503 returns a 503 error response / HTML page to the http.ResponseWriter +func Serve503(w http.ResponseWriter) { + serveErrorPage(w, content503) +} diff --git a/shared/invalid-pages/.update/.gitkeep b/shared/invalid-pages/.update/.gitkeep new file mode 100644 index 00000000..e69de29b --- /dev/null +++ b/shared/invalid-pages/.update/.gitkeep |