api

package
v0.0.4-rc Latest Latest
Warning

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

Go to latest
Published: Jun 4, 2023 License: Apache-2.0 Imports: 9 Imported by: 0

Documentation

Index

Constants

View Source
const (
	MaxUploadSize   = 2 * (1 << 20) // 2MB
	DefaultBodySize = 1 << 18       // 256KB

)

Variables

View Source
var (
	ErrNotFound      = errors.New("not found")
	ErrNotAZip       = errors.New("file is not a valid zip")
	ErrDuplicatedKey = errors.New("already exists")
	ErrInvalidParam  = errors.New("invalid param")
)

Functions

func Duplicated

func Duplicated(v []string) bool

func FromJsonBody

func FromJsonBody[T Validable](r io.ReadCloser) (T, error)

func FromUrlParams

func FromUrlParams[T Parseable[T]](r *http.Request, name string) (T, error)

func FromUrlQuery

func FromUrlQuery[T Parseable[T]](r *http.Request, name string, fallback T) (T, error)

func HandlerFunc

func HandlerFunc(f ApiFunction) http.HandlerFunc

func MakeHttpError

func MakeHttpError(err error) error

func MakeServiceError

func MakeServiceError(err error) error

func WithInterval

func WithInterval(i IntervalParams, column string) func(db *gorm.DB) *gorm.DB

func WithMaximumBodySize

func WithMaximumBodySize(n int64) func(http.Handler) http.Handler

func WithPagination

func WithPagination(p PaginationParams) func(db *gorm.DB) *gorm.DB

func WriteJson

func WriteJson(w http.ResponseWriter, statusCode int, v any) error

Types

type ApiError

type ApiError struct {
	Message string `json:"message"`
	// contains filtered or unexported fields
}

ApiError represents the http error returned by the REST service. Implements error interface.

func (ApiError) Error

func (ae ApiError) Error() string

type ApiFunction

type ApiFunction func(http.ResponseWriter, *http.Request) error

type IntervalParams

type IntervalParams struct {
	Start time.Time
	End   time.Time
}

type PaginatedResponse

type PaginatedResponse struct {
	Data     any                `json:"data"`
	Metadata PaginationMetadata `json:"metadata"`
}

func MakePaginatedResponse

func MakePaginatedResponse(v any, count int64, p PaginationParams) *PaginatedResponse

type PaginationMetadata

type PaginationMetadata struct {
	HasNext  bool  `json:"hasNext"`
	Count    int64 `json:"count"`
	Page     int64 `json:"page"`
	PageSize int64 `json:"pageSize"`
}

type PaginationParams

type PaginationParams struct {
	Page     int64
	PageSize int64
}

type Parseable

type Parseable[T any] interface {
	Parse(s string) (T, error)
}

type Validable

type Validable interface {
	Validate() error
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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