Documentation
¶
Index ¶
- Constants
- Variables
- func Authenticate(source string, h *HandlerConfig, c *fiber.Ctx) (repo.User, error)
- func CRUDErrorHandler(log *zap.Logger) fiber.ErrorHandler
- func DecodeJWT(tokenString string) (jwt.MapClaims, error)
- func EnsureAdmin(source string, h *HandlerConfig, c *fiber.Ctx) error
- func GenerateToken(userID int32) (string, error)
- func GetJWTFromHeader(c *fiber.Ctx, authScheme string) (string, error)
- func GetPagination(c *fiber.Ctx) (page int, limit int, offset int)
- func GetPaginationNoLimit(c *fiber.Ctx) (page int, limit int, offset int)
- func GetUserIDFromJWT(c *fiber.Ctx) int32
- func NewCreateAdminJSONHandler[T CReq[U], U CRepo, R CRes](c *fiber.Ctx, h *HandlerConfig, source string, ...) error
- func NewDeleteAdminJSONHandler(c *fiber.Ctx, h *HandlerConfig, source string, ...) error
- func NewUpdateAdminJSONHandler[T UReq[U], U URepo](c *fiber.Ctx, h *HandlerConfig, source string, ...) error
- func SendOTP(mobile string, logger *zap.Logger) error
- func SetupS3Client(accessKey, secretKey, endpoint string) *s3.Client
- func UploadToS3(ctx context.Context, client *s3.Client, file multipart.File, fileName string, ...) error
- func ValidateOTP(mobile, otp string) bool
- type CRUDError
- func NewCRUDError(code int, message string, source string, internalMessage string, ...) *CRUDError
- func NewCreateError(source string, err error, args ...string) *CRUDError
- func NewDBError(source string, err error, args ...string) *CRUDError
- func NewDeleteError(source string, err error, args ...string) *CRUDError
- func NewInvalidTokenError(source string, internalMessage string) *CRUDError
- func NewJSONError(source string, err error, args ...string) *CRUDError
- func NewNotFoundError(source string, err error, args ...string) *CRUDError
- func NewPermissionError(source string, internalMessage string, args ...string) *CRUDError
- func NewUpdateError(source string, err error, args ...string) *CRUDError
- func NewValidationError(source string, err error, args ...string) *CRUDError
- type CRepo
- type CReq
- type CRes
- type Config
- type ConfigOption
- type HandlerConfig
- type Meta
- type OTP
- type OTPStore
- type Res
- type ResWithMeta
- type URepo
- type UReq
Constants ¶
const ( // ERROR MESSAGES InternalServerError = "خطای داخلی سرور" InvalidJsonBody = "بدنه ی درخواست معتبر نیست" InvalidRequest = "درخواست شما معتبر نیست" CreateError = "در ایجاد ایتم مورد نظر خطایی رخ داد" DeleteError = "در حذف ایتم مورد نظر خطایی رخ داد" NoDataFound = "داده ای یافت نشد" BadPerson = "ای ناقلا 🤡🤡🤡 این کارا چیه؟" IncorrectOTP = "کد احراز هویت اشتباه می باشد" InvalidToken = "توکن نامعتبر است" PermissionNotAllowed = "شما به این قسمت دسترسی ندارید" UploadError = "خطا در آپلود فایل" UpdateError = "خطایی در بروزرسانی داده رخ داد" UpdateDoneButImageFailed = "" /* 126-byte string literal not displayed */ DataNotUnique = "داده ای با این مشخصات وجود دارد" ImageNotFound = "تصویری یافت نشد" // Success = "عملیات موفقیت آمیز" SendingOTP = "کد احراز هویت با موفقیت ارسال شد" Wait2Minutes = "لطفا 2 دقیقه دیگر امتحان کنید" EmptyCart = "سبد خرید خالی است" ProductOutOfStock = "محصول موجود نمی باشد" Friend = "دوست" )
Variables ¶
var ErrNotFound error = errors.New("no data found")
Some Simple errors
var GoCRUDConfig = SetConfig()
GoCRUDConfig is the default config You should set this to your needs in your main file Look at the main file in the example
Functions ¶
func Authenticate ¶
func Authenticate(source string, h *HandlerConfig, c *fiber.Ctx) (repo.User, error)
This is Very usefull for authenticating the user Usefull for most crud applications
func CRUDErrorHandler ¶
This function for handling errors that are returned from the handlers Just use it
func EnsureAdmin ¶
func EnsureAdmin(source string, h *HandlerConfig, c *fiber.Ctx) error
EnsureAdmin checks if the user is admin You can make this in a middleware This is just an example for a system who has admin role You can use it as a template for your system and roles
func GenerateToken ¶
This is for generating tokens not much to explain
func GetJWTFromHeader ¶
This is for getting the jwt from header not much to explain
func GetPagination ¶
GetPagination returns page, limit and offset
func GetPaginationNoLimit ¶
GetPaginationNoLimit returns page, limit and offset Use in rare cases that user wants to get alot of data
func GetUserIDFromJWT ¶
func GetUserIDFromJWT(c *fiber.Ctx) int32
For cases that a request can contain or not contain a token Use this to get the user id
func SetupS3Client ¶
This is for setting up the db call this in the main file and set it to gocrud.GoCRUDConfig Object
Types ¶
type CRUDError ¶
type CRUDError struct { Code int `json:"code"` Message string `json:"message"` Source string `json:"-"` InternalMessage string `json:"-"` Args []string `json:"-"` }
func NewCRUDError ¶
func NewCRUDError(code int, message string, source string, internalMessage string, args ...string) *CRUDError
Only use when there is no error and want to create a new error I barely use this one
func NewDBError ¶
------------------------------------------------------------------ ------------------- Database Errors ------------------------------ ------------------------------------------------------------------
func NewInvalidTokenError ¶
func NewJSONError ¶
------------------------------------------------------------------ ---------------- JSON and Validation Errors ---------------------- ------------------------------------------------------------------
func NewNotFoundError ¶
Used for 404
func NewPermissionError ¶
------------------------------------------------------------------ ------------------- Other Errors --------------------------------- ------------------------------------------------------------------
func NewValidationError ¶
type Config ¶
type Config struct {
// contains filtered or unexported fields
}
Config for GoCRUD
func SetConfig ¶
func SetConfig(options ...ConfigOption) *Config
SetConfig sets the config Look at the main file in the example
type ConfigOption ¶
type ConfigOption func(*Config)
func WithAuthSecret ¶
func WithAuthSecret(authSecret string) ConfigOption
func WithLanguage ¶
func WithLanguage(language string) ConfigOption
func WithOtpApiKey ¶
func WithOtpApiKey(otpApiKey string) ConfigOption
func WithS3Client ¶
func WithS3Client(s3Client *s3.Client, bucketName string) ConfigOption
type HandlerConfig ¶
Example of what a handler config should look like It is not recommanded to use this just write your own
func NewHandlerConfig ¶
func NewHandlerConfig(db *repo.Queries, logger *zap.Logger) *HandlerConfig
Creates a new handler config This is not recommanded to use this just write your own
type Meta ¶
type Res ¶
type Res[T any] struct { Data T `json:"data"` Message string `json:"message"` Status int `json:"status"` }
Standard response suitable for most handlers
type ResWithMeta ¶
type ResWithMeta[T any] struct { Data T `json:"data"` Message string `json:"message"` Status int `json:"status"` Meta Meta `json:"meta,omitempty"` }
Standard response suitable for handlers who do pagination In go 1.24 when the new omitzero json tag will be introduced we can merge the 2 responses probably
func NewResWithMeta ¶
func NewResWithMeta[T any](data T, message string, status int, meta Meta) ResWithMeta[T]