common

package
v0.44.5 Latest Latest
Warning

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

Go to latest
Published: Oct 29, 2024 License: MIT Imports: 7 Imported by: 0

Documentation

Overview

Common functions, constants, and structures package for the backend.

Index

Constants

View Source
const (
	HDR_PAGE_NO      = "X-Page-No"
	HDR_HIDE_REPLIES = "X-Hide-Replies"
	HDR_DUMP_TOKEN   = "X-Dump-Token"
	DHR_API_TOKEN    = "X-API-Token"
)

Header names.

View Source
const (
	// Auth-related error messages
	ERR_AUTH_FAIL           = "wrong credentials entered, or such user does not exist"
	ERR_AUTH_ACC_TOKEN_FAIL = "could not generate new access token"
	ERR_AUTH_REF_TOKEN_FAIL = "could not generate new refresh token"
	ERR_TOKEN_SAVE_FAIL     = "could not save new token to database"

	// Generic error messages
	ERR_CALLER_BLANK      = "callerID cannot be empty"
	ERR_CALLER_FAIL       = "could not get caller's name"
	ERR_CALLER_NOT_FOUND  = "caller not found in the database"
	ERR_USER_NOT_FOUND    = "user not found in the database"
	ERR_PAGENO_INCORRECT  = "pageNo has to be specified as integer/number"
	ERR_PAGE_EXPORT_NIL   = "could not get more pages, one exported map is nil!"
	ERR_INPUT_DATA_FAIL   = "could not process the input data, try again"
	ERR_API_TOKEN_BLANK   = "blank API token sent"
	ERR_API_TOKEN_INVALID = "invalid API token sent"
	ERR_NO_SERVER_SECRET  = "missing the server's secret (APP_PEPPER)"

	// Image-processing-related error messages
	ERR_IMG_DECODE_FAIL      = "image: could not decode to byte stream"
	ERR_IMG_ENCODE_FAIL      = "image: could not re-encode"
	ERR_IMG_ORIENTATION_FAIL = "image: could not fix the orientation"
	ERR_IMG_GIF_TO_WEBP_FAIL = "image: could not convert GIF to WebP"
	ERR_IMG_UNKNOWN_TYPE     = "image: unsupported format entered"
	ERR_IMG_SAVE_FILE_FAIL   = "image: could not save to a file"
	ERR_IMG_THUMBNAIL_FAIL   = "image: could not re-encode the thumbnail"

	// Poll-related error messages
	ERR_POLL_AUTHOR_MISMATCH    = "you cannot post a foreigner's poll"
	ERR_POLL_SAVE_FAIL          = "could not save the poll, try again"
	ERR_POLL_POST_FAIL          = "could not save a post about the new poll"
	ERR_POLL_NOT_FOUND          = "such poll not found in the database (may be deleted)"
	ERR_POLL_SELF_VOTE          = "you cannot vote in yours own poll"
	ERR_POLL_EXISTING_VOTE      = "you have already voted on such poll"
	ERR_POLL_DELETE_FOREIGN     = "you cannot delete a foreigner's poll"
	ERR_POLL_DELETE_FAIL        = "could not delete the poll, try again"
	ERR_POLLID_BLANK            = "pollID param is required"
	ERR_POLL_INVALID_VOTE_COUNT = "you can pass only one vote per poll"
	ERR_POLL_DUPLICIT_OPTIONS   = "all options (inc. the very question) have to be unique"

	// Post-related error messages
	ERR_POST_BLANK          = "post has got no content"
	ERR_POSTER_INVALID      = "you can add yours posts only"
	ERR_POST_SAVE_FAIL      = "could not save the post, try again"
	ERR_POST_NOT_FOUND      = "could not find the post (may be deleted)"
	ERR_POST_SELF_RATE      = "you cannot rate your own posts"
	ERR_POST_UPDATE_FOREIGN = "you cannot update a foreigner's post"
	ERR_POST_DELETE_FOREIGN = "you cannot delete a foreigner's post"
	ERR_POST_DELETE_FAIL    = "could not delete the post, try again"
	ERR_POST_DELETE_THUMB   = "could not delete associated thumbnail"
	ERR_POST_DELETE_FULLIMG = "could not delete associated full image"
	ERR_POSTID_BLANK        = "postID param is required"
	ERR_HASHTAG_BLANK       = "hashtag param is required"

	// Push-related (non-)error messages
	MSG_WEBPUSH_GW_RESPONSE         = "push goroutine: webpush gateway:"
	ERR_DEVICE_NOT_FOUND            = "devices not found in the database"
	ERR_SUBSCRIPTION_SAVE_FAIL      = "could not save the subscription to database"
	ERR_SUBSCRIPTION_NOT_FOUND      = "such subscription not found in the database"
	ERR_DEVICE_SUBSCRIBED_ALREADY   = "this device has been already registered"
	ERR_PUSH_SELF_NOTIF             = "will not notify oneself"
	ERR_PUSH_DISABLED_NOTIF         = "will not notify original poster"
	ERR_PUSH_UUID_BLANK             = "device's UUID cannot be sent empty"
	ERR_PUSH_BODY_COMPOSE_FAIL      = "failed to compose the notification body"
	ERR_NOTIFICATION_NOT_SENT       = "notification could not be sent"
	ERR_NOTIFICATION_RESP_BODY_FAIL = "failed to read the notification's response body"
	ERR_DEVICE_LIST_UPDATE_FAIL     = "failed to save the updated device list"
	ERR_UUID_BLANK                  = "uuid param is required"

	// User-related error messages
	ERR_USER_DELETE_FOREIGN       = "you cannot delete a foreigner's account"
	ERR_USER_DELETE_FAIL          = "could not delete the user from user database, try again"
	ERR_SUBSCRIPTION_DELETE_FAIL  = "could not delete the user from subscriptions, try again"
	ERR_MISSING_IMG_CONTENT       = "no image data received, try again"
	ERR_REGISTRATION_DISABLED     = "registration is disabled at the moment"
	ERR_RESTRICTED_NICKNAME       = "this nickname is restricted"
	ERR_USER_NICKNAME_TAKEN       = "this nickname has been already taken"
	ERR_NICKNAME_CHARSET_MISMATCH = "the nickname can only consist of characters a-z, A-Z and numbers 0-9"
	ERR_NICKNAME_TOO_LONG_SHORT   = "nickname is too long (>12 runes), or too short (<3 runes)"
	ERR_WRONG_EMAIL_FORMAT        = "wrong format of the e-mail address"
	ERR_EMAIL_ALREADY_USED        = "this e-mail address has been already used"
	ERR_USER_SAVE_FAIL            = "could not save new user to database"
	ERR_POSTREG_POST_SAVE_FAIL    = "could not save a new post about the new user's addition"
	ERR_USER_UPDATE_FOREIGN       = "could not update a foreign's account"
	ERR_USERID_BLANK              = "userID param is required"
	ERR_USER_UPDATE_FAIL          = "could not update the user in database"
	ERR_USER_PASSPHRASE_FOREIGN   = "you can change yours passphrase only"
	ERR_PASSPHRASE_REQ_INCOMPLETE = "passphrase change request is partly or completely empty"
	ERR_PASSPHRASE_CURRENT_WRONG  = "current passphrase sent is wrong"
	ERR_NO_EMAIL_MATCH            = "could not find a match for such e-mail address"
	ERR_MAIL_COMPOSITION_FAIL     = "could not compose the mail properly, try again"
	ERR_MAIL_NOT_SENT             = "could not send the mail, try again"
	ERR_REQUEST_UUID_BLANK        = "could not process blank request's UUID"
	ERR_REQUEST_EMAIL_BLANK       = "could not process blank request's e-mail address"
	ERR_REQUEST_UUID_INVALID      = "entered UUID is invalid"
	ERR_REQUEST_UUID_EXPIRED      = "entered UUID has expired"
	ERR_REQUEST_DELETE_FAIL       = "could not delete the request from database, try again"
	ERR_PASSPHRASE_UPDATE_FAIL    = "could not update the passphrase in database, try again"
	ERR_TARGET_USER_NOT_PRIVATE   = "target user is not private, no need to file new follow requests"
	ERR_USER_AVATAR_FOREIGN       = "you can update yours avatar only"
	ERR_ACTIVATION_MAIL_FAIL      = "the activation mail was not sent, try again"
	ERR_USER_NOT_ACTIVATED        = "user has not been activated yet, check your mail inbox"
)

(Non-)Error messages.

View Source
const (
	// Localhost as the IPv4 address.
	LOCALHOST4 = "127.0.0.1"

	// Localhost as the IPv6 address.
	LOCALHOST6 = "::1"
)
View Source
const (
	// Refresh token's TTL.
	TOKEN_TTL = time.Hour * 168 * 4
)

Token consts.

Variables

This section is empty.

Functions

func FlushUserData added in v0.42.1

func FlushUserData(users *map[string]models.User, callerID string) *map[string]models.User

helper function to flush sensitive user data in the export for response

func UnmarshalRequestData

func UnmarshalRequestData[T any](r *http.Request, model *T) error

UnmarshalRequestData is a helper function that combines reading the request body and data structure unmarshalling from a JSON stream.

func WriteResponse added in v0.41.0

func WriteResponse(w http.ResponseWriter, resp interface{}, code int) error

Types

type APIResponse added in v0.42.0

type APIResponse struct {
	// Common fields for all responses
	Message   string `json:"message"`
	Timestamp int64  `json:"timestamp"`

	// Data field for any payload
	Data interface{} `json:"data"`
}

new generic API response schema idea

type Logger

type Logger struct {
	// CallerID is a nickname of the user calling the API.
	CallerID string `json:"-"`

	// Code integer is a HTTP return code.
	Code int `json:"code" validation:"required"`

	// IPAddress string is basically an user's IPv4/IPv6 address (beware of proxies).
	IPAddress string `json:"-"`

	// Message string holds a custom message returned by a various HTTP handler.
	Message string `json:"message" validation:"required"`

	// Prefix stands at the start of the logger output before the very message.
	Prefix string `json:"-"`

	// Method string hold a HTTP method name.
	Method string `json:"method"`

	// Route string is the very route called by user.
	Route string `json:"route"`

	// Time property hold the actual time of the request processing.
	Time time.Time `json:"time" validation:"required"`

	// Version is the tagged version of the client's SW (compiled in).
	Version string `json:"version"`

	// WorkerName string is the name of a worker processing such request.
	WorkerName string `json:"worker_name" validation:"required"`

	// Response is a helper field to hold the prepared API response for sending.
	Response *APIResponse `json:"-"`

	// Err is a helper error field to hold the error type from the BE logging callback procedure.
	Err error `json:"-"`
}

func NewLogger

func NewLogger(r *http.Request, worker string) *Logger

NewLogger takes the HTTP request structure in (can be nil), and the worker's name (required string) to prepare a new Logger instance. Returns a pointer to the new Logger instance.

func (*Logger) Error added in v0.42.0

func (l *Logger) Error(err error) *Logger

Error takes an error and holds it in the Logger structure for the possible output.

func (*Logger) Log added in v0.42.0

func (l *Logger) Log() *Logger

Log write the logger's JSON output to the stdout.

func (*Logger) Msg added in v0.42.0

func (l *Logger) Msg(msg string) *Logger

Msg writes the input <msg> string to the Logger struct for its following output.

func (*Logger) Payload added in v0.42.0

func (l *Logger) Payload(pl interface{}) *Logger

Payload takes and prepares the HTTP response's body payload. The input can be nil.

func (*Logger) Println

func (l *Logger) Println(msg string, code int) bool

Println formats the encoded Logger struct into an output string to stdin. Deprecated.

func (*Logger) RemovePrefix added in v0.44.0

func (l *Logger) RemovePrefix() *Logger

func (*Logger) SetPrefix added in v0.44.0

func (l *Logger) SetPrefix(prefix string) *Logger

SetPrefix sets the log's prefix according to the input <prefix> string.

func (*Logger) Status added in v0.42.0

func (l *Logger) Status(code int) *Logger

Status writes the HTTP Status code (as integer) for the following logger output.

func (*Logger) Write added in v0.42.0

func (l *Logger) Write(w http.ResponseWriter)

Write writes the HTTP headers and sends the response to the client.

Jump to

Keyboard shortcuts

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