Merge pull request #1417 from ajnavarro/fix-log-web-ui-http-handler

This commit is contained in:
Antonio Navarro Perez 2021-02-12 10:18:51 +01:00 committed by GitHub
commit 2089e5eeb4
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 10 additions and 4 deletions

View file

@ -67,6 +67,8 @@ type Handler struct {
api *pilosa.API
ln net.Listener
// url is used to hold the advertise bind address for printing a log during startup.
url string
closeTimeout time.Duration
@ -135,9 +137,13 @@ func OptHandlerLogger(logger logger.Logger) handlerOption {
}
}
func OptHandlerListener(ln net.Listener) handlerOption {
// OptHandlerListener set the listener that will be used by the HTTP server.
// Url must be the advertised URL. It will be used to show a log to the user
// about where the Web UI is. This option is mandatory.
func OptHandlerListener(ln net.Listener, url string) handlerOption {
return func(h *Handler) error {
h.ln = ln
h.url = url
return nil
}
}
@ -502,7 +508,7 @@ type statikHandler struct {
func newStatikHandler(h *Handler) statikHandler {
fs, err := h.fileSystem.New()
if err == nil {
h.logger.Printf("enabled Web UI (%s) at %s", h.api.LatticeVersion(), h.ln.Addr().String())
h.logger.Printf("enabled Web UI (%s) at %s", h.api.LatticeVersion(), h.url)
}
return statikHandler{

View file

@ -45,7 +45,7 @@ func TestHandlerOptions(t *testing.T) {
return err
}, 10)
_, err = http.NewHandler(http.OptHandlerListener(ln))
_, err = http.NewHandler(http.OptHandlerListener(ln, ln.Addr().String()))
if err == nil {
t.Fatalf("expected error making handler without options, got nil")
}

View file

@ -450,7 +450,7 @@ func (m *Command) SetupServer() error {
http.OptHandlerAPI(m.API),
http.OptHandlerLogger(m.logger),
http.OptHandlerFileSystem(&statik.FileSystem{}),
http.OptHandlerListener(m.ln),
http.OptHandlerListener(m.ln, m.Config.Advertise),
http.OptHandlerCloseTimeout(m.closeTimeout),
http.OptHandlerMiddleware(m.grpcServer.middleware(m.Config.Handler.AllowedOrigins)),
)