Repository: nsqio/nsq
File: nsqadmin/http.go (Go)

Code before:
		router:   router,
	}

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

	if s.ctx.nsqadmin.opts.ProxyGraphite {
		router.Handler("GET", "/render", s.proxy)
	}

	return s
}

func (s *httpServer) ServeHTTP(w http.ResponseWriter, req *http.Request) {
	s.router.ServeHTTP(w, req)
}

func (s *httpServer) pingHandler(w http.ResponseWriter, req *http.Request, ps httprouter.Params) (interface{}, error) {
	return "OK", nil
}

func (s *httpServer) embeddedAssetHandler(w http.ResponseWriter, req *http.Request, ps httprouter.Params) (interface{}, error) {
	assetName := ps.ByName("asset")

	asset, error := templates.Asset(assetName)
	if error != nil {
		return nil, http_api.Err{404, "NOT_FOUND"}
	}

	if strings.HasSuffix(assetName, ".js") {

Review comment (refactor):
maybe it makes sense to use a little function-local helper function at this point, maybe something like

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


Code after:
		router:   router,
	}

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

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

	return s
}

func (s *httpServer) ServeHTTP(w http.ResponseWriter, req *http.Request) {
	s.router.ServeHTTP(w, req)
}

func (s *httpServer) pingHandler(w http.ResponseWriter, req *http.Request, ps httprouter.Params) (interface{}, error) {
	return "OK", nil
}

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 {
		return nil, http_api.Err{404, "NOT_FOUND"}