Commit bba7d53e authored by Nick Thomas's avatar Nick Thomas

Merge branch 'webpack-dev-server' into 'master'

Add webpack dev-server support

Closes #104

See merge request !121
parents 815be1cc ff64d30c
......@@ -10,6 +10,7 @@ type Config struct {
Version string
DocumentRoot string
DevelopmentMode bool
WebpackAddr string
Socket string
ProxyHeadersTimeout time.Duration
APILimit uint
......
......@@ -19,6 +19,7 @@ import (
"gitlab.com/gitlab-org/gitlab-workhorse/internal/staticpages"
"gitlab.com/gitlab-org/gitlab-workhorse/internal/terminal"
"gitlab.com/gitlab-org/gitlab-workhorse/internal/upload"
"gitlab.com/gitlab-org/gitlab-workhorse/internal/webpack"
)
type matcherFunc func(*http.Request) bool
......@@ -139,6 +140,18 @@ func (u *Upstream) configureRoutes() {
route("", apiPattern, proxy),
route("", ciAPIPattern, proxy),
// In development mode, proxy /assets/webpack requests to webpack
// dev-server, otherwise serve static files from disk.
route(
"", `^/assets/webpack/`,
webpack.DevServer(u.DevelopmentMode, u.WebpackAddr,
static.ServeExisting(
u.URLPrefix,
staticpages.CacheExpireMax,
nil,
)),
),
// Serve assets
route(
"", `^/assets/`,
......
package webpack
import (
"fmt"
"net/http"
"net/http/httputil"
"net/url"
"gitlab.com/gitlab-org/gitlab-workhorse/internal/helper"
)
func DevServer(enabled bool, address string, fallbackHandler http.Handler) http.Handler {
if !enabled {
return fallbackHandler
}
u, err := buildURL(address)
if err != nil {
panic(err)
}
return httputil.NewSingleHostReverseProxy(u)
}
func buildURL(address string) (*url.URL, error) {
u := helper.URLMustParse(address)
if u == nil {
return nil, fmt.Errorf("failed to parse URL in %q", address)
}
// Hope to support unix:// in the future
if u.Scheme != "tcp" {
return nil, fmt.Errorf("invalid scheme: %v", u)
}
u.Scheme = "http"
return u, nil
}
package webpack
import (
"testing"
)
func TestBuildURL(t *testing.T) {
examples := []struct {
input string
ok bool
}{
{"", false},
{"localhost:5000", false},
{"tcp://localhost:5000", true},
}
for _, ex := range examples {
u, err := buildURL(ex.input)
if ex.ok {
if err != nil {
t.Errorf("example %v: expected no error, got %v", ex, err)
}
expectedScheme := "http"
if u.Scheme != expectedScheme {
t.Errorf("example %v: expected scheme %q, got %q", ex, expectedScheme, u.Scheme)
}
} else {
if err == nil {
t.Errorf("example %v: expected error, got none", ex)
}
}
}
}
......@@ -51,6 +51,7 @@ var apiQueueLimit = flag.Uint("apiQueueLimit", 0, "Number of API requests allowe
var apiQueueTimeout = flag.Duration("apiQueueDuration", queueing.DefaultTimeout, "Maximum queueing duration of requests")
var logFile = flag.String("logFile", "", "Log file to be used")
var prometheusListenAddr = flag.String("prometheusListenAddr", "", "Prometheus listening address, e.g. ':9100'")
var webpackAddr = flag.String("webpackAddr", "", "(development mode only) Network address of Webpack dev server, e.g. 'tcp://localhost:5000'")
func main() {
flag.Usage = func() {
......@@ -115,6 +116,7 @@ func main() {
Version: Version,
DocumentRoot: *documentRoot,
DevelopmentMode: *developmentMode,
WebpackAddr: *webpackAddr,
ProxyHeadersTimeout: *proxyHeadersTimeout,
APILimit: *apiLimit,
APIQueueLimit: *apiQueueLimit,
......
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