Exemplo n.º 1
0
func DeleteEndpoint(feature features.Features) endpoint.Endpoint {
	return func(ctx context.Context, feature_key interface{}) (interface{}, error) {
		fk := feature_key.(string)
		err := feature.Delete(fk)
		if err != nil {
			return nil, err
		}

		return HTTPResponse{StatusCode: http.StatusNoContent}, nil
	}
}
Exemplo n.º 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
	}
}
Exemplo n.º 3
0
func ListEndpoint(feature features.Features) endpoint.Endpoint {
	return func(ctx context.Context, feature_key interface{}) (interface{}, error) {

		ffs, err := feature.List()
		if err != nil {
			return nil, err
		}

		cs := &CollectionSerializer{
			Items: ffs,
			Count: len(ffs),
		}
		return HTTPResponse{StatusCode: http.StatusOK, Body: cs}, nil
	}
}
Exemplo n.º 4
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
	}
}
Exemplo n.º 5
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
	}
}
Exemplo n.º 6
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
	}
}