service

package
v1.9.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 12, 2023 License: MIT Imports: 18 Imported by: 0

Documentation

Overview

Package service provides utilities used for implementing services.

Index

Constants

View Source
const FromFirstRow = iota

FromFirstRow is a special value of SortingAndPagingParameters.StartFromRowSubQuery needed to bypass pagination completely and ignore 'from.*' parameters of the HTTP request.

Variables

View Source
var InsufficientAccessRightsError = ErrForbidden(errors.New("insufficient access rights"))

InsufficientAccessRightsError is an APIError to be returned when the has no access rights to perform an action.

View Source
var NoError = APIError{0, nil}

NoError is an APIError to be returned when there is no error.

Functions

func ConvertDBTimeToJSONTime

func ConvertDBTimeToJSONTime(data interface{}) string

ConvertDBTimeToJSONTime converts the DB datetime representation to RFC3339.

func ConvertMapFromDBToJSON

func ConvertMapFromDBToJSON(dbMap map[string]interface{}) map[string]interface{}

ConvertMapFromDBToJSON given a map that represents DB result data, converts it a map for rendering JSON so that: 1) all map keys with "__" are considered as paths in JSON (converts "User__ID":... to "user":{"id": ...}) 2) all map keys are converted to snake case 3) prefixes are stripped, values are converted to needed types accordingly.

func ConvertSliceOfMapsFromDBToJSON

func ConvertSliceOfMapsFromDBToJSON(dbMaps []map[string]interface{}) []map[string]interface{}

ConvertSliceOfMapsFromDBToJSON given a slice of maps that represents DB result data, converts it to a slice of maps for rendering JSON so that: 1) all maps keys with "__" are considered as paths in JSON (converts "User__ID":... to "user":{"id": ...}) 2) all maps keys are converted to snake case 3) prefixes are stripped, values are converted to needed types accordingly.

func CreationSuccess

func CreationSuccess(data interface{}) render.Renderer

CreationSuccess generated a success response for a POST creation.

func DeletionSuccess

func DeletionSuccess(data interface{}) render.Renderer

DeletionSuccess generated a success response for a DELETE deletion.

func MustNotBeError

func MustNotBeError(err error)

MustNotBeError panics if the error is not nil.

func NotFound

func NotFound(w http.ResponseWriter, r *http.Request)

NotFound is a basic HTTP handler which generates a 404 error.

func ParsePagingParameters

func ParsePagingParameters(r *http.Request, expectedFromFields map[string]FieldType) (map[string]interface{}, error)

ParsePagingParameters returns a map with values provided for paging in a request URL (none or all should be present).

func ParticipantIDFromContext

func ParticipantIDFromContext(ctx context.Context) int64

ParticipantIDFromContext retrieves a participant id set by the middleware from a context.

func ParticipantMiddleware

func ParticipantMiddleware(srv GetStorer) func(next http.Handler) http.Handler

ParticipantMiddleware is a middleware retrieving a participant from the request content. The participant id is the `as_team_id` parameter value if it is given or the user's `group_id` otherwise. If `as_team_id` is given, it should be an id of a team and the user should be a member of this team, otherwise the 'forbidden' error is returned.

func ResolveURLQueryGetBoolField

func ResolveURLQueryGetBoolField(httpReq *http.Request, name string) (bool, error)

ResolveURLQueryGetBoolField extracts a get-parameter of type bool (0 or 1) from the query, fails if the value is empty.

func ResolveURLQueryGetInt64Field

func ResolveURLQueryGetInt64Field(httpReq *http.Request, name string) (int64, error)

ResolveURLQueryGetInt64Field extracts a get-parameter of type int64 from the query.

func ResolveURLQueryGetInt64SliceField

func ResolveURLQueryGetInt64SliceField(req *http.Request, paramName string) ([]int64, error)

ResolveURLQueryGetInt64SliceField extracts from the query parameter of the request a list of integer separated by commas (',') returns `nil` for no IDs.

func ResolveURLQueryGetStringField

func ResolveURLQueryGetStringField(httpReq *http.Request, name string) (string, error)

ResolveURLQueryGetStringField extracts a get-parameter of type string from the query, fails if the value is empty.

func ResolveURLQueryGetStringSliceField

func ResolveURLQueryGetStringSliceField(req *http.Request, paramName string) ([]string, error)

ResolveURLQueryGetStringSliceField extracts from the query parameter of the request a list of strings separated by commas (',') returns `nil` the parameter is missing.

func ResolveURLQueryGetStringSliceFieldFromIncludeExcludeParameters

func ResolveURLQueryGetStringSliceFieldFromIncludeExcludeParameters(
	r *http.Request, fieldName string, knownValuesMap map[string]bool,
) ([]string, error)

