Documentation
¶
Overview ¶
Package lys is used for rapid development of REST APIs handling database CRUD actions.
Please see the README and wiki for an overview.
Index ¶
- Constants
- Variables
- func Archive[idT lyspg.PrimaryKeyType](env Env, db *pgxpool.Pool, store lyspg.Archiver[idT]) http.HandlerFunc
- func DecodeJsonBody[T any](body []byte) (dest T, err error)
- func Delete[idT lyspg.PrimaryKeyType](env Env, store lyspg.Deleter[idT]) http.HandlerFunc
- func ExtractJsonBody(r *http.Request, maxBodySize int64) (body []byte, err error)
- func FileResponse(filePath, outputFileName string, remove bool, w http.ResponseWriter)
- func Get[T any](env Env, store lyspg.Selecter[T], opts *GetOpts[T]) http.HandlerFunc
- func GetById[idT lyspg.PrimaryKeyType, outT any](env Env, store lyspg.SelecterById[idT, outT]) http.HandlerFunc
- func GetEnumValues(env Env, db *pgxpool.Pool, schema, enum string) http.HandlerFunc
- func GetFunc[T any](env Env, store lyspg.Selecter[T], ...) http.HandlerFunc
- func GetSimple[T any](env Env, selectFunc func(ctx context.Context) (items []T, err error)) http.HandlerFunc
- func GetUserIdFromCtx(ctx context.Context) int64
- func GetUserInfoFromCtx(ctx context.Context) any
- func GetUserNameFromCtx(ctx context.Context) string
- func GetValue[T any](env Env, db *pgxpool.Pool, stmt string) http.HandlerFunc
- func GetWithLastSync[T any](env Env, store lyspg.SelecterWithLastSync[T]) http.HandlerFunc
- func HandleDbError(ctx context.Context, line int, stmt string, err error, logger *slog.Logger, ...)
- func HandleError(ctx context.Context, err error, logger *slog.Logger, w http.ResponseWriter)
- func HandleExtError(ctx context.Context, extMessage string, err error, logger *slog.Logger, ...)
- func HandleInternalError(ctx context.Context, err error, logger *slog.Logger, w http.ResponseWriter)
- func HandleUserError(err lyserr.User, w http.ResponseWriter)
- func Import[inputT any](env Env, db *pgxpool.Pool, store lyspg.Importer[inputT], ...) http.HandlerFunc
- func JsonResponse(resp StdResponse, httpStatus int, w http.ResponseWriter)
- func LimitAuthed(rateLimits *lysauth.AppRateLimits, logger *slog.Logger) func(http.Handler) http.Handler
- func LimitUnauthed(rateLimits *lysauth.AppRateLimits, logger *slog.Logger) func(http.Handler) http.Handler
- func Message(msg string) http.HandlerFunc
- func NotFound() http.HandlerFunc
- func Patch[idT lyspg.PrimaryKeyType](env Env, store lyspg.UpdatePartialer[idT]) http.HandlerFunc
- func PgSleep(db lyspg.PoolOrTx, logger *slog.Logger, sleepSecs, cancelAfterSecs int) http.HandlerFunc
- func Post[inputT any, outputT any](env Env, store lyspg.Inserter[inputT, outputT]) http.HandlerFunc
- func ProcessSlice[T any](env Env, processFunc func(context.Context, []T) (int64, error)) http.HandlerFunc
- func Put[idT lyspg.PrimaryKeyType, inputT any](env Env, store lyspg.Updater[idT, inputT]) http.HandlerFunc
- func RejectBlockedIp(loginAttempts *lysauth.AppLoginAttempts, logger *slog.Logger) func(http.Handler) http.Handler
- func Restore[idT lyspg.PrimaryKeyType](env Env, db *pgxpool.Pool, store lyspg.Archiver[idT]) http.HandlerFunc
- func RunSimple(env Env, runFunc func(context.Context) error) http.HandlerFunc
- func SetClientIp(useRealIpHeader bool, logger *slog.Logger) func(http.Handler) http.Handler
- func Upload(extractParams lysformfile.ExtractParams, destPath string, logger *slog.Logger) http.HandlerFunc
- func WithUserInfo(ctx context.Context, userInfo any) context.Context
- type Env
- type ExtractGetRequestModifierParams
- type GetOpts
- type GetReqModifiers
- type ImportValueRepl
- type RouteAdderFunc
- type StatusWriter
- type StdResponse
- type SubRoute
Constants ¶
const ( // status ReqSucceeded string = "succeeded" ReqFailed string = "failed" // data DataArchived string = "archived" DataDeleted string = "deleted" DataRestored string = "restored" DataUpdated string = "updated" )
response constants
Variables ¶
var ( // bad requests (default status) ErrBodyMissing = lyserr.User{Message: "request body missing"} ErrIdMissing = lyserr.User{Message: "id missing"} ErrIdNotAUuid = lyserr.User{Message: "id not a uuid"} ErrIdNotAnInteger = lyserr.User{Message: "id not an integer"} ErrIdNotUnique = lyserr.User{Message: "id not unique"} // the handling func was expecting id to be unique, but it is not ErrIdParseError = lyserr.User{Message: "id could not be parsed"} ErrInvalidContentType = lyserr.User{Message: "content type must be application/json"} ErrInvalidId = lyserr.User{Message: "invalid id"} // the id sent is not present in the relevant table ErrInvalidJson = lyserr.User{Message: "invalid json"} ErrNoAssignments = lyserr.User{Message: "no assignments found"} // for patch reqs where assignmentMap is expected ErrNotParseableToMap = lyserr.User{Message: "json body could not be parsed into a map of field names to values"} ErrRouteNotFound = lyserr.User{Message: "route not found"} // auth ErrPermissionDenied = lyserr.User{Message: "permission denied", StatusCode: http.StatusForbidden} // authorization failed ErrUserInfoMissing = lyserr.User{Message: "userInfo missing", StatusCode: http.StatusUnauthorized} // failed to get ReqUserInfo from context )
expected user errors during validation
Functions ¶
func Archive ¶ added in v0.1.16
func Archive[idT lyspg.PrimaryKeyType](env Env, db *pgxpool.Pool, store lyspg.Archiver[idT]) http.HandlerFunc
Archive handles moving a record from the supplied store into its archived table
func DecodeJsonBody ¶
DecodeJsonBody decodes the supplied json body into dest and checks for a variety of error conditions. Caller should check that body is valid JSON and should enforce a maximum body size (usually done in ExtractJsonBody).
func Delete ¶
func Delete[idT lyspg.PrimaryKeyType](env Env, store lyspg.Deleter[idT]) http.HandlerFunc
Delete handles deletion of a single item using the supplied store.
func ExtractJsonBody ¶
ExtractJsonBody reads and validates the body of the supplied request.
func FileResponse ¶ added in v0.1.5
func FileResponse(filePath, outputFileName string, remove bool, w http.ResponseWriter)
FileResponse opens the supplied file and streams it to w as a file
func GetById ¶
func GetById[idT lyspg.PrimaryKeyType, outT any](env Env, store lyspg.SelecterById[idT, outT]) http.HandlerFunc
GetById handles retrieval of a single item from the supplied store.
func GetEnumValues ¶
GetEnumValues returns enum values from the supplied schema and enum type name
func GetFunc ¶ added in v0.1.38
func GetFunc[T any](env Env, store lyspg.Selecter[T], selectFunc func(ctx context.Context, params lyspg.SelectParams) (items []T, unpagedCount lyspg.TotalCount, err error)) http.HandlerFunc
GetFunc is a wrapper for Get which allows passing an alternative Select func with the same signature.
func GetSimple ¶
func GetSimple[T any](env Env, selectFunc func(ctx context.Context) (items []T, err error)) http.HandlerFunc
GetSimple handles retrieval of all items returned by selectFunc, which may only take ctx as param
func GetUserIdFromCtx ¶ added in v0.3.44
GetUserIdFromCtx returns the user ID if it can be obtained from a ctx set by WithUserInfo if it has a GetUserId() method. Otherwise it returns 0.
func GetUserInfoFromCtx ¶ added in v0.3.73
func GetUserNameFromCtx ¶
GetUserNameFromCtx returns the username if it can be obtained from a ctx set by WithUserInfo if it has a GetUserName() method. Otherwise it returns "Unknown".
func GetWithLastSync ¶ added in v0.1.9
func GetWithLastSync[T any](env Env, store lyspg.SelecterWithLastSync[T]) http.HandlerFunc
GetWithLastSync is a wrapper for Get which adds the lastSyncAt timestamp from the supplied func to the response headers.
func HandleDbError ¶
func HandleDbError(ctx context.Context, line int, stmt string, err error, logger *slog.Logger, w http.ResponseWriter)
HandleDbError returns a specific error message to the API user if the error is caused by a bad input, e.g. a check or uniqueness violation. Otherwise it returns a generic error message to the API user and logs the specific error
func HandleError ¶ added in v0.1.25
HandleError is the general method for handling API errors where err could contain wrapped errors of other types
func HandleExtError ¶ added in v0.1.25
func HandleExtError(ctx context.Context, extMessage string, err error, logger *slog.Logger, w http.ResponseWriter)
HandleExtError returns the external message to the API user and logs the error
func HandleInternalError ¶
func HandleInternalError(ctx context.Context, err error, logger *slog.Logger, w http.ResponseWriter)
HandleInternalError returns a generic error message to the API user and logs the error
func HandleUserError ¶
func HandleUserError(err lyserr.User, w http.ResponseWriter)
HandleUserError returns a helpful message to the API user, but does not log the error. If HTTP status is not provided, BadRequest is assumed.
func Import ¶ added in v0.3.8
func Import[inputT any](env Env, db *pgxpool.Pool, store lyspg.Importer[inputT], valRepls ...ImportValueRepl) http.HandlerFunc
Import handles creating multiple new items using the supplied store and returning the number of rows inserted the supplied db is used for the MapFunc in valRepls
func JsonResponse ¶
func JsonResponse(resp StdResponse, httpStatus int, w http.ResponseWriter)
JsonResponse marshals the supplied StdResponse to json and writes it to w
func LimitAuthed ¶ added in v0.3.75
func LimitAuthed(rateLimits *lysauth.AppRateLimits, logger *slog.Logger) func(http.Handler) http.Handler
LimitAuthed is middleware that applies rate limiting to authenticated users based on user ID.
func LimitUnauthed ¶ added in v0.3.75
func LimitUnauthed(rateLimits *lysauth.AppRateLimits, logger *slog.Logger) func(http.Handler) http.Handler
LimitUnauthed is middleware that applies rate limiting to unauthenticated users based on IP.
func Message ¶
func Message(msg string) http.HandlerFunc
Message returns the supplied msg in the Data field
func NotFound ¶
func NotFound() http.HandlerFunc
NotFound provides a response informing the user that the requested route was not found
func Patch ¶
func Patch[idT lyspg.PrimaryKeyType](env Env, store lyspg.UpdatePartialer[idT]) http.HandlerFunc
Patch handles changing some of an item's fields using the supplied store.
func PgSleep ¶
func PgSleep(db lyspg.PoolOrTx, logger *slog.Logger, sleepSecs, cancelAfterSecs int) http.HandlerFunc
PgSleep creates an artifical longrunning query in the db which can be viewed using pg_stat_activity. Pass cancelAfterSecs as 0 to not cancel the request. Used for testing context cancelation
func Post ¶
Post handles creating a new item using the supplied store and returning an output (the new item or its ID) in the response
func ProcessSlice ¶ added in v0.1.27
func ProcessSlice[T any](env Env, processFunc func(context.Context, []T) (int64, error)) http.HandlerFunc
ProcessSlice extracts a slice from the req body and passes it into the supplied processFunc
func Put ¶
func Put[idT lyspg.PrimaryKeyType, inputT any](env Env, store lyspg.Updater[idT, inputT]) http.HandlerFunc
Put handles changing an item using the supplied store.
func RejectBlockedIp ¶ added in v0.3.75
func RejectBlockedIp(loginAttempts *lysauth.AppLoginAttempts, logger *slog.Logger) func(http.Handler) http.Handler
RejectBlockedIp is middleware that rejects requests from blocked IPs based on login attempts.
func Restore ¶
func Restore[idT lyspg.PrimaryKeyType](env Env, db *pgxpool.Pool, store lyspg.Archiver[idT]) http.HandlerFunc
Restore handles moving a record from the store's archived table back to the main table
func SetClientIp ¶ added in v0.3.75
SetClientIp returns middleware that resolves the client IP and stores it in context.
func Upload ¶ added in v0.3.52
func Upload(extractParams lysformfile.ExtractParams, destPath string, logger *slog.Logger) http.HandlerFunc
Upload is a handler that extracts file uploads from a multipart form request and streams them to the specified destination path.
func WithUserInfo ¶ added in v0.3.73
WithUserInfo binds a user info struct to a request via context. if you use this setter and add a GetUserName() string method to the struct, the username will be included in error logs when using the error handlers in error_handlers.go. if you add a GetUserId() int64 method to the struct, GetUserIdFromCtx will be available to use in your code.
Types ¶
type Env ¶
type Env struct {
Logger *slog.Logger
Validate *validator.Validate
GetOptions lysapi.GetOptions
PostOptions lysapi.PostOptions
}
Env (environment) contains objects and options needed by API calls
type ExtractGetRequestModifierParams ¶ added in v0.3.28
type GetOpts ¶ added in v0.3.36
type GetOpts[T any] struct { // AdditionalFilterParamNames are param names that are not in the store's db tags, but should be allowed anyway. Must be handled by the store's Select func. AdditionalFilterParamNames lysset.Set[string] // GetLastSyncAt gets the last synced timestamp for external data and returns it as a response header. GetLastSyncAt func(ctx context.Context) (lastSyncAt lystype.Datetime, err error) // SelectFunc, if passed, overrides the default store Select() func. SelectFunc func(ctx context.Context, params lyspg.SelectParams) (items []T, unpagedCount lyspg.TotalCount, err error) // SetFuncUrlParamNames are used if selecting from a setFunc rather than a view. They are the names of the url params that will be passed, in order, to the setFunc. // Don't use a Set: order must be preserved. SetFuncUrlParamNames []string }
type GetReqModifiers ¶
type GetReqModifiers struct {
Format string
Fields []string
Conditions []lyspg.Condition
Page int
PerPage int
Sorts []string
SetFuncParamValues []any
}
GetReqModifiers contains data from a GET request's Url params which is used to modify a database SELECT statement
func ExtractGetRequestModifiers ¶
func ExtractGetRequestModifiers(r *http.Request, params ExtractGetRequestModifierParams) (getReqModifiers GetReqModifiers, err error)
ExtractGetRequestModifiers reads the Url params of the supplied GET request and converts them into a GetReqModifiers
type ImportValueRepl ¶ added in v0.3.9
type ImportValueRepl struct {
StringJsonName string
Int64JsonName string
MapFunc func(context.Context, *pgxpool.Pool) (map[string]int64, error)
}
ImportValueRepl is a struct that can be used when the input contains a foreign key. It allows the referenced table's string representation to be passed by the user. The string attribute gets replaced with the int64 attribute, and all the values are mapped using the supplied map. For example:
StringJsonName: "car_manufacturer" Int64JsonName: "car_manufacturer_fk" MapFunc: returns Ford = 1, Nissan = 2, etc
type RouteAdderFunc ¶
RouteAdderFunc is a function returning a subrouter
type StatusWriter ¶ added in v0.3.24
type StatusWriter struct {
http.ResponseWriter
Status int
Bytes int
}
StatusWriter is a wrapper around http.ResponseWriter that captures the status code and number of bytes written in the response. It implements http.Flusher and http.Hijacker so that it can also write websocket responses.
func (*StatusWriter) Flush ¶ added in v0.3.42
func (sw *StatusWriter) Flush()
func (*StatusWriter) Hijack ¶ added in v0.3.42
func (sw *StatusWriter) Hijack() (net.Conn, *bufio.ReadWriter, error)
func (*StatusWriter) Unwrap ¶ added in v0.3.42
func (sw *StatusWriter) Unwrap() http.ResponseWriter
func (*StatusWriter) WriteHeader ¶ added in v0.3.24
func (sw *StatusWriter) WriteHeader(code int)
type StdResponse ¶
type StdResponse struct {
Status string `json:"status"`
Data any `json:"data,omitzero"`
GetMetadata *lysapi.SelectMetadata `json:"metadata,omitzero"` // only used for GET many
ErrDescription string `json:"err_description,omitzero"`
}
StdResponse is the return type of all API routes
type SubRoute ¶
type SubRoute struct {
Url string
RouteAdder RouteAdderFunc
}
SubRoute contains a Url path and the function returning the subrouter to process that path
Source Files
¶
Directories
¶
| Path | Synopsis |
|---|---|
|
internal
|
|
|
cmd/lyscli
command
|
|
|
Package lysclient contains types and functions to help test a REST API which was created using lys functions.
|
Package lysclient contains types and functions to help test a REST API which was created using lys functions. |
|
Package lyserr contains structs related to error handling used in lys and lyspg.
|
Package lyserr contains structs related to error handling used in lys and lyspg. |
|
Package lysgen contains experimental functions to generate code from Postgres database tables.
|
Package lysgen contains experimental functions to generate code from Postgres database tables. |
|
Package lysmeta contains functions that analyze structs.
|
Package lysmeta contains functions that analyze structs. |
|
Package lyspg contains structs and functions providing generic CRUD operations on a Postgres database.
|
Package lyspg contains structs and functions providing generic CRUD operations on a Postgres database. |
|
Package lyspgdb contains functions for creating and connecting to Postgres databases.
|
Package lyspgdb contains functions for creating and connecting to Postgres databases. |
|
Package lyspgdb contains functions for monitoring Postgres databases.
|
Package lyspgdb contains functions for monitoring Postgres databases. |
|
Package lysstring contains string functions.
|
Package lysstring contains string functions. |
|
Package lystype contains date/time types used in lys and lyspg.
|
Package lystype contains date/time types used in lys and lyspg. |