put Statik behind an interface

This commit is contained in:
Travis Turner 2018-03-06 16:57:45 -06:00
parent 36cc056618
commit e03dee9386
No known key found for this signature in database
GPG key ID: 7F08008DFD9314C9
5 changed files with 88 additions and 2 deletions

42
filesystem.go Normal file
View file

@ -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")
}

32
filesystem/statik.go Normal file
View file

@ -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()
}

View file

@ -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`.")

View file

@ -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))

View file

@ -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.