本文整理汇总了Golang中github.com/bitly/nsq/util.ApiResponse函数的典型用法代码示例。如果您正苦于以下问题:Golang ApiResponse函数的具体用法?Golang ApiResponse怎么用?Golang ApiResponse使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了ApiResponse函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: pauseChannelHandler
func (s *httpServer) pauseChannelHandler(w http.ResponseWriter, req *http.Request) {
reqParams, err := util.NewReqParams(req)
if err != nil {
log.Printf("ERROR: failed to parse request params - %s", err.Error())
util.ApiResponse(w, 500, "INVALID_REQUEST", nil)
return
}
topicName, channelName, err := util.GetTopicChannelArgs(reqParams)
if err != nil {
util.ApiResponse(w, 500, err.Error(), nil)
return
}
topic, err := s.context.nsqd.GetExistingTopic(topicName)
if err != nil {
util.ApiResponse(w, 500, "INVALID_TOPIC", nil)
return
}
channel, err := topic.GetExistingChannel(channelName)
if err != nil {
util.ApiResponse(w, 500, "INVALID_CHANNEL", nil)
return
}
if strings.HasPrefix(req.URL.Path, "/pause") {
channel.Pause()
} else {
channel.UnPause()
}
util.ApiResponse(w, 200, "OK", nil)
}
示例2: deleteChannelHandler
func (s *httpServer) deleteChannelHandler(w http.ResponseWriter, req *http.Request) {
reqParams, err := util.NewReqParams(req)
if err != nil {
log.Printf("ERROR: failed to parse request params - %s", err.Error())
util.ApiResponse(w, 500, "INVALID_REQUEST", nil)
return
}
topicName, channelName, err := util.GetTopicChannelArgs(reqParams)
if err != nil {
util.ApiResponse(w, 500, err.Error(), nil)
return
}
topic, err := s.context.nsqd.GetExistingTopic(topicName)
if err != nil {
util.ApiResponse(w, 500, "INVALID_TOPIC", nil)
return
}
err = topic.DeleteExistingChannel(channelName)
if err != nil {
util.ApiResponse(w, 500, "INVALID_CHANNEL", nil)
return
}
util.ApiResponse(w, 200, "OK", nil)
}
示例3: deleteTopicHandler
func deleteTopicHandler(w http.ResponseWriter, req *http.Request) {
reqParams, err := util.NewReqParams(req)
if err != nil {
util.ApiResponse(w, 500, "INVALID_REQUEST", nil)
return
}
topicName, err := reqParams.Get("topic")
if err != nil {
util.ApiResponse(w, 500, "MISSING_ARG_TOPIC", nil)
return
}
registrations := lookupd.DB.FindRegistrations("channel", topicName, "*")
for _, registration := range registrations {
log.Printf("DB: removing channel(%s) from topic(%s)", registration.SubKey, topicName)
lookupd.DB.RemoveRegistration(*registration)
}
registrations = lookupd.DB.FindRegistrations("topic", topicName, "")
for _, registration := range registrations {
log.Printf("DB: removing topic(%s)", topicName)
lookupd.DB.RemoveRegistration(*registration)
}
util.ApiResponse(w, 200, "OK", nil)
}
示例4: putHandler
func putHandler(w http.ResponseWriter, req *http.Request) {
reqParams, err := util.NewReqParams(req)
if err != nil {
log.Printf("ERROR: failed to parse request params - %s", err.Error())
util.ApiResponse(w, 500, "INVALID_REQUEST", nil)
return
}
topicName, err := reqParams.Get("topic")
if err != nil {
util.ApiResponse(w, 500, "MISSING_ARG_TOPIC", nil)
return
}
if !nsq.IsValidTopicName(topicName) {
util.ApiResponse(w, 500, "INVALID_ARG_TOPIC", nil)
return
}
if int64(len(reqParams.Body)) > nsqd.options.maxMessageSize {
util.ApiResponse(w, 500, "MSG_TOO_BIG", nil)
return
}
topic := nsqd.GetTopic(topicName)
msg := nsq.NewMessage(<-nsqd.idChan, reqParams.Body)
err = topic.PutMessage(msg)
if err != nil {
util.ApiResponse(w, 500, "NOK", nil)
return
}
w.Header().Set("Content-Length", "2")
io.WriteString(w, "OK")
}
示例5: tombstoneTopicProducerHandler
func tombstoneTopicProducerHandler(w http.ResponseWriter, req *http.Request) {
reqParams, err := util.NewReqParams(req)
if err != nil {
util.ApiResponse(w, 500, "INVALID_REQUEST", nil)
return
}
topicName, err := reqParams.Get("topic")
if err != nil {
util.ApiResponse(w, 500, "MISSING_ARG_TOPIC", nil)
return
}
node, err := reqParams.Get("node")
if err != nil {
util.ApiResponse(w, 500, "MISSING_ARG_NODE", nil)
return
}
log.Printf("DB: setting tombstone for [email protected]%s of topic(%s)", node, topicName)
producers := lookupd.DB.FindProducers("topic", topicName, "")
for _, p := range producers {
thisNode := fmt.Sprintf("%s:%d", p.peerInfo.BroadcastAddress, p.peerInfo.HttpPort)
if thisNode == node {
p.Tombstone()
}
}
util.ApiResponse(w, 200, "OK", nil)
}
示例6: pauseTopicHandler
func (s *httpServer) pauseTopicHandler(w http.ResponseWriter, req *http.Request) {
reqParams, err := util.NewReqParams(req)
if err != nil {
log.Printf("ERROR: failed to parse request params - %s", err.Error())
util.ApiResponse(w, 500, "INVALID_REQUEST", nil)
return
}
topicName, err := reqParams.Get("topic")
if err != nil {
util.ApiResponse(w, 500, "MISSING_ARG_TOPIC", nil)
return
}
topic, err := s.context.nsqd.GetExistingTopic(topicName)
if err != nil {
util.ApiResponse(w, 500, "INVALID_TOPIC", nil)
return
}
if strings.HasPrefix(req.URL.Path, "/pause") {
err = topic.Pause()
} else {
err = topic.UnPause()
}
if err != nil {
log.Printf("ERROR: failure in %s - %s", req.URL.Path, err.Error())
}
util.ApiResponse(w, 200, "OK", nil)
}
示例7: deleteChannelHandler
func deleteChannelHandler(w http.ResponseWriter, req *http.Request) {
reqParams, err := util.NewReqParams(req)
if err != nil {
util.ApiResponse(w, 500, "INVALID_REQUEST", nil)
return
}
topicName, channelName, err := util.GetTopicChannelArgs(reqParams)
if err != nil {
util.ApiResponse(w, 500, err.Error(), nil)
return
}
registrations := lookupd.DB.FindRegistrations("channel", topicName, channelName)
if len(registrations) == 0 {
util.ApiResponse(w, 404, "NOT_FOUND", nil)
return
}
log.Printf("DB: removing channel(%s) from topic(%s)", channelName, topicName)
for _, registration := range registrations {
lookupd.DB.RemoveRegistration(*registration)
}
util.ApiResponse(w, 200, "OK", nil)
}
示例8: statsHandler
func (s *httpServer) statsHandler(w http.ResponseWriter, req *http.Request) {
reqParams, err := util.NewReqParams(req)
if err != nil {
log.Printf("ERROR: failed to parse request params - %s", err)
if acceptVersion(req) == 1 {
util.V1ApiResponse(w, 400, "INVALID_REQUEST")
} else {
// this handler always returns 500 for backwards compatibility
util.ApiResponse(w, 500, "INVALID_REQUEST", nil)
}
return
}
formatString, _ := reqParams.Get("format")
jsonFormat := formatString == "json"
stats := s.context.nsqd.GetStats()
if !jsonFormat {
s.doTextStats(stats, w)
return
}
if acceptVersion(req) == 1 {
util.V1ApiResponse(w, 200, struct {
Version string `json:"version"`
Topics []TopicStats `json:"topics"`
}{util.BINARY_VERSION, stats})
} else {
util.ApiResponse(w, 200, "OK", struct {
Version string `json:"version"`
Topics []TopicStats `json:"topics"`
}{util.BINARY_VERSION, stats})
}
}
示例9: lookupHandler
func lookupHandler(w http.ResponseWriter, req *http.Request) {
reqParams, err := util.NewReqParams(req)
if err != nil {
util.ApiResponse(w, 500, "INVALID_REQUEST", nil)
return
}
topicName, err := reqParams.Get("topic")
if err != nil {
util.ApiResponse(w, 500, "MISSING_ARG_TOPIC", nil)
return
}
registration := lookupd.DB.FindRegistrations("topic", topicName, "")
if len(registration) == 0 {
util.ApiResponse(w, 500, "INVALID_ARG_TOPIC", nil)
return
}
channels := lookupd.DB.FindRegistrations("channel", topicName, "*").SubKeys()
producers := lookupd.DB.FindProducers("topic", topicName, "")
producers = producers.FilterByActive(lookupd.inactiveProducerTimeout, lookupd.tombstoneLifetime)
data := make(map[string]interface{})
data["channels"] = channels
data["producers"] = producers.PeerInfo()
util.ApiResponse(w, 200, "OK", data)
}
示例10: counterDataHandler
// this endpoint works by giving out an ID that maps to a stats dictionary
// The initial request is the number of messages processed since each nsqd started up.
// Subsequent requsts pass that ID and get an updated delta based on each individual channel/nsqd message count
// That ID must be re-requested or it will be expired.
func (s *httpServer) counterDataHandler(w http.ResponseWriter, req *http.Request) {
reqParams, err := util.NewReqParams(req)
if err != nil {
log.Printf("ERROR: failed to parse request params - %s", err.Error())
util.ApiResponse(w, 500, "INVALID_REQUEST", nil)
return
}
statsID, _ := reqParams.Get("id")
now := time.Now()
if statsID == "" {
// make a new one
statsID = fmt.Sprintf("%d.%d", now.Unix(), now.UnixNano())
}
stats, ok := s.counters[statsID]
if !ok {
stats = make(map[string]int64)
}
newStats := make(map[string]int64)
newStats["time"] = now.Unix()
producers, _ := lookupd.GetLookupdProducers(s.context.nsqadmin.options.NSQLookupdHTTPAddresses)
addresses := make([]string, len(producers))
for i, p := range producers {
addresses[i] = p.HTTPAddress()
}
_, channelStats, _ := lookupd.GetNSQDStats(addresses, "")
var newMessages int64
var totalMessages int64
for _, channelStats := range channelStats {
for _, hostChannelStats := range channelStats.HostStats {
key := fmt.Sprintf("%s:%s:%s", channelStats.TopicName, channelStats.ChannelName, hostChannelStats.HostAddress)
d, ok := stats[key]
if ok && d <= hostChannelStats.MessageCount {
newMessages += (hostChannelStats.MessageCount - d)
}
totalMessages += hostChannelStats.MessageCount
newStats[key] = hostChannelStats.MessageCount
}
}
s.counters[statsID] = newStats
data := make(map[string]interface{})
data["new_messages"] = newMessages
data["total_messages"] = totalMessages
data["id"] = statsID
util.ApiResponse(w, 200, "OK", data)
}
示例11: ServeHTTP
func (s *httpServer) ServeHTTP(w http.ResponseWriter, req *http.Request) {
switch req.URL.Path {
case "/ping":
s.pingHandler(w, req)
case "/info":
s.infoHandler(w, req)
case "/lookup":
s.lookupHandler(w, req)
case "/topics":
s.topicsHandler(w, req)
case "/channels":
s.channelsHandler(w, req)
case "/nodes":
s.nodesHandler(w, req)
case "/delete_topic":
s.deleteTopicHandler(w, req)
case "/delete_channel":
s.deleteChannelHandler(w, req)
case "/tombstone_topic_producer":
s.tombstoneTopicProducerHandler(w, req)
case "/create_topic":
s.createTopicHandler(w, req)
case "/create_channel":
s.createChannelHandler(w, req)
case "/debug":
s.debugHandler(w, req)
default:
util.ApiResponse(w, 404, "NOT_FOUND", nil)
}
}
示例12: infoHandler
func infoHandler(w http.ResponseWriter, req *http.Request) {
util.ApiResponse(w, 200, "OK", struct {
Version string `json:"version"`
}{
Version: util.BINARY_VERSION,
})
}
示例13: debugHandler
func debugHandler(w http.ResponseWriter, req *http.Request) {
lookupd.DB.RLock()
defer lookupd.DB.RUnlock()
data := make(map[string][]map[string]interface{})
for r, producers := range lookupd.DB.registrationMap {
key := r.Category + ":" + r.Key + ":" + r.SubKey
data[key] = make([]map[string]interface{}, 0)
for _, p := range producers {
m := make(map[string]interface{})
m["id"] = p.peerInfo.id
m["address"] = p.peerInfo.Address //TODO: remove for 1.0
m["hostname"] = p.peerInfo.Hostname
m["broadcast_address"] = p.peerInfo.BroadcastAddress
m["tcp_port"] = p.peerInfo.TcpPort
m["http_port"] = p.peerInfo.HttpPort
m["version"] = p.peerInfo.Version
m["last_update"] = p.peerInfo.lastUpdate.UnixNano()
m["tombstoned"] = p.tombstoned
m["tombstoned_at"] = p.tombstonedAt.UnixNano()
data[key] = append(data[key], m)
}
}
util.ApiResponse(w, 200, "OK", data)
}
示例14: ServeHTTP
func (s *httpServer) ServeHTTP(w http.ResponseWriter, req *http.Request) {
err := s.debugRouter(w, req)
if err != nil {
log.Printf("ERROR: %s", err)
util.ApiResponse(w, 404, "NOT_FOUND", nil)
}
}
示例15: pauseChannelHandler
func (s *httpServer) pauseChannelHandler(w http.ResponseWriter, req *http.Request) {
err := s.doPauseChannel(req)
if err != nil {
if acceptVersion(req) == 1 {
util.V1ApiResponse(w, err.(httpError).Code(), err)
} else {
// this handler always returns 500 for backwards compatibility
util.ApiResponse(w, 500, err.Error(), nil)
}
return
}
if acceptVersion(req) == 1 {
util.V1ApiResponse(w, 200, nil)
} else {
util.ApiResponse(w, 200, "OK", nil)
}
}