utils

package
v0.0.0-...-1ab3394 Latest Latest
Warning

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

Go to latest
Published: Jan 3, 2025 License: AGPL-3.0 Imports: 19 Imported by: 0

Documentation

Overview

pkg/utils/context.go

Index

Constants

View Source
const (
	ErrAuthenticationKeyNotFound = "authentication_key_not_found"
	ErrUnauthorized              = "unauthorized"
	ErrTokenExpired              = "token_expired"
)

Authentication errors

View Source
const (
	ErrBadRequest     = "bad_request"
	ErrUserIDNotFound = "user_id_not_found"
)

Request errors

View Source
const (
	ErrInvalidUsernameOrEmail = "invalid_username_or_email"
	ErrInvalidPassword        = "invalid_password"
	ErrEmailAlreadyUsed       = "email_already_used"
	ErrUsernameAlreadyUsed    = "username_already_used"
	ErrEmailNotVerify         = "email_not_verify"
)

User-related errors

View Source
const (
	ErrDuplicateCourseModule = "duplicate_courses_module"
	ErrMissingCourseID       = "missing_course_id"
	ErrCourseNotExist        = "course_not_exist"

	ErrImageRequired      = "image_required"
	ErrImageTooLarge      = "image_too_large"
	ErrOpeningImage       = "opening_image_failed"
	ErrReadingImage       = "reading_image_failed"
	ErrInvalidImage       = "invalid_image"
	ErrGeneratingFilePath = "generating_file_path_failed"
	ErrResetFilePointer   = "reset_file_pointer_failed"
	ErrS3UploadFailed     = "s3_upload_failed"

	ErrRivisionNotExist = "revision_not_exist"
	ErrAlreadyMerged    = "revision_already_merged"
)

Courses-releated errors

View Source
const (
	ErrSaveData   = "error_save_data"
	ErrGetData    = "error_get_data"
	ErrDeleteData = "error_delete_data"
)

Database errors

View Source
const (
	ErrHashData        = "hash_data_failed"
	ErrParseFile       = "template_parse_failed"
	ErrParse           = "data_parse_failed"
	ErrUnmarshal       = "data_unmarshal_failed"
	ErrSendEmail       = "send_email_failed"
	ErrGenerateSession = "generate_session_failed"
	ErrParseData       = "parse_data_failed"
	ErrGenerateToken   = "generate_token_failed"
	ErrExecuteTemplate = "execute_template_failed"
	ErrStoreRedis      = "store_redis_failed"
	ErrChangeType      = "change_type_failed"
)

Internal errors

View Source
const (
	ErrCreateNewCourse = "create_new_course_failed"
	ErrSaveCourseFile  = "save_new_course_file_failed"
)

Gitea errors

Variables

View Source
var (
	// Unit is days
	CookieRefreshTokenExpires int
	// Unit is minutes
	CookieAccessTokenExpires int
	BackendURL               string
	FrontendURl              string
	GiteaORGName             string
	GiteaCommitEmail         string
)
View Source
var LangList = []string{"en", "es", "zh-tw", "zh-cn"}
View Source
var (
	RootPath = filepath.Join(filepath.Dir(b), "../..")
)

Functions

func Atoi

func Atoi(value string) int

Return string to int and ignore error

func BoolPtr

func BoolPtr(value bool) *bool

func ConvertToStringIfNeeded

func ConvertToStringIfNeeded(input interface{}) (interface{}, error)

func FullyResponse

func FullyResponse(c *gin.Context, statusCode int, message string, errorCode interface{}, data interface{})

Fully response

func GenerateUserSession

func GenerateUserSession(c *gin.Context, userID uint64) error

Generate new user access_token and refresh_token

func GetUserIDFromContext

func GetUserIDFromContext(c *gin.Context) (uint64, error)

GetUserIDFromContext retrieves the user ID from the request context.

func IsValidImageType

func IsValidImageType(magic []byte) (bool, string, error)

Check if the uploaded file is an image using Magic Bytes

func SendEmail

func SendEmail(to string, subject string, body string) error

func ServerErrorResponse

func ServerErrorResponse(c *gin.Context, statusCode int, message string, errorCode string, err error)

ServerErrorResponse is a helper function to handle errors and send responses

func StrToUint64

func StrToUint64(str string) (uint64, error)

func StrToUint64NoError

func StrToUint64NoError(str string) uint64

func Uint64ToStr

func Uint64ToStr(id uint64) string

func Uint64ToStrPtr

func Uint64ToStrPtr(id uint64) *string

Types

type LangLocal

type LangLocal string
const (
	English   LangLocal = "en"
	Spanish   LangLocal = "es"
	ChineseTW LangLocal = "zh-tw"
	ChineseCN LangLocal = "zh-cn"
)

Jump to

Keyboard shortcuts

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