util

package
v0.5.2 Latest Latest
Warning

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

Go to latest
Published: Mar 15, 2025 License: MIT Imports: 19 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// Permanents cookies.
	SessionCookieName = "bloggulus_session"
	SessionCookieTTL  = 7 * 24 * time.Hour

	// Session cookies.
	NextCookieName  = "bloggulus_next"
	StateCookieName = "bloggulus_state"
	ToastCookieName = "bloggulus_toast"
)

Functions

func BadRequestResponse

func BadRequestResponse(w http.ResponseWriter, r *http.Request)

Render a 400 Bad Request error page.

func CreateErrorResponse added in v0.4.0

func CreateErrorResponse(w http.ResponseWriter, r *http.Request, err error)

Handle errors that arise from creating a new row in the database.

func DeleteErrorResponse added in v0.4.0

func DeleteErrorResponse(w http.ResponseWriter, r *http.Request, err error)

Handle errors that arise from deleting a single row from the database.

func ForbiddenResponse added in v0.3.7

func ForbiddenResponse(w http.ResponseWriter, r *http.Request)

Render a 403 Forbidden error page.

func GetContextAccount added in v0.4.0

func GetContextAccount(r *http.Request) (*model.Account, bool)

The GetContextAccount() retrieves the model.Account and an "exists" bool from the request context.

func GetContextConfig added in v0.5.0

func GetContextConfig(r *http.Request) (config.Config, bool)

func HashUserID added in v0.5.0

func HashUserID(userID, key string) string

Hash a user ID with a given key (via HMAC).

func InternalServerErrorResponse added in v0.4.0

func InternalServerErrorResponse(w http.ResponseWriter, r *http.Request, err error)

Render a 500 Internal Server Error page.

func IsHTMXRequest added in v0.4.0

func IsHTMXRequest(r *http.Request) bool

func ListErrorResponse added in v0.4.0

func ListErrorResponse(w http.ResponseWriter, r *http.Request, err error)

Handle errors that arise from reading many (zero or more) rows from the database.

func NewBaseCookie added in v0.4.0

func NewBaseCookie() http.Cookie

Return a base cookie with secure defaults.

func NewExpiredCookie added in v0.4.0

func NewExpiredCookie(name string) http.Cookie

Create a cookie that is instantly expired.

func NewPermanentCookie added in v0.4.0

func NewPermanentCookie(name, value string, ttl time.Duration) http.Cookie

Create a permanent (not session) cookie with a given time-to-live.

func NewSessionCookie added in v0.4.0

func NewSessionCookie(name, value string) http.Cookie

Create a session (not permanent) cookie that expires when the user's session ends.

func NotFoundResponse

func NotFoundResponse(w http.ResponseWriter, r *http.Request)

Render a 404 Not Found error page.

func PageSizeToLimitOffset

func PageSizeToLimitOffset(page, size int) (int, int)

Convert user-friendly "page/size" pagination to DB-friendly "limit/offset".

func ReadErrorResponse added in v0.4.0

func ReadErrorResponse(w http.ResponseWriter, r *http.Request, err error)

Handle errors that arise from reading a single row from the database.

func Render added in v0.4.0

func Render(w http.ResponseWriter, r *http.Request, code int, render RenderFunc)

Render an HTML template based on a given RenderFunc.

func RenderError added in v0.4.0

func RenderError(w http.ResponseWriter, r *http.Request, code int, render RenderFunc)

Render an HTML error template based on a given RenderFunc. This function differs from Render in that it doesn't call InternalServerErrorResponse if something goes wrong since that could cause an infinite recursion.

func SetContextAccount added in v0.4.0

func SetContextAccount(r *http.Request, account *model.Account) *http.Request

The SetContextAccount() method returns a new copy of the request with the provided model.Account added to the context. Note that we use our accountContextKey constant as the key.

func SetContextConfig added in v0.5.0

func SetContextConfig(r *http.Request, conf config.Config) *http.Request

func TemplateBaseData added in v0.4.0

func TemplateBaseData(r *http.Request, w http.ResponseWriter) layout.BaseData

func UpdateErrorResponse added in v0.4.0

func UpdateErrorResponse(w http.ResponseWriter, r *http.Request, err error)

Handle errors that arise from updating a single row in the database.

Types

type RenderFunc added in v0.4.0

type RenderFunc func(w io.Writer) error

Generic function type for rendering data to an io.Writer.

type Validator added in v0.4.0

type Validator map[string]string

func NewValidator added in v0.4.0

func NewValidator() Validator

func (Validator) Add added in v0.4.0

func (v Validator) Add(field, message string)

func (Validator) Check added in v0.4.0

func (v Validator) Check(field, message string, ok bool)

func (Validator) CheckMaxCharacters added in v0.4.0

func (v Validator) CheckMaxCharacters(field, value string, n int)

func (Validator) CheckRequired added in v0.4.0

func (v Validator) CheckRequired(field, value string)

func (Validator) IsValid added in v0.4.0

func (v Validator) IsValid() bool

Jump to

Keyboard shortcuts

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