common

package module
v0.0.0-...-652736d Latest Latest
Warning

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

Go to latest
Published: Apr 24, 2023 License: MIT Imports: 13 Imported by: 0

README

go-common

Sharing is caring

Documentation

Index

Constants

View Source
const PermissionsContextKey contextKey = "permissions"
View Source
const PermissionsKey key = 123234

Variables

View Source
var (
	ErrRequiredField     error = errors.New("*Required field")
	ErrUnsupportedValue  error = errors.New("unsupported Value")
	ErrRecordDoesntExist error = errors.New("record does not exist")
	ErrNotImplemented    error = errors.New("Endpoint not implemented")
	ErrBadJSONBody       error = errors.New("unable to parse body, exptected json")
)
View Source
var ErrIncorrectPermissions error = errors.New("incorrect permissions, missing group or role to perform this action")

Functions

func AUTH

func AUTH(next http.Handler, secret string) http.Handler

func CORS

func CORS(next http.Handler) http.Handler

func CreateJWTToken

func CreateJWTToken(secret string, expirationDelta time.Duration) (string, error)

func LOGS

func LOGS(next http.Handler) http.Handler

func MustHaveGroupRole

func MustHaveGroupRole(next http.Handler, groupID, roleID int) http.Handler

func ParseURLParamInt64

func ParseURLParamInt64(req *http.Request, param string) (int64, error)

This function parses the request URL and returns the parameter given by name in param. E.g. /project/{PprojectID} --> "ProjecID" is the URL param

func SetPermissions

func SetPermissions(r *http.Request, permissions []GroupRoles)

Types

type AppHandler

type AppHandler func(*http.Request) http.Handler

func (AppHandler) ServeHTTP

func (fn AppHandler) ServeHTTP(rw http.ResponseWriter, req *http.Request)

type FieldError

type FieldError struct {
	Field   string `json:"field,omitempty"`
	Message string `json:"message,omitempty"`
}

type FieldErrors

type FieldErrors []FieldError

func (*FieldErrors) Append

func (e *FieldErrors) Append(field, message string)

type GroupRoles

type GroupRoles struct {
	GroupID int   `json:"groupID"`
	RoleIDs []int `json:"roleIDs"`
}

func GetPermissions

func GetPermissions(req *http.Request) []GroupRoles

type ID

type ID struct {
	Int64  int64
	String string
	Valid  bool
}

type NullBool

type NullBool struct {
	sql.NullBool
}

func (NullBool) MarshalJSON

func (n NullBool) MarshalJSON() ([]byte, error)

func (*NullBool) UnmarshalJSON

func (n *NullBool) UnmarshalJSON(data []byte) error

type NullFloat64

type NullFloat64 struct {
	sql.NullFloat64
}

func (NullFloat64) MarshalJSON

func (n NullFloat64) MarshalJSON() ([]byte, error)

func (*NullFloat64) UnmarshalJSON

func (n *NullFloat64) UnmarshalJSON(data []byte) error

type NullInt64

type NullInt64 struct {
	sql.NullInt64
}

func (NullInt64) MarshalJSON

func (n NullInt64) MarshalJSON() ([]byte, error)

func (*NullInt64) UnmarshalJSON

func (n *NullInt64) UnmarshalJSON(data []byte) error

type NullString

type NullString struct {
	sql.NullString
}

func NewNullString

func NewNullString(value string, valid bool) NullString

func (NullString) MarshalJSON

func (n NullString) MarshalJSON() ([]byte, error)

func (*NullString) Set

func (n *NullString) Set(value string)

func (*NullString) UnmarshalJSON

func (n *NullString) UnmarshalJSON(data []byte) error

type NullTime

type NullTime struct {
	Time  time.Time
	Valid bool
}

func (NullTime) MarshalJSON

func (n NullTime) MarshalJSON() ([]byte, error)

func (*NullTime) Scan

func (n *NullTime) Scan(src interface{}) error

func (*NullTime) UnmarshalJSON

func (n *NullTime) UnmarshalJSON(data []byte) error

type Response

type Response struct {
	Status   bool                `json:"success"`
	Type     ResponseType        `json:"-"`
	Result   interface{}         `json:"response,omitempty"`
	Error    string              `json:"error,omitempty"`
	Errors   FieldErrors         `json:"errors,omitempty"`
	Updated  *SubResponseUpdated `json:"updated,omitempty"`
	Deleted  *SubResponseDeleted `json:"deleted,omitempty"`
	Paged    *SubResponsePaged   `json:"paged,omitempty"`
	File     *os.File            `json:"-"`
	FileName string              `json:"-"`
}

func BadRequestErrorsResponse

func BadRequestErrorsResponse(errors FieldErrors) Response

func BadRequestResponse

func BadRequestResponse(err error) Response

func DeletedResponse

func DeletedResponse(count int64) Response

func FileDownloadResponse

func FileDownloadResponse(file *os.File, filename string) Response

func InternalErrorResponse

func InternalErrorResponse(err error) Response

func PagedResponse

func PagedResponse(result interface{}, page, per_page, total int64) Response

func SuccessResult

func SuccessResult(result interface{}) Response

func UnauthorisedResponse

func UnauthorisedResponse(err error) Response

func UpdatedResponse

func UpdatedResponse(count int64) Response

func UserErrorResponse

func UserErrorResponse(err error) Response

func (Response) ServeHTTP

func (r Response) ServeHTTP(rw http.ResponseWriter, req *http.Request)

type ResponseType

type ResponseType int
const (
	RESPOND_OK ResponseType = iota
	RESPOND_BAD_REQUEST
	RESPOND_INTERNAL_ERROR
	RESPOND_USER_ERROR
	RESPOND_DELETED
	RESPOND_UPDATED
	RESPOND_PAGED
	RESPOND_FILE_DOWNLOAD
	RESPOND_UNAUTHORISED
)

type SubResponseDeleted

type SubResponseDeleted struct {
	Count int64 `json:"count"`
}

type SubResponsePaged

type SubResponsePaged struct {
	Page    int64 `json:"page"`
	PerPage int64 `json:"perPage"`
	Total   int64 `json:"totalCount"`
}

type SubResponseUpdated

type SubResponseUpdated struct {
	Count int64 `json:"count"`
}

Jump to

Keyboard shortcuts

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