Beispiel #1
0
func (h *DeQHTTPHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
	q, ok := mux.Vars(r)["queue"]
	if !ok {
		http.Error(w, "unkown queue", http.StatusBadRequest)
		return
	}

	ctx, cnl := context.WithTimeout(context.Background(), httpTimeout)
	defer cnl()

	d := Deque{Queue: Queue(q)}
	res, err := h.Hive.Sync(ctx, d)
	if err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}

	switch res := res.(type) {
	case Error:
		http.Error(w, res.Message, http.StatusInternalServerError)
	case Dequed:
		if err = json.NewEncoder(w).Encode(res.Task); err != nil {
			http.Error(w, err.Error(), http.StatusInternalServerError)
		}
	}
}
Beispiel #2
0
func (h *EnQHTTPHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
	q, ok := mux.Vars(r)["queue"]
	if !ok {
		http.Error(w, "unkown queue", http.StatusBadRequest)
		return
	}

	b, err := ioutil.ReadAll(r.Body)
	if err != nil {
		http.Error(w, "cannot read request body", http.StatusBadRequest)
		return
	}

	e := Enque{
		Queue: Queue(q),
		Body:  b,
	}
	ctx, cnl := context.WithTimeout(context.Background(), httpTimeout)
	defer cnl()

	res, err := h.Hive.Sync(ctx, e)
	if err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}

	switch res := res.(type) {
	case Error:
		http.Error(w, res.Message, http.StatusInternalServerError)
	case Enqued:
		fmt.Fprintf(w, "task %v enqueued\n", res.TaskID)
	}
}
Beispiel #3
0
func (s *KVStore) ServeHTTP(w http.ResponseWriter, r *http.Request) {
	k, ok := mux.Vars(r)["key"]
	if !ok {
		http.Error(w, "no key in the url", http.StatusBadRequest)
		return
	}

	ctx, cnl := context.WithTimeout(context.Background(), 30*time.Second)
	var res interface{}
	var err error
	switch r.Method {
	case "GET":
		res, err = s.Hive.Sync(ctx, Get(k))
	case "PUT":
		var v []byte
		v, err = ioutil.ReadAll(r.Body)
		if err != nil {
			break
		}
		res, err = s.Hive.Sync(ctx, Put{Key: k, Val: string(v)})
	case "DELETE":
		res, err = s.Hive.Sync(ctx, Del(k))
	}
	cnl()

	if err != nil {
		switch {
		case err.Error() == errKeyNotFound.Error():
			http.Error(w, err.Error(), http.StatusNotFound)
			return
		case err.Error() == errInternal.Error():
			http.Error(w, err.Error(), http.StatusInternalServerError)
			return
		case err.Error() == errInvalid.Error():
			http.Error(w, err.Error(), http.StatusBadRequest)
			return
		}
	}

	if res == nil {
		return
	}

	js, err := json.Marshal(res)
	if err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}
	w.Header().Set("Content-Type", "application/json")
	w.Write(js)
}
Beispiel #4
0
func (h *HelloHTTPHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
	vars := mux.Vars(r)
	name, ok := vars["name"]
	if !ok {
		http.Error(w, "no name", http.StatusBadRequest)
		return
	}

	res, err := beehive.Sync(context.TODO(), HelloHTTP{Name: name})
	if err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}

	fmt.Fprintf(w, "hello %s (%d)\n", name, res.(int))
}
Beispiel #5
0
func defaultHTTPHandler(w http.ResponseWriter, r *http.Request, h bh.Hive) {
	w.Header().Set("Server", "Beehive-netctrl-HTTP-Server")

	vars := mux.Vars(r)

	submodule, ok := vars["submodule"]
	if !ok {
		/* This should not happened :) */
		http.Error(w, "Invalid request", http.StatusBadRequest)
		return
	}

	verb, ok := vars["verb"]
	if !ok {
		/* This should not happened :) */
		http.Error(w, "Invalid request", http.StatusBadRequest)
		return
	}

	creq := HTTPRequest{
		AppName: submodule,
		Verb:    verb,
	}

	// Read content data if available :)
	if r.ContentLength > 0 {
		data, err := ioutil.ReadAll(r.Body)
		if err == nil {
			creq.Data = data
		}
	}

	cres, err := h.Sync(context.TODO(), creq)
	if err == nil && cres != nil {
		w.Write(cres.(HTTPResponse).Data)
		w.WriteHeader(http.StatusOK)
		return
	} else {
		http.Error(w, err.Error(), http.StatusInternalServerError)
		fmt.Errorf("defaultTTPHandler: %v\n", err)
		return
	}

}
func (httpReceiver *HTTPReceiver) ServeHTTP(
	responseWriter http.ResponseWriter,
	httpRequest *http.Request) {
	logger.Info.Printf("[HTTPReceiver] ServeHTTP %s %s \n",
		httpRequest.Method,
		httpRequest.URL)
	vars := mux.Vars(httpRequest)
	destinationBee, ok := vars["destinationBee"]
	if !ok {
		BadRequest(responseWriter, "No destinationBee")
		return
	}

	fibNumberStr, ok := vars["fibNumber"]
	if !ok {
		BadRequest(responseWriter, "No fibNumber")
		return
	}

	fibNumber, err := strconv.Atoi(fibNumberStr)
	if err != nil {
		BadRequest(responseWriter, "FibNumber must be number")
		return
	}

	message := MessageToBee{
		DestinationBee: destinationBee,
		FibNumber:      fibNumber,
	}
	logger.Trace.Printf("[HTTPReceiver] Message to bee %+v \n", message)

	beeRespond, err := beehive.Sync(context.TODO(), message)
	if err != nil {
		logger.Error.Printf("[HTTPReceiver] %s \n", err.Error())
		http.Error(responseWriter, err.Error(), http.StatusInternalServerError)
		return
	}

	fmt.Fprintf(responseWriter, "%d", beeRespond.(int))
	logger.Trace.Println("[HTTPReceiver] Done sending message to bee")
}
Beispiel #7
0
func (h *AckHTTPHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
	vars := mux.Vars(r)
	q, ok := vars["queue"]
	if !ok {
		http.Error(w, "unkown queue", http.StatusBadRequest)
		return
	}

	t, ok := vars["id"]
	if !ok {
		http.Error(w, "unkown task", http.StatusBadRequest)
		return
	}

	id, err := strconv.ParseUint(t, 10, 64)
	if err != nil {
		http.Error(w, fmt.Sprintf("invalid task id: %v", err),
			http.StatusBadRequest)
		return
	}

	ctx, cnl := context.WithTimeout(context.Background(), httpTimeout)
	defer cnl()

	a := Ack{
		TaskID: TaskID(id),
		Queue:  Queue(q),
	}
	res, err := h.Hive.Sync(ctx, a)
	if err != nil {
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}

	switch res := res.(type) {
	case Error:
		http.Error(w, res.Message, http.StatusInternalServerError)
	case Acked:
		fmt.Fprintf(w, "task %v acknowledged\n", id)
	}
}