diff --git a/apps/nsqadmin/main.go b/apps/nsqadmin/main.go index 55e7bdf7a..eb98ff266 100644 --- a/apps/nsqadmin/main.go +++ b/apps/nsqadmin/main.go @@ -30,6 +30,7 @@ func nsqadminFlagSet(opts *nsqadmin.Options) *flag.FlagSet { flagSet.String("http-address", opts.HTTPAddress, ": to listen on for HTTP clients") flagSet.String("base-path", opts.BasePath, "URL base path") + flagSet.String("dev-static-dir", opts.DevStaticDir, "(development use only)") flagSet.String("graphite-url", opts.GraphiteURL, "graphite HTTP address") flagSet.Bool("proxy-graphite", false, "proxy HTTP requests to graphite") diff --git a/nsqadmin/README.md b/nsqadmin/README.md index 78fcf5a04..c1ff63364 100644 --- a/nsqadmin/README.md +++ b/nsqadmin/README.md @@ -11,14 +11,16 @@ Read the [docs](https://nsq.io/components/nsqadmin.html) ### Dependencies 1. Install NodeJS 16.x (includes `npm`) + 2. Install [`go-bindata`](https://github.com/shuLhan/go-bindata) (for "legacy" go < 1.16) ### Live Reload Workflow 1. `$ npm install` 2. `$ ./gulp --series clean watch` - 3. `$ go build --tags debug` (from `apps/nsqadmin` directory) + 3. `$ cd .. && make && ./build/nsqadmin --dev-static-dir=nsqadmin/static/build --lookupd-http-address=<...>` 4. make changes to static assets (repeat step 3 only if you make changes to any Go code) ### Build 1. `$ ./gulp --series clean build` + 2. `$ go-bindata --pkg=nsqadmin --prefix=static/build/ static/build/...` (for "legacy" go < 1.16) diff --git a/nsqadmin/http.go b/nsqadmin/http.go index 1b6a33919..0187074a3 100644 --- a/nsqadmin/http.go +++ b/nsqadmin/http.go @@ -48,11 +48,12 @@ func NewSingleHostReverseProxy(target *url.URL, connectTimeout time.Duration, re } type httpServer struct { - nsqadmin *NSQAdmin - router http.Handler - client *http_api.Client - ci *clusterinfo.ClusterInfo - basePath string + nsqadmin *NSQAdmin + router http.Handler + client *http_api.Client + ci *clusterinfo.ClusterInfo + basePath string + devStaticDir string } func NewHTTPServer(nsqadmin *NSQAdmin) *httpServer { @@ -66,12 +67,15 @@ func NewHTTPServer(nsqadmin *NSQAdmin) *httpServer { router.PanicHandler = http_api.LogPanicHandler(nsqadmin.logf) router.NotFound = http_api.LogNotFoundHandler(nsqadmin.logf) router.MethodNotAllowed = http_api.LogMethodNotAllowedHandler(nsqadmin.logf) + s := &httpServer{ nsqadmin: nsqadmin, router: router, client: client, ci: clusterinfo.New(nsqadmin.logf, client), - basePath: nsqadmin.getOpts().BasePath, + + basePath: nsqadmin.getOpts().BasePath, + devStaticDir: nsqadmin.getOpts().DevStaticDir, } bp := func(p string) string { @@ -164,7 +168,17 @@ func (s *httpServer) indexHandler(w http.ResponseWriter, req *http.Request, ps h func (s *httpServer) staticAssetHandler(w http.ResponseWriter, req *http.Request, ps httprouter.Params) (interface{}, error) { assetName := ps.ByName("asset") - asset, err := staticAsset(assetName) + var ( + asset []byte + err error + ) + if s.devStaticDir != "" { + s.nsqadmin.logf(LOG_DEBUG, "using dev dir %q for static asset %q", s.devStaticDir, assetName) + fsPath := path.Join(s.devStaticDir, assetName) + asset, err = ioutil.ReadFile(fsPath) + } else { + asset, err = staticAsset(assetName) + } if err != nil { return nil, http_api.Err{404, "NOT_FOUND"} } diff --git a/nsqadmin/options.go b/nsqadmin/options.go index 8100af881..55dfd51a6 100644 --- a/nsqadmin/options.go +++ b/nsqadmin/options.go @@ -14,6 +14,8 @@ type Options struct { HTTPAddress string `flag:"http-address"` BasePath string `flag:"base-path"` + DevStaticDir string `flag:"dev-static-dir"` + GraphiteURL string `flag:"graphite-url"` ProxyGraphite bool `flag:"proxy-graphite"` diff --git a/nsqadmin/static.go b/nsqadmin/static.go index ead099948..59eea0586 100644 --- a/nsqadmin/static.go +++ b/nsqadmin/static.go @@ -1,5 +1,5 @@ -//go:build go1.16 && !debug -// +build go1.16,!debug +//go:build go1.16 +// +build go1.16 package nsqadmin diff --git a/nsqadmin/static_debug.go b/nsqadmin/static_debug.go deleted file mode 100644 index 10741acdb..000000000 --- a/nsqadmin/static_debug.go +++ /dev/null @@ -1,19 +0,0 @@ -//go:build go1.16 && debug -// +build go1.16,debug - -package nsqadmin - -import ( - "io/ioutil" - "os" - "path" -) - -func staticAsset(name string) ([]byte, error) { - path := path.Join("../../nsqadmin/static/build", name) - _, err := os.Stat(path) - if err != nil { - return nil, err - } - return ioutil.ReadFile(path) -}