示例#1
0
文件: server.go 项目: hyl87/pushgo
func (self *Serv) Regis(cmd PushCommand, sock *PushWS) (result int, arguments util.JsMap) {
	// A semi-no-op, since we don't care about the appid, but we do want
	// to create a valid endpoint.
	MetricIncrement("endpoint registration attempt")
	var err error
	args := cmd.Arguments.(util.JsMap)
	args["status"] = 200
	var endPoint string
	if _, ok := self.config["push.endpoint"]; !ok {
		if _, ok := self.config["pushEndpoint"]; !ok {
			endPoint = "http://localhost/update/<token>"
		} else {
			endPoint = self.config["pushEndpoint"].(string)
		}
	} else {
		endPoint = self.config["push.endpoint"].(string)
	}
	// Generate the call back URL
	token, err := storage.GenPK(sock.Uaid,
		args["channelID"].(string))
	if err != nil {
		return 500, nil
	}
	// if there is a key, encrypt the token
	if self.key != nil {
		btoken := []byte(token)
		token, err = Encode(self.key, btoken)
		if err != nil {
			if self.logger != nil {
				self.logger.Error("server",
					"Token Encoding error",
					util.Fields{"uaid": sock.Uaid,
						"channelID": args["channelID"].(string)})
			}
			return 500, nil
		}

	}
	// cheezy variable replacement.
	endPoint = strings.Replace(endPoint, "<token>", token, -1)
	host := self.config["shard.current_host"].(string) + ":" +
		self.config["port"].(string)
	endPoint = strings.Replace(endPoint, "<current_host>", host, -1)
	args["push.endpoint"] = endPoint
	if self.logger != nil {
		self.logger.Info("server",
			"Generated Endpoint",
			util.Fields{"uaid": sock.Uaid,
				"channelID": args["channelID"].(string),
				"token":     token,
				"endpoint":  endPoint})
	}
	return 200, args
}
示例#2
0
文件: main.go 项目: rtilder/pushgo
// Handle a routed update.
func updater(update *router.Update, logger *util.HekaLogger) (err error) {
	//log.Printf("UPDATE::: %s", update)
	simplepush.MetricIncrement("updates.routed.incoming")
	pk, _ := storage.GenPK(update.Uaid, update.Chid)
	err = store.UpdateChannel(pk, update.Vers)
	if client, ok := simplepush.Clients[update.Uaid]; ok {
		simplepush.Flush(client, update.Chid, int64(update.Vers))
		duration := strconv.FormatInt(time.Now().Sub(update.Time).Nanoseconds(), 10)
		if logger != nil {
			logger.Info("timer", "Routed flush to client completed",
				util.Fields{
					"uaid":     update.Uaid,
					"chid":     update.Chid,
					"duration": duration})
		} else {
			log.Printf("Routed flush complete: %s", duration)
		}
	}
	return nil
}