예제 #1
0
func ValidateEndpoint(feature features.Features) endpoint.Endpoint {
	return func(ctx context.Context, request interface{}) (interface{}, error) {
		req := request.(validationRequest)

		ff, err := feature.Find(req.Key)
		if err != nil {
			return HTTPResponse{StatusCode: http.StatusForbidden}, nil
		}

		var access bool
		if ff.Percentage > 0 || len(ff.Users) > 0 {
			if access = feature.UserHasAccess(req.Key, req.User); access {
				return HTTPResponse{StatusCode: http.StatusOK}, nil
			} else {
				return HTTPResponse{StatusCode: http.StatusForbidden}, nil
			}
		}

		if access, _ = feature.IsEnabled(req.Key); access {
			return HTTPResponse{StatusCode: http.StatusOK}, nil
		}

		return HTTPResponse{StatusCode: http.StatusForbidden}, nil
	}
}
예제 #2
0
func FindEndpoint(feature features.Features) endpoint.Endpoint {
	return func(ctx context.Context, feature_key interface{}) (interface{}, error) {
		fk := feature_key.(string)
		ff, err := feature.Find(fk)
		if err != nil {
			return nil, err
		}

		return HTTPResponse{StatusCode: http.StatusOK, Body: ff}, nil
	}
}
예제 #3
0
func UpdateEndpoint(feature features.Features) endpoint.Endpoint {
	return func(ctx context.Context, request interface{}) (interface{}, error) {
		req := request.(featureFlagRequest)
		ff := req.FeatureFlag

		if ff, err := feature.Find(ff.Key); ff == nil {
			return nil, NewNotFoundError(err)
		}

		err := feature.Save(ff)
		if err != nil {
			return nil, err
		}

		return HTTPResponse{StatusCode: http.StatusOK, Body: ff}, nil
	}
}
예제 #4
0
func CreateEndpoint(feature features.Features) endpoint.Endpoint {
	return func(ctx context.Context, request interface{}) (interface{}, error) {
		req := request.(featureFlagRequest)
		ff := req.FeatureFlag

		if ff, _ := feature.Find(ff.Key); ff != nil {
			errResp := NewErrorResponse(E_BAD_REQUEST, "There's another feature for the same key value.")
			return HTTPResponse{StatusCode: http.StatusBadRequest, Body: errResp}, nil
		}

		err := feature.Save(ff)
		if err != nil {
			return nil, err
		}

		return HTTPResponse{StatusCode: http.StatusCreated, Body: ff}, nil
	}
}