Skip to content

Commit

Permalink
nsqadmin: properly write responses
Browse files Browse the repository at this point in the history
jehiah committed Jul 31, 2015

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
1 parent df41bf9 commit 36c4174
Showing 2 changed files with 33 additions and 26 deletions.
16 changes: 12 additions & 4 deletions internal/http_api/api_response.go
Original file line number Diff line number Diff line change
@@ -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
}
}
43 changes: 21 additions & 22 deletions nsqadmin/http.go
Original file line number Diff line number Diff line change
@@ -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))
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))

if s.ctx.nsqadmin.opts.ProxyGraphite {
router.Handler("GET", "/render", s.proxy)
@@ -139,7 +139,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
}

0 comments on commit 36c4174

Please sign in to comment.