Documentation
¶
Overview ¶
Package task is the domain shared by both examples: the Task model, its create/patch request shapes and validation schemas, and the paginated list response. Both examples/fiber and examples/nethttp import this unchanged — everything that differs between them is the HTTP framework adapter around it, not the domain itself.
Index ¶
- Constants
- Variables
- func AttachmentKey(id string) string
- func CacheKey(id string) string
- func NewCacheStore(driver, prefix string) (cache.Store, func() error, error)
- func NewStorageDisk(ctx context.Context, driver, localRoot, localBaseURL, bucket, s3Prefix string) (storage.Disk, func() error, error)
- type AttachmentURL
- type CreateRequest
- type List
- type PatchRequest
- type Task
Constants ¶
const MaxAttachmentSize = 32 << 20 // 32MiB
MaxAttachmentSize caps a single attachment upload. Applied explicitly by both examples (Fiber's own Config.BodyLimit and net/http's http.MaxBytesReader) rather than left to each framework's own default: Fiber's is 4MiB out of the box, net/http's is unbounded, and those two defaults disagreeing would make the "same API" the two examples claim to be reject the identical oversized upload differently depending only on which one happened to be running.
const TableSchema = `` /* 247-byte string literal not displayed */
TableSchema creates the tasks table if it isn't already there. A real service would run this through a migration tool instead; inlining it here keeps the example runnable against a bare Postgres with no extra tooling.
Variables ¶
var CreateSchema = zog.Struct(zog.Shape{ "title": zog.String().Required().Trim().Min(1).Max(200), "done": zog.Bool(), })
CreateSchema's keys are matched against CreateRequest's field names case-insensitively (zog capitalizes the key's first letter and looks up that field) — "title" -> Title, "done" -> Done. No struct tags needed.
var PatchSchema = zog.Struct(zog.Shape{ "title": zog.Ptr(zog.String().Trim().Min(1).Max(200)), "done": zog.Ptr(zog.Bool()), })
PatchSchema validates each field only when present: zog.Ptr's wrapped schema simply isn't run against a nil pointer (and, with no .NotNil() call here, that's not itself a validation failure), but a *provided* title still has to pass the same Trim/Min/Max Title always has.
Functions ¶
func AttachmentKey ¶ added in v0.3.0
AttachmentKey builds the storage path a task's attachment is stored under. It deliberately has no file extension (a task's attachment could be anything) — storage/local's ContentType re-sniffs the real type from the bytes regardless, but storage/s3's Content-Type is guessed from the key's extension at upload time, so an S3-backed attachment always reports back as generic application/octet-stream. A real service that cares about that would store the client-declared content type as its own column instead of leaning on the backend's guess.
id is stripped of "/" and ".." before use: it's a route param, normally a server-generated ULID, but a request can put any string there. storage/s3 has no path validation of its own, and storage/local's os.Root only blocks a full escape from its root — neither stops an id like ".." resolving to a location outside this task's own tasks/<id>/ scope, so this closes that off at the one place every attachment path gets built.
func NewCacheStore ¶
NewCacheStore builds the cache.Store an example uses to cache task reads ahead of Postgres, chosen by driver ("memory" or "redis"). It always returns a close func — a no-op for memory — so callers can defer it unconditionally regardless of which backend was picked.
func NewStorageDisk ¶ added in v0.3.0
func NewStorageDisk(ctx context.Context, driver, localRoot, localBaseURL, bucket, s3Prefix string) (storage.Disk, func() error, error)
NewStorageDisk builds the storage.Disk an example uses to store task attachments, chosen by driver ("local" or "s3"). It always returns a close func — a no-op for s3, which holds no resource that needs releasing, unlike local's os.Root handle — so callers can defer it unconditionally regardless of which backend was picked.
Unlike NewCacheStore, this does real network I/O for the s3 driver (EnsureBucket), so it takes a ctx: a first run against a fresh MinIO has no bucket yet, and failing fast here beats failing on the first upload. localBaseURL is only used by the local driver, to build a signed TemporaryUrl/PresignedPutUrl that actually resolves back to this app's own /storage route (see main.go) — s3's signed URLs point at the S3 endpoint directly and need no such base. s3Prefix scopes s3 keys the same way NewCacheStore's own prefix argument scopes cache keys — without it, two apps pointed at the same default S3_BUCKET would share one flat keyspace and could read or delete each other's attachments.
Types ¶
type AttachmentURL ¶ added in v0.3.0
type AttachmentURL struct {
URL string `json:"url"`
ExpiresInSeconds int `json:"expires_in_seconds"`
}
AttachmentURL is the response shape for a signed, time-limited download link.
type CreateRequest ¶
CreateRequest is the POST /tasks payload: a full task, so every field is required. validator/fiber's and validator/nethttp's ValidateRequest[T] decode and validate it against CreateSchema before a handler ever sees it.
type List ¶
type List struct {
Items []*Task `json:"items"`
Count int `json:"count"`
Limit int `json:"limit"`
Offset int `json:"offset"`
}
List is the paginated response a task list endpoint returns.
type PatchRequest ¶
PatchRequest is the PATCH /tasks/{id} payload: a true partial update, so every field is a pointer — nil means "leave this field alone," distinct from its zero value. A plain `Done bool` here couldn't tell an omitted "done" apart from an explicit "done: false", which would silently reset a completed task back to not-done on any title-only patch.
type Task ¶
type Task struct {
bun.BaseModel `bun:"table:tasks,alias:t"`
db.Model
Title string `bun:"title,notnull" json:"title"`
Done bool `bun:"done,notnull,default:false" json:"done"`
}
Task is the example domain model, persisted via db.Repository[*Task] and (for reads) cached ahead of Postgres via a cache.Store.