示例#1
0
func CallRpcServiceWithContext(c *gin.Context, env Env, name, method string, req interface{}, res rpcResponse) bool {

	// Get the service TCP client
	client, err := getEnvTcpClient(name, env)
	if goutils.HasErrorAndPrintStack(err) {
		c.AbortWithError(http.StatusInternalServerError, err)
		return true
	}

	err = client.Call(method, req, res)
	if err != nil {

		// Check if it is not found error
		if err.Error() == "not found" {
			c.AbortWithStatus(http.StatusNotFound)
		} else {
			goutils.PrintStackAndError(err)
			c.AbortWithError(http.StatusInternalServerError, err)
		}
		return true
	}

	if res.HasError() {
		c.JSON(http.StatusBadRequest, res.ReturnErrorMap())
		return true
	}

	return false
}
示例#2
0
文件: nsqutil.go 项目: kobeld/goutils
// The general publish method that put data into NSQ as a producer
func PuslishNsqTopicWithData(topic string, topicData interface{}) (err error) {
	producer, err := getNsqProducer()
	if err != nil {
		goutils.PrintStackAndError(err)
		return
	}

	data, err := json.Marshal(topicData)
	if err != nil {
		goutils.PrintStackAndError(err)
		return
	}

	err = producer.Publish(topic, data)
	if err != nil {
		goutils.PrintStackAndError(err)
		return
	}

	return
}
示例#3
0
文件: nsqutil.go 项目: kobeld/goutils
// Get the the singleton NSQ Producer
func getNsqProducer() (*nsq.Producer, error) {
	var err error
	if defaultNsqProducer == nil {
		config := nsq.NewConfig()
		defaultNsqProducer, err = nsq.NewProducer(defaultNsqAddr, config)
		if err != nil {
			goutils.PrintStackAndError(err)
			return nil, err
		}
	}
	return defaultNsqProducer, nil
}
示例#4
0
文件: authorize.go 项目: iguiyu/demo
func OAuthUser() gin.HandlerFunc {
	return func(c *gin.Context) {

		// Check if the Authorization header exists
		authHeader := c.Request.Header.Get("Authorization")
		if authHeader == "" {
			c.Next()
			return
		}

		// check if the Bearer Token exists
		parts := strings.SplitN(authHeader, " ", 2)
		if len(parts) != 2 || parts[0] != "Bearer" || parts[1] == "" {
			c.Next()
			return
		}

		accessData, err := oauth.GetAccess(parts[1])
		if err != nil {
			if err != global.ErrInvalidToken { // Should be a system error
				goutils.PrintStackAndError(err)
			}
			c.Next()
			return
		}

		// Check the Access data
		if accessData.IsExpired() || accessData.UserData == nil {
			c.Next()
			return
		}

		var (
			userId = accessData.UserData.(string)
			req    = &userReqres.UserReq{Id: bson.ObjectIdHex(userId)}
			res    = &userReqres.UserRes{}
		)

		if helpers.CallRpcServiceWithContext(c, configs.AppConf, userReqres.SERVICE_NAME, userReqres.RPC_USER_SHOW, req, res) {
			return
		}

		c.Set("me", res.User)

		c.Next()
	}
}
示例#5
0
文件: nsqutil.go 项目: kobeld/goutils
func RegisterNsqConsumers(consumers []Consumer) (err error) {

	config := nsq.NewConfig()
	for _, consumer := range consumers {
		topic, channel := consumer.TopicAndChannel()

		nsqConsumer, err := nsq.NewConsumer(topic, channel, config)
		if err != nil {
			panic(err)
		}

		nsqConsumer.AddHandler(consumer)
		err = nsqConsumer.ConnectToNSQLookupd(defaultLookupAddr)
		if err != nil {
			goutils.PrintStackAndError(err)
			return err
		}
	}

	return
}
示例#6
0
func NewMqttManager(domain MqttDomain) (manager *MqttManager, err error) {

	opts := &mqtt.ClientOptions{
		ClientID:             domain.ClientId(),
		CleanSession:         true,
		MaxReconnectInterval: 1 * time.Second,
		AutoReconnect:        true,
		KeepAlive:            30 * time.Second,
	}

	opts.AddBroker(domain.Address())
	opts.OnConnect = func(c *mqtt.Client) {
		println("Connected")

		for _, item := range manager.Domain.Topics() {

			println("Reregistering topic: " + item.Topic)

			// TODO: Should also store the qos
			token := manager.Client.Subscribe(item.Topic, item.Qos, func(c *mqtt.Client, msg mqtt.Message) {
				item.Callback(msg)
			})
			if token.Wait() && token.Error() != nil {
				goutils.PrintStackAndError(token.Error())
				continue
			}

		}
	}

	manager = &MqttManager{
		Domain: domain,
		Client: mqtt.NewClient(opts),
	}

	return
}