Commit 362c1385 authored by Kamil Trzcinski's avatar Kamil Trzcinski

Added development mode and serve assets with cache enabled

parent ac409d67
package main
import "net/http"
func handleDevelopmentMode(developmentMode *bool, handler serviceHandleFunc) serviceHandleFunc {
return func(w http.ResponseWriter, r *gitRequest) {
if !*developmentMode {
http.NotFound(w, r.Request)
return
}
handler(w, r)
}
}
package main
import (
"net/http"
"net/http/httptest"
"testing"
)
func TestDevelopmentModeEnabled(t *testing.T) {
developmentMode := true
r, _ := http.NewRequest("GET", "/something", nil)
w := httptest.NewRecorder()
executed := false
handleDevelopmentMode(&developmentMode, func(w http.ResponseWriter, r *gitRequest) {
executed = true
})(w, &gitRequest{Request: r})
if !executed {
t.Error("The handler should get executed")
}
}
func TestDevelopmentModeDisabled(t *testing.T) {
developmentMode := false
r, _ := http.NewRequest("GET", "/something", nil)
w := httptest.NewRecorder()
executed := false
handleDevelopmentMode(&developmentMode, func(w http.ResponseWriter, r *gitRequest) {
executed = true
})(w, &gitRequest{Request: r})
if executed {
t.Error("The handler should not get executed")
}
assertResponseCode(t, w, 404)
}
...@@ -16,3 +16,9 @@ func assertResponseBody(t *testing.T, response *httptest.ResponseRecorder, expec ...@@ -16,3 +16,9 @@ func assertResponseBody(t *testing.T, response *httptest.ResponseRecorder, expec
t.Fatalf("for HTTP request expected to receive %q, got %q instead as body", expectedBody, response.Body.String()) t.Fatalf("for HTTP request expected to receive %q, got %q instead as body", expectedBody, response.Body.String())
} }
} }
func assertResponseHeader(t *testing.T, response *httptest.ResponseRecorder, header string, expectedValue string) {
if response.Header().Get(header) != expectedValue {
t.Fatalf("for HTTP request expected to receive the header %q with %q, got %q", header, expectedValue, response.Header().Get(header))
}
}
...@@ -39,6 +39,7 @@ var pprofListenAddr = flag.String("pprofListenAddr", "", "pprof listening addres ...@@ -39,6 +39,7 @@ var pprofListenAddr = flag.String("pprofListenAddr", "", "pprof listening addres
var relativeURLRoot = flag.String("relativeURLRoot", "/", "GitLab relative URL root") var relativeURLRoot = flag.String("relativeURLRoot", "/", "GitLab relative URL root")
var documentRoot = flag.String("documentRoot", "public", "Path to static files content") var documentRoot = flag.String("documentRoot", "public", "Path to static files content")
var responseHeadersTimeout = flag.Duration("proxyHeadersTimeout", time.Minute, "How long to wait for response headers when proxying the request") var responseHeadersTimeout = flag.Duration("proxyHeadersTimeout", time.Minute, "How long to wait for response headers when proxying the request")
var developmentMode = flag.Bool("developmentMode", false, "Allow to serve assets from Rails app")
type httpRoute struct { type httpRoute struct {
method string method string
...@@ -85,6 +86,19 @@ var httpRoutes = [...]httpRoute{ ...@@ -85,6 +86,19 @@ var httpRoutes = [...]httpRoute{
httpRoute{"", regexp.MustCompile(apiPattern), proxyRequest}, httpRoute{"", regexp.MustCompile(apiPattern), proxyRequest},
httpRoute{"", regexp.MustCompile(ciAPIPattern), proxyRequest}, httpRoute{"", regexp.MustCompile(ciAPIPattern), proxyRequest},
// Serve assets
httpRoute{"", regexp.MustCompile(`^/assets/`),
handleServeFile(documentRoot, CacheExpireMax,
handleDevelopmentMode(developmentMode,
handleDeployPage(documentRoot,
handleRailsError(documentRoot,
proxyRequest,
),
),
),
),
},
// Serve static files or forward the requests // Serve static files or forward the requests
httpRoute{"", nil, httpRoute{"", nil,
handleServeFile(documentRoot, CacheDisabled, handleServeFile(documentRoot, CacheDisabled,
......
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