Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

nsqadmin: format responses #612

Merged
merged 2 commits into from
Jul 31, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 12 additions & 4 deletions internal/http_api/api_response.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,18 @@ func PlainText(f APIHandler) APIHandler {
code = err.(Err).Code
data = err.Error()
}
response := data.(string)
w.Header().Set("Content-Length", strconv.Itoa(len(response)))
w.WriteHeader(code)
io.WriteString(w, response)
switch d := data.(type) {
case string:
w.Header().Set("Content-Length", strconv.Itoa(len(d)))
w.WriteHeader(code)
io.WriteString(w, d)
case []byte:
w.Header().Set("Content-Length", strconv.Itoa(len(d)))
w.WriteHeader(code)
w.Write(d)
default:
panic(fmt.Sprintf("unknown response type %T", data))
}
return nil, nil
}
}
Expand Down
46 changes: 24 additions & 22 deletions nsqadmin/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,27 +89,27 @@ func NewHTTPServer(ctx *Context) *httpServer {

router.Handle("GET", "/ping", http_api.Decorate(s.pingHandler, log, http_api.PlainText))

router.Handle("GET", "/", http_api.Decorate(s.indexHandler, log))
router.Handle("GET", "/nodes", http_api.Decorate(s.nodesHandler, log))
router.Handle("GET", "/node/:node", http_api.Decorate(s.nodeHandler, log))
router.Handle("GET", "/topic/:topic", http_api.Decorate(s.topicHandler, log))
router.Handle("GET", "/topic/:topic/:channel", http_api.Decorate(s.channelHandler, log))
router.Handle("GET", "/static/:asset", http_api.Decorate(s.embeddedAssetHandler, log))
router.Handle("GET", "/counter", http_api.Decorate(s.counterHandler, log))
router.Handle("GET", "/counter/data", http_api.Decorate(s.counterDataHandler, log))
router.Handle("GET", "/lookup", http_api.Decorate(s.lookupHandler, log))
router.Handle("GET", "/graphite_data", http_api.Decorate(s.graphiteDataHandler, log))

router.Handle("POST", "/tombstone_topic_producer", http_api.Decorate(s.tombstoneTopicProducerHandler, log))
router.Handle("POST", "/empty_topic", http_api.Decorate(s.emptyTopicHandler, log))
router.Handle("POST", "/delete_topic", http_api.Decorate(s.deleteTopicHandler, log))
router.Handle("POST", "/pause_topic", http_api.Decorate(s.pauseTopicHandler, log))
router.Handle("POST", "/unpause_topic", http_api.Decorate(s.pauseTopicHandler, log))
router.Handle("POST", "/empty_channel", http_api.Decorate(s.emptyChannelHandler, log))
router.Handle("POST", "/delete_channel", http_api.Decorate(s.deleteChannelHandler, log))
router.Handle("POST", "/pause_channel", http_api.Decorate(s.pauseChannelHandler, log))
router.Handle("POST", "/unpause_channel", http_api.Decorate(s.pauseChannelHandler, log))
router.Handle("POST", "/create_topic_channel", http_api.Decorate(s.createTopicChannelHandler, log))
router.Handle("GET", "/", http_api.Decorate(s.indexHandler, log, http_api.PlainText))
router.Handle("GET", "/nodes", http_api.Decorate(s.nodesHandler, log, http_api.PlainText))
router.Handle("GET", "/node/:node", http_api.Decorate(s.nodeHandler, log, http_api.PlainText))
router.Handle("GET", "/topic/:topic", http_api.Decorate(s.topicHandler, log, http_api.PlainText))
router.Handle("GET", "/topic/:topic/:channel", http_api.Decorate(s.channelHandler, log, http_api.PlainText))
router.Handle("GET", "/static/*asset", http_api.Decorate(s.embeddedAssetHandler, log, http_api.PlainText))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Meh, I should have dug into this a bit more - only this static route needed the output formatter, all the other template based endpoints write directly to the http.ResponseWriter.

It's fine though, it won't cause any problems and it's all going away in #323

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think they still needed it for early error returns where the handler didn't write a response?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

aha!

router.Handle("GET", "/counter", http_api.Decorate(s.counterHandler, log, http_api.PlainText))
router.Handle("GET", "/counter/data", http_api.Decorate(s.counterDataHandler, log, http_api.PlainText))
router.Handle("GET", "/lookup", http_api.Decorate(s.lookupHandler, log, http_api.PlainText))
router.Handle("GET", "/graphite_data", http_api.Decorate(s.graphiteDataHandler, log, http_api.PlainText))

router.Handle("POST", "/tombstone_topic_producer", http_api.Decorate(s.tombstoneTopicProducerHandler, log, http_api.PlainText))
router.Handle("POST", "/empty_topic", http_api.Decorate(s.emptyTopicHandler, log, http_api.PlainText))
router.Handle("POST", "/delete_topic", http_api.Decorate(s.deleteTopicHandler, log, http_api.PlainText))
router.Handle("POST", "/pause_topic", http_api.Decorate(s.pauseTopicHandler, log, http_api.PlainText))
router.Handle("POST", "/unpause_topic", http_api.Decorate(s.pauseTopicHandler, log, http_api.PlainText))
router.Handle("POST", "/empty_channel", http_api.Decorate(s.emptyChannelHandler, log, http_api.PlainText))
router.Handle("POST", "/delete_channel", http_api.Decorate(s.deleteChannelHandler, log, http_api.PlainText))
router.Handle("POST", "/pause_channel", http_api.Decorate(s.pauseChannelHandler, log, http_api.PlainText))
router.Handle("POST", "/unpause_channel", http_api.Decorate(s.pauseChannelHandler, log, http_api.PlainText))
router.Handle("POST", "/create_topic_channel", http_api.Decorate(s.createTopicChannelHandler, log, http_api.PlainText))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe it makes sense to use a little function-local helper function at this point, maybe something like

func router_reg(method, path string, handler http.Handler) {
    router.Handle(method, path, http_api.Decorate(handler, log), http_api.PlainText)
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not a bad idea but also not necessary


if s.ctx.nsqadmin.opts.ProxyGraphite {
router.Handler("GET", "/render", s.proxy)
Expand All @@ -128,6 +128,9 @@ func (s *httpServer) pingHandler(w http.ResponseWriter, req *http.Request, ps ht

func (s *httpServer) embeddedAssetHandler(w http.ResponseWriter, req *http.Request, ps httprouter.Params) (interface{}, error) {
assetName := ps.ByName("asset")
if strings.HasPrefix(assetName, "/") {
assetName = assetName[1:]
}

asset, error := templates.Asset(assetName)
if error != nil {
Expand All @@ -139,7 +142,6 @@ func (s *httpServer) embeddedAssetHandler(w http.ResponseWriter, req *http.Reque
} else if strings.HasSuffix(assetName, ".css") {
w.Header().Set("Content-Type", "text/css")
}

return asset, nil
}

Expand Down