diff --git a/filesystem.go b/filesystem.go new file mode 100644 index 000000000..0b5b27220 --- /dev/null +++ b/filesystem.go @@ -0,0 +1,42 @@ +// Copyright 2017 Pilosa Corp. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package pilosa + +import ( + "fmt" + "net/http" +) + +// Ensure nopStaticFileSystem implements interface. +var _ StaticFileSystem = &nopStaticFileSystem{} + +// StaticFileSystem represents an interface for a static WebUI. +type StaticFileSystem interface { + New() (http.FileSystem, error) +} + +func init() { + NopStaticFileSystem = &nopStaticFileSystem{} +} + +// NopStaticFileSystem represents a StaticFileSystem that returns an error if called. +var NopStaticFileSystem StaticFileSystem + +type nopStaticFileSystem struct{} + +// New is a no-op implementation of StaticFileSystem New method. +func (n *nopStaticFileSystem) New() (http.FileSystem, error) { + return nil, fmt.Errorf("static file system not implemented") +} diff --git a/filesystem/statik.go b/filesystem/statik.go new file mode 100644 index 000000000..405e31a11 --- /dev/null +++ b/filesystem/statik.go @@ -0,0 +1,32 @@ +// Copyright 2017 Pilosa Corp. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package filesystem + +import ( + "net/http" + + "github.com/pilosa/pilosa" + "github.com/rakyll/statik/fs" +) + +// Ensure nopStaticFileSystem implements interface. +var _ pilosa.StaticFileSystem = &StatikFS{} + +type StatikFS struct{} + +// New is a statik implementation of StaticFileSystem New method. +func (s *StatikFS) New() (http.FileSystem, error) { + return fs.New() +} diff --git a/handler.go b/handler.go index 38fd24eca..1688cf5a3 100644 --- a/handler.go +++ b/handler.go @@ -47,7 +47,6 @@ import ( // Allow building Pilosa without the web UI. _ "github.com/pilosa/pilosa/statik" - "github.com/rakyll/statik/fs" ) // Handler represents an HTTP handler. @@ -57,6 +56,8 @@ type Handler struct { BroadcastHandler BroadcastHandler StatusHandler StatusHandler + StaticFileSystem StaticFileSystem + // Local hostname & cluster configuration. Node *Node Cluster *Cluster @@ -98,6 +99,11 @@ type errorResponse struct { // NewHandler returns a new instance of Handler with a default logger. func NewHandler() *Handler { handler := &Handler{ + Broadcaster: NopBroadcaster, + //BroadcastHandler: NopBroadcastHandler, // TODO: implement the nop + //StatusHandler: NopStatusHandler, // TODO: implement the nop + StaticFileSystem: NopStaticFileSystem, + LogOutput: os.Stderr, } BuildRouters(handler) @@ -285,7 +291,7 @@ func (h *Handler) handleWebUI(w http.ResponseWriter, r *http.Request) { http.Error(w, "Welcome. Pilosa is running. Visit https://www.pilosa.com/docs/ for more information or try the WebUI by visiting this URL in your browser.", http.StatusNotFound) return } - statikFS, err := fs.New() + statikFS, err := h.StaticFileSystem.New() if err != nil { h.writeQueryResponse(w, r, &QueryResponse{Err: err}) h.logger().Println("Pilosa WebUI is not available. Please run `make generate-statik` before building Pilosa with `make install`.") diff --git a/handler_test.go b/handler_test.go index 1caf7bf99..6386f53f9 100644 --- a/handler_test.go +++ b/handler_test.go @@ -29,6 +29,7 @@ import ( "github.com/gogo/protobuf/proto" "github.com/pilosa/pilosa" + "github.com/pilosa/pilosa/filesystem" "github.com/pilosa/pilosa/internal" "github.com/pilosa/pilosa/pql" "github.com/pilosa/pilosa/test" @@ -1853,6 +1854,7 @@ func TestHandler_WebUI(t *testing.T) { h := test.NewHandler() h.Holder = hldr.Holder h.Cluster = test.NewCluster(1) + h.StaticFileSystem = &filesystem.StatikFS{} w := httptest.NewRecorder() h.ServeHTTP(w, test.MustNewHTTPRequest("GET", "/", nil)) diff --git a/server/server.go b/server/server.go index f0d3314fa..e056bc76d 100644 --- a/server/server.go +++ b/server/server.go @@ -33,6 +33,7 @@ import ( "crypto/tls" "github.com/pilosa/pilosa" + "github.com/pilosa/pilosa/filesystem" "github.com/pilosa/pilosa/gcnotify" "github.com/pilosa/pilosa/gossip" "github.com/pilosa/pilosa/statsd" @@ -191,6 +192,9 @@ func (m *Command) SetupServer() error { m.Server.Handler.RemoteClient = c m.Server.Cluster.RemoteClient = c + // Statik file system. + m.Server.Handler.StaticFileSystem = &filesystem.StatikFS{} + // Default coordintor to port 0 when not specified so that coordinator // can be set to the value of server.URI after server binds to a port. // This would only be useful in a one-node cluster.