Exemplo n.º 1
0
func success(ctx echo.Context, data interface{}) error {
	result := map[string]interface{}{
		"ok":   1,
		"msg":  "操作成功",
		"data": data,
	}

	b, err := json.Marshal(result)
	if err != nil {
		return err
	}

	go func(b []byte) {
		if cacheKey := ctx.Get(nosql.CacheKey); cacheKey != nil {
			nosql.DefaultLRUCache.CompressAndAdd(cacheKey, b, nosql.NewCacheData())
		}
	}(b)

	if ctx.Response().Committed() {
		getLogger(ctx).Flush()
		return nil
	}

	return ctx.JSONBlob(http.StatusOK, b)
}
Exemplo n.º 2
0
func appDelete(c echo.Context) error {
	//arg := new(types.DeleteParams)
	//reply := new(types.Event)

	data := []byte{}
	fmt.Println(data)
	return c.JSONBlob(http.StatusOK, data)
}
Exemplo n.º 3
0
Arquivo: create.go Projeto: zqzca/back
// Create logs a user in
func (s Controller) Create(e echo.Context) error {
	session := &userSession{}

	if err := e.Bind(session); err != nil {
		return err
	}

	user, err := models.Users(s.DB, qm.Select("hash"), qm.Where("username=$1", session.Username)).One()
	if err != nil {
		s.Error("failed to fetch user", "err", err)
		return e.NoContent(http.StatusInternalServerError)
	}

	if err := bcrypt.CompareHashAndPassword([]byte(user.Hash), []byte(session.Password)); err != nil {
		return e.JSONBlob(http.StatusUnauthorized, errInvalidCredentials)
	}

	return e.NoContent(http.StatusOK)
}
Exemplo n.º 4
0
func (ee *EventEndpoints) Get(c echo.Context) error {
	eventID := c.Param("sg_event_id")

	cachedResponse := ee.CacheAdaptor.GetEvent(eventID)
	if cachedResponse != "" {
		log.Println("returning cached response")

		return c.JSONBlob(http.StatusOK, []byte(cachedResponse))
	}

	event, err := ee.DBAdaptor.GetEvent(eventID)
	if err != nil {
		log.Println("error returning data from cassandra: " + err.Error())

		return c.JSON(http.StatusInternalServerError, ControllerError{
			Message: "Something went wrong retrieving data from storage",
		})
	}

	return c.JSON(http.StatusOK, event)
}
Exemplo n.º 5
0
func appPatch(c echo.Context) error {
	data := []byte{}
	fmt.Println(data)
	return c.JSONBlob(http.StatusOK, data)
}
Exemplo n.º 6
0
func (ee *EventsEndpoints) Get(c echo.Context) error {
	headers := c.Response().Header()
	headers.Set("Cache-Control", "private, max-age=600")

	var nextPage string

	queryString := []byte(c.Request().URL().QueryString())

	cachedResponse := ee.CacheAdaptor.GetEventRequest(queryString)
	if cachedResponse != "" {
		log.Println("returning cached response")

		return c.JSONBlob(http.StatusOK, []byte(cachedResponse))
	}

	offsetID := c.QueryParam("offset_id")
	limit, err := strconv.Atoi(c.QueryParam("limit"))
	if err != nil || limit == 0 {
		limit = 50
	}

	fieldName := c.QueryParam("field_name")

	var events []database.Event
	var getErr error

	if fieldName == Timestamp {
		startValue := c.QueryParam("start_date")
		if startValue == "" {
			return c.JSON(http.StatusBadRequest, ControllerError{
				Message: "Start date is required for fieldValue = day",
			})
		}
		startDate, timeErr := time.Parse(date, startValue)
		if timeErr != nil {
			log.Println("Could not convert start date: " + timeErr.Error())

			return c.JSON(http.StatusBadRequest, ControllerError{
				Message: "Could not convert start date ",
			})
		}

		endValue := c.QueryParam("end_date")
		if endValue != "" {
			endDate, timeErr := time.Parse(date, endValue)
			if timeErr != nil {
				log.Println("Could not convert end date: " + timeErr.Error())

				return c.JSON(http.StatusBadRequest, ControllerError{
					Message: "Could not convert end date ",
				})
			}
			events, getErr = ee.DBAdaptor.GetEventsInInterval(startDate, endDate, limit, offsetID)

		} else {
			events, getErr = ee.DBAdaptor.GetEventsInInterval(startDate, time.Now(), limit, offsetID)
		}
	} else {
		fieldValue := c.QueryParam("field_value")
		events, getErr = ee.DBAdaptor.GetEvents(fieldName, fieldValue, limit, offsetID)

		if len(events) == limit-1 {
			nextPage = fmt.Sprintf("%s%s?field_name=%s&field_value=%s&limit=%d&offset_id=%s", URL, c.Get("route"), fieldName, fieldValue, limit, events[limit-1].SGEventID)
		}
	}

	if getErr != nil {
		log.Println("error returning data from cassandra: " + getErr.Error())

		return c.JSON(http.StatusInternalServerError, ControllerError{
			Message: "Something went wrong retrieving data from storage",
		})
	}

	var response common.ListResponse

	if len(events) < limit {
		response = common.ListResponse{
			Events: events}
	} else {
		response = common.ListResponse{
			Events:   events,
			NextPage: nextPage,
		}
	}

	ee.CacheAdaptor.SaveEventRequest(queryString, response)

	return c.JSON(http.StatusOK, response)
}