ResolveURLQueryGetStringSliceFieldFromIncludeExcludeParameters extracts a list of values out from '<fieldName>_include'/'<fieldName>_exclude' request parameters:

  1. If none of '<fieldName>_include'/'<fieldName>_exclude' is present, all the known values are returned.
  2. If '<fieldName>_include' is present, then it becomes the result list.
  3. If '<fieldName>_exclude' is present, then we exclude all its values from the result list.

All values from both the request parameters are checked against the list of known values.

func ResolveURLQueryGetTimeField

func ResolveURLQueryGetTimeField(httpReq *http.Request, name string) (time.Time, error)

ResolveURLQueryGetTimeField extracts a get-parameter of type time.Time (rfc3339) from the query.

func ResolveURLQueryPathInt64Field

func ResolveURLQueryPathInt64Field(httpReq *http.Request, name string) (int64, error)

ResolveURLQueryPathInt64Field extracts a path element of type int64 from the query.

func ResolveURLQueryPathInt64SliceField

func ResolveURLQueryPathInt64SliceField(req *http.Request, paramName string) ([]int64, error)

ResolveURLQueryPathInt64SliceField extracts a list of integers separated by commas (',') from the query path of the request.

func ResolveURLQueryPathInt64SliceFieldWithLimit

func ResolveURLQueryPathInt64SliceFieldWithLimit(r *http.Request, paramName string, limit int) ([]int64, error)

ResolveURLQueryPathInt64SliceFieldWithLimit extracts a list of integers separated by commas (',') from the query path of the request applying the given limit.

func URLQueryPathHasField added in v1.9.0

func URLQueryPathHasField(httpReq *http.Request, name string) bool

URLQueryPathHasField checks whether a field is present in the query.

func UnchangedSuccess

func UnchangedSuccess(httpStatus int) render.Renderer

UnchangedSuccess generated a success response for a POST/PUT/DELETE action if no data have been modified.

func UpdateSuccess

func UpdateSuccess(data interface{}) render.Renderer

UpdateSuccess generated a success response for a PUT updating.

Types

type APIError

type APIError struct {
	HTTPStatusCode int
	Error          error
}

APIError represents an error as returned by this application. It works in tandem with AppHandler for easy handling of errors.

func ApplySortingAndPaging

func ApplySortingAndPaging(r *http.Request, query *database.DB, parameters *SortingAndPagingParameters) (*database.DB, APIError)

ApplySortingAndPaging applies ordering and paging according to the given parameters taking into the account the URL parameters 'from.*' if needed.

When `parameters.IgnoreSortParameter` is true, the 'sort' request parameter is ignored.

When `parameters.StartFromRowSubQuery` is set, the 'from.*' request parameters are ignored.

func ErrConflict

func ErrConflict(err error) APIError

ErrConflict is for errors caused by wrong resource state not allowing to perform the operation It results in a 409 Conflict.

func ErrForbidden

func ErrForbidden(err error) APIError

ErrForbidden is for errors caused by a lack of permissions globally or on a requested object It results in a 403 Forbidden.

func ErrInvalidRequest

func ErrInvalidRequest(err error) APIError

ErrInvalidRequest is for errors caused by invalid request input It results in a 400 Invalid request response.

func ErrNotFound

func ErrNotFound(err error) APIError

ErrNotFound is for errors caused by absence of a requested object It results in a 404 Not Found.

func ErrUnexpected

func ErrUnexpected(err error) APIError

ErrUnexpected is for internal errors (not supposed to fail) not directly caused by the user input It results in a 500 Internal Server Error response.

func ErrUnprocessableEntity

func ErrUnprocessableEntity(err error) APIError

ErrUnprocessableEntity is for errors caused by our inability to perform data modifications for some reason It results in a 422 Unprocessable Entity.

func GetParticipantIDFromRequest

func GetParticipantIDFromRequest(httpReq *http.Request, user *database.User, store *database.DataStore) (int64, APIError)

GetParticipantIDFromRequest returns `as_team_id` parameter value if it is given or the user's `group_id` otherwise. If `as_team_id` is given, it should be an id of a team and the user should be a member of this team, otherwise the 'forbidden' error is returned.

type AppHandler

type AppHandler func(http.ResponseWriter, *http.Request) APIError

AppHandler is a type that implements http.Handler and makes handling errors easier. When its method returns an error, it prints it to the logs and shows a JSON formatted error to the user.

func (AppHandler) ServeHTTP

func (fn AppHandler) ServeHTTP(w http.ResponseWriter, r *http.Request)

type Base

type Base struct {
	ServerConfig *viper.Viper
	AuthConfig   *viper.Viper
	DomainConfig []domain.ConfigItem
	TokenConfig  *token.Config
	// contains filtered or unexported fields
}

Base is the common service context data.

func (*Base) GetStore added in v1.3.0

func (srv *Base) GetStore(r *http.Request) *database.DataStore

GetStore returns a data store with the given request's context.

func (*Base) GetUser

func (srv *Base) GetUser(r *http.Request) *database.User

GetUser returns the authenticated user data from context.

func (*Base) ResolveWatchedGroupID

func (srv *Base) ResolveWatchedGroupID(httpReq *http.Request) (watchedGroupID int64, ok bool, apiError APIError)

ResolveWatchedGroupID returns the watched_group_id parameter (if given) and checks if the current user has rights to watch for its members.

func (*Base) SetGlobalStore added in v1.3.0

func (srv *Base) SetGlobalStore(store *database.DataStore)

SetGlobalStore sets the global store shared by all the request (should be called only once on start).

type ErrorResponse

type ErrorResponse struct {
	Response
	ErrorText string      `json:"error_text,omitempty"` // application-level error message, for debugging
	Errors    interface{} `json:"errors,omitempty"`     // form errors
}

ErrorResponse is an extension of the response for returning errors.

type FieldSortingParams

type FieldSortingParams struct {
	// ColumnName is a DB column name (should contain a table name as a prefix, e.g. "groups.id")
	ColumnName string
	// Nullable means that the field can be null
	Nullable bool
}

FieldSortingParams represents sorting parameters for one field.

type FieldType

type FieldType string

FieldType represents a type of tie-breaker field's value.

const (
	FieldTypeInt64  FieldType = "int64"
	FieldTypeBool   FieldType = "bool"
	FieldTypeString FieldType = "string"
	FieldTypeTime   FieldType = "time"
)

Value type of 'from.*' field in HTTP requests.

type GetStorer added in v1.3.0

type GetStorer interface {
	GetStore(r *http.Request) *database.DataStore
}

GetStorer is an interface allowing to get a data store bound to the context of the given request.

type QueryLimiter

type QueryLimiter struct {
	// contains filtered or unexported fields
}

QueryLimiter applies the limit parameter from an HTTP request to a given DB query (see QueryLimiter.Apply()).

func NewQueryLimiter

func NewQueryLimiter() *QueryLimiter

NewQueryLimiter creates a QueryLimiter with default settings:

  • default limit is 500,
  • maximum allowed limit is 1000.

func (*QueryLimiter) Apply

func (ql *QueryLimiter) Apply(r *http.Request, db *database.DB) *database.DB

Apply limits the number of records of the given DB query according to the `limit` request parameter.

func (*QueryLimiter) SetDefaultLimit

func (ql *QueryLimiter) SetDefaultLimit(defaultLimit int64) *QueryLimiter

SetDefaultLimit sets the default limit used when the 'limit' request parameter is missing.

func (*QueryLimiter) SetMaxAllowedLimit

func (ql *QueryLimiter) SetMaxAllowedLimit(maxAllowedLimit int64) *QueryLimiter

SetMaxAllowedLimit sets the maximum allowed limit.

type Response

type Response struct {
	HTTPStatusCode int         `json:"-"`
	Success        bool        `json:"success"`
	Message        string      `json:"message"`
	Data           interface{} `json:"data,omitempty"`
}

Response is used for generating non-data responses, i.e. on error or on POST/PUT/PATCH/DELETE request.

func (*Response) Render

func (resp *Response) Render(w http.ResponseWriter, r *http.Request) error

Render generates the HTTP response from Response.

type SortingAndPagingFields

type SortingAndPagingFields map[string]*FieldSortingParams

SortingAndPagingFields is a type of SortingAndPagingParameters.Fields (field_name -> params).

type SortingAndPagingParameters

type SortingAndPagingParameters struct {
	Fields       SortingAndPagingFields
	DefaultRules string
	TieBreakers  SortingAndPagingTieBreakers

	// If IgnoreSortParameter is true, the 'sort' parameter of the HTTP request is ignored
	IgnoreSortParameter bool

	// If StartFromRowSubQuery is not nil, 'from.*' parameters of the HTTP request are not analyzed,
	// the sub-query should provide tie-breaker fields instead.
	//
	// If StartFromRowSubQuery is FromFirstRow, pagination gets skipped.
	StartFromRowSubQuery interface{}
}

SortingAndPagingParameters represents sorting and paging parameters to apply.

type SortingAndPagingTieBreakers

type SortingAndPagingTieBreakers map[string]FieldType

SortingAndPagingTieBreakers is a type of SortingAndPagingParameters.TieBreakers (field_name -> type).

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL