Documentation ¶
Overview ¶
Common functions, constants, and structures package for the backend.
Index ¶
- Constants
- Variables
- func FlushUserData(users *map[string]models.User, callerID string) *map[string]models.User
- func UnmarshalRequestData[T any](r *http.Request, model *T) error
- func WriteResponse(w http.ResponseWriter, resp interface{}, code int) error
- type APIResponse
- type DefaultLogger
- func (l DefaultLogger) CallerID() string
- func (l DefaultLogger) Error(err error) Logger
- func (l DefaultLogger) Log() Logger
- func (l DefaultLogger) Msg(msg string) Logger
- func (l DefaultLogger) Payload(pl interface{}) Logger
- func (l DefaultLogger) RemovePrefix() Logger
- func (l DefaultLogger) ResetTimer() Logger
- func (l DefaultLogger) SetPrefix(prefix string) Logger
- func (l DefaultLogger) Status(code int) Logger
- func (l DefaultLogger) Write(w http.ResponseWriter)
- type DummyLogger
- func (l DummyLogger) CallerID() string
- func (l DummyLogger) Error(_ error) Logger
- func (l DummyLogger) Log() Logger
- func (l DummyLogger) Msg(message string) Logger
- func (l DummyLogger) Payload(pl interface{}) Logger
- func (l DummyLogger) RemovePrefix() Logger
- func (l DummyLogger) ResetTimer() Logger
- func (l DummyLogger) SetPrefix(prefix string) Logger
- func (l DummyLogger) Status(code int) Logger
- func (l DummyLogger) Write(w http.ResponseWriter)
- type Logger
Constants ¶
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.
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" ERR_TOKEN_NOT_FOUND = "requested token was not found" // 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_TYPE_BLANK = "request type param not specified" ERR_REQUEST_TYPE_UNKNOWN = "request type unknown" 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_SAVE_FAIL = "could not save the request to database, try again" 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" ERR_USER_UPDATE_REQ_BLANK = "update request for user not specified" )
(Non-)Error messages.
const ( // Localhost as the IPv4 address. LOCALHOST4 = "127.0.0.1" // Localhost as the IPv6 address. LOCALHOST6 = "::1" )
const ( // Refresh token's TTL. TOKEN_TTL = time.Hour * 168 * 4 )
Token consts.
Variables ¶
var DecideStatusFromError = func(err error) int { if err == nil { return http.StatusOK } if err.Error() == ERR_USER_NOT_ACTIVATED || err.Error() == ERR_REQUEST_EMAIL_BLANK || err.Error() == ERR_REQUEST_UUID_BLANK || err.Error() == ERR_INPUT_DATA_FAIL || err.Error() == ERR_PASSPHRASE_REQ_INCOMPLETE || err.Error() == ERR_REQUEST_UUID_EXPIRED || err.Error() == ERR_REQUEST_UUID_BLANK || err.Error() == ERR_REQUEST_UUID_INVALID || err.Error() == ERR_RESTRICTED_NICKNAME || err.Error() == ERR_USER_NICKNAME_TAKEN || err.Error() == ERR_NICKNAME_CHARSET_MISMATCH || err.Error() == ERR_NICKNAME_TOO_LONG_SHORT || err.Error() == ERR_WRONG_EMAIL_FORMAT || err.Error() == ERR_INPUT_DATA_FAIL || err.Error() == ERR_IMG_UNKNOWN_TYPE { return http.StatusBadRequest } if err.Error() == ERR_POLL_SELF_VOTE || err.Error() == ERR_USER_DELETE_FOREIGN || err.Error() == ERR_USER_PASSPHRASE_FOREIGN || err.Error() == ERR_REGISTRATION_DISABLED || err.Error() == ERR_POLL_EXISTING_VOTE || err.Error() == ERR_POLL_INVALID_VOTE_COUNT { return http.StatusForbidden } if err.Error() == ERR_POLL_NOT_FOUND || err.Error() == ERR_NO_EMAIL_MATCH || err.Error() == ERR_USER_NOT_FOUND { return http.StatusNotFound } if err.Error() == ERR_EMAIL_ALREADY_USED || err.Error() == ERR_PASSPHRASE_CURRENT_WRONG { return http.StatusConflict } return http.StatusInternalServerError }
Common helper function to decide the HTTP error according to the error contents.
Functions ¶
func FlushUserData ¶ added in v0.42.1
helper function to flush sensitive user data in the export for response
func UnmarshalRequestData ¶
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 DefaultLogger ¶ added in v0.44.19
type DefaultLogger struct { // 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"` // 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:"-"` // TimerStart holds the starting point of the time measurement. To be subtracted and written to the JSON output afterwards. TimerStart time.Time `json:"request_start"` // TimeStop is the very stop time mark in terms of the system/application process duration. TimerStop time.Time `json:"request_stop"` // TimeDurationNS hold the difference between the start and stop time points regarding the application process making its duration (in nanoseconds). TimerDurationNS time.Duration `json:"request_duration_ns"` // contains filtered or unexported fields }
func (DefaultLogger) CallerID ¶ added in v0.44.19
func (l DefaultLogger) CallerID() string
CallerID returns the caller's ID (nickname), that has been (hopefully) decided from the context.
func (DefaultLogger) Error ¶ added in v0.44.19
func (l DefaultLogger) Error(err error) Logger
Error takes an error and holds it in the Logger structure for the possible output.
func (DefaultLogger) Log ¶ added in v0.44.19
func (l DefaultLogger) Log() Logger
Log write the logger's JSON output to the stdout.
func (DefaultLogger) Msg ¶ added in v0.44.19
func (l DefaultLogger) Msg(msg string) Logger
Msg writes the input <msg> string to the Logger struct for its following output.
func (DefaultLogger) Payload ¶ added in v0.44.19
func (l DefaultLogger) Payload(pl interface{}) Logger
Payload takes and prepares the HTTP response's body payload. The input can be nil.
func (DefaultLogger) RemovePrefix ¶ added in v0.44.19
func (l DefaultLogger) RemovePrefix() Logger
RemovePrefix remove preiously prepended string from the Logger struct.
func (DefaultLogger) ResetTimer ¶ added in v0.44.19
func (l DefaultLogger) ResetTimer() Logger
ResetTimer resets the TimerStart timestamp. Usable in the procedures where the logger is passed (???) or not to log the whole HTTP server uptime (the gracefully HTTP server shutdown goroutine).
func (DefaultLogger) SetPrefix ¶ added in v0.44.19
func (l DefaultLogger) SetPrefix(prefix string) Logger
SetPrefix sets the log's prefix according to the input <prefix> string.
func (DefaultLogger) Status ¶ added in v0.44.19
func (l DefaultLogger) Status(code int) Logger
Status writes the HTTP Status code (as integer) for the following logger output.
func (DefaultLogger) Write ¶ added in v0.44.19
func (l DefaultLogger) Write(w http.ResponseWriter)
Write writes the HTTP headers and sends the response to the client.
type DummyLogger ¶ added in v0.44.19
type DummyLogger struct{}
func NewDummyLogger ¶ added in v0.44.19
func NewDummyLogger() *DummyLogger
func (DummyLogger) CallerID ¶ added in v0.44.22
func (l DummyLogger) CallerID() string
func (DummyLogger) Error ¶ added in v0.44.19
func (l DummyLogger) Error(_ error) Logger
func (DummyLogger) Log ¶ added in v0.44.19
func (l DummyLogger) Log() Logger
func (DummyLogger) Msg ¶ added in v0.44.19
func (l DummyLogger) Msg(message string) Logger
func (DummyLogger) Payload ¶ added in v0.44.19
func (l DummyLogger) Payload(pl interface{}) Logger
func (DummyLogger) RemovePrefix ¶ added in v0.44.19
func (l DummyLogger) RemovePrefix() Logger
func (DummyLogger) ResetTimer ¶ added in v0.44.19
func (l DummyLogger) ResetTimer() Logger
func (DummyLogger) SetPrefix ¶ added in v0.44.19
func (l DummyLogger) SetPrefix(prefix string) Logger
func (DummyLogger) Status ¶ added in v0.44.19
func (l DummyLogger) Status(code int) Logger
func (DummyLogger) Write ¶ added in v0.44.19
func (l DummyLogger) Write(w http.ResponseWriter)
type Logger ¶
type Logger interface { // Basic methods: setters. Msg(message string) Logger Status(code int) Logger Error(err error) Logger SetPrefix(prefix string) Logger RemovePrefix() Logger // Getters. CallerID() string // Timer-related methods. ResetTimer() Logger // Data-related methods. Log() Logger Payload(pl interface{}) Logger Write(w http.ResponseWriter) }