Example #1
0
func NewMNSEncodingComponent(config spirit.Map) (component spirit.Component, err error) {
	conf := MNSEncodingComponentConfig{}

	if err = config.ToObject(&conf); err != nil {
		return
	}

	component = &MNSEncodingComponent{conf: conf}
	return
}
Example #2
0
func NewMNSWriter(config spirit.Map) (w io.WriteCloser, err error) {
	conf := MNSWriterConfig{}
	if err = config.ToObject(&conf); err != nil {
		return
	}

	if conf.Queue == "" {
		err = ErrAliMNSQueueIsEmpty
		return
	}

	if conf.URL == "" {
		err = ErrAliURLIsEmpty
		return
	}

	if conf.AccessKeyId == "" {
		err = ErrAliAccessKeyIdIsEmpty
		return
	}

	if conf.AccessKeySecret == "" {
		err = ErrAliAccessKeySecretIsEmpty
		return
	}

	if conf.Priority == 0 {
		conf.Priority = 8
	}

	if conf.DelaySeconds > 604800 {
		conf.DelaySeconds = 604800
	}

	if conf.DelaySeconds < 0 {
		conf.DelaySeconds = 0
	}

	client := ali_mns.NewAliMNSClient(conf.URL, conf.AccessKeyId, conf.AccessKeySecret)

	if conf.ProxyAddress != "" {
		client.SetProxy(conf.ProxyAddress)
	}

	w = &MNSWriter{conf: conf, client: client}

	return
}
Example #3
0
func NewMNSReader(config spirit.Map) (r io.ReadCloser, err error) {
	conf := MNSReaderConfig{}
	if err = config.ToObject(&conf); err != nil {
		return
	}

	if conf.Queue == "" {
		err = ErrAliMNSQueueIsEmpty
		return
	}

	if conf.URL == "" {
		err = ErrAliURLIsEmpty
		return
	}

	if conf.AccessKeyId == "" {
		err = ErrAliAccessKeyIdIsEmpty
		return
	}

	if conf.AccessKeySecret == "" {
		err = ErrAliAccessKeySecretIsEmpty
		return
	}

	if conf.MaxReadCount <= 0 || conf.MaxReadCount > 16 {
		conf.MaxReadCount = int(ali_mns.DefaultNumOfMessages)
	}

	if conf.PollingWaitSeconds <= 0 || conf.PollingWaitSeconds > 30 {
		conf.PollingWaitSeconds = int(ali_mns.DefaultTimeout)
	}

	client := ali_mns.NewAliMNSClient(conf.URL, conf.AccessKeyId, conf.AccessKeySecret)

	if conf.ProxyAddress != "" {
		client.SetProxy(conf.ProxyAddress)
	}

	r = &MNSReader{conf: conf, client: client}

	return
}
Example #4
0
func NewMNSSender(config spirit.Map) (sender spirit.Sender, err error) {
	conf := MNSSenderConfig{}
	if err = config.ToObject(&conf); err != nil {
		return
	}

	if conf.URL == "" {
		err = ErrAliURLIsEmpty
		return
	}

	if conf.AccessKeyId == "" {
		err = ErrAliAccessKeyIdIsEmpty
		return
	}

	if conf.AccessKeySecret == "" {
		err = ErrAliAccessKeySecretIsEmpty
		return
	}

	if conf.Priority == 0 {
		conf.Priority = 8
	}

	if conf.DelaySeconds > 604800 {
		conf.DelaySeconds = 604800
	}

	if conf.DelaySeconds < 0 {
		conf.DelaySeconds = 0
	}

	sender = &MNSSender{
		conf:       conf,
		mnsClient:  make(map[string]ali_mns.AliMNSQueue),
		terminaled: make(chan bool),
	}

	return
}
Example #5
0
func NewJsonApiReceiver(name string, options spirit.Map) (receiver spirit.Receiver, err error) {
	conf := JsonApiReceiverConfig{}

	if err = options.ToObject(&conf); err != nil {
		return
	}

	conf.initial()

	jsonApiReceiver := &JsonApiReceiver{
		name: name,
		conf: conf,
	}

	if jsonApiReceiver.HTTPReceiver, err = http.NewHTTPReceiver(conf.Http, jsonApiReceiver.requestHandler); err != nil {
		return
	}

	jsonApiReceiver.responseRenderer = NewAPIResponseRenderer()

	path := strings.TrimRight(conf.Path, "/")
	jsonApiReceiver.Group(path, func(r martini.Router) {
		r.Post("", jsonApiReceiver.HTTPReceiver.Handler)
		r.Post("/:apiName", jsonApiReceiver.HTTPReceiver.Handler)
		r.Options("", jsonApiReceiver.optionHandle)
		r.Options("/:apiName", jsonApiReceiver.optionHandle)
	})

	jsonApiReceiver.htmlProxy = strings.Replace(proxyHtml, "{{#XDomainLib#}}", conf.XDomain.LibUrl, 1)

	jsonApiReceiver.Group("/", func(r martini.Router) {
		r.Get("ping", func() string {
			return "pong"
		})

		if conf.XDomain.HtmlPath != "" {
			r.Get(conf.XDomain.HtmlPath, func(r *gohttp.Request) string {
				refer := r.Referer()
				if refer == "" {
					refer = r.Header.Get("Origin")
				}

				html := jsonApiReceiver.htmlProxy

				if conf.XDomain.Masters != nil && refer != "" {
					protocol, domain := parseRefer(refer)
					origin := protocol + "://" + domain

					if path, exist := conf.XDomain.Masters[protocol+"://"+domain]; exist {
						master := map[string]string{origin: path}

						jsonData, _ := json.MarshalIndent(master, "", "  ")

						html = strings.Replace(html, "{{#Masters#}}", string(jsonData), 1)
						return html
					}
				}

				html = strings.Replace(html, "{{#Masters#}}", "{}", 1)

				return html
			})
		}

		if conf.XDomain.LibPath != "" {
			r.Get(conf.XDomain.LibPath, func() string {
				return xdomainLib
			})
		}
	})

	receiver = jsonApiReceiver
	return
}