func (h HTTPSHandler) agentHandler(handlerFunc boshhandler.HandlerFunc) (agentHandler func(http.ResponseWriter, *http.Request)) {
	agentHandler = func(w http.ResponseWriter, r *http.Request) {
		if r.Method != "POST" {
			w.WriteHeader(404)
			return
		}

		if h.requestNotAuthorized(r) {
			w.Header().Add("WWW-Authenticate", `Basic realm=""`)
			w.WriteHeader(401)
			return
		}

		rawJSONPayload, err := ioutil.ReadAll(r.Body)
		if err != nil {
			err = bosherr.WrapError(err, "Reading http body")
			return
		}

		respBytes, _, err := boshhandler.PerformHandlerWithJSON(
			rawJSONPayload,
			handlerFunc,
			boshhandler.UnlimitedResponseLength,
			h.logger,
		)
		if err != nil {
			err = bosherr.WrapError(err, "Running handler in a nice JSON sandwhich")
			return
		}

		w.Write(respBytes)
	}
	return
}
示例#2
0
func (h natsHandler) Start(handlerFunc boshhandler.HandlerFunc) (err error) {
	connProvider, err := h.getConnectionInfo()
	if err != nil {
		err = bosherr.WrapError(err, "Getting connection info")
		return
	}

	err = h.client.Connect(connProvider)
	if err != nil {
		err = bosherr.WrapError(err, "Connecting")
		return
	}

	subject := fmt.Sprintf("agent.%s", h.settings.GetAgentId())

	h.client.Subscribe(subject, func(natsMsg *yagnats.Message) {
		respBytes, req, err := boshhandler.PerformHandlerWithJSON(natsMsg.Payload, handlerFunc, h.logger)
		if err != nil {
			err = bosherr.WrapError(err, "Running handler in a nice JSON sandwhich")
			return
		}

		h.client.Publish(req.ReplyTo, respBytes)
	})

	return
}
示例#3
0
func (h natsHandler) handleNatsMsg(natsMsg *yagnats.Message, handlerFunc boshhandler.HandlerFunc) {
	respBytes, req, err := boshhandler.PerformHandlerWithJSON(
		natsMsg.Payload,
		handlerFunc,
		responseMaxLength,
		h.logger,
	)
	if err != nil {
		h.logger.Error(natsHandlerLogTag, "Running handler: %s", err)
		return
	}

	if len(respBytes) > 0 {
		h.client.Publish(req.ReplyTo, respBytes)
	}
}