api

package
v0.5.0 Latest Latest
Warning

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

Go to latest
Published: Feb 5, 2026 License: GPL-3.0 Imports: 24 Imported by: 0

Documentation

Overview

Package api provides REST API handlers for the CMS.

Package api provides REST API handlers for the CMS.

Package api provides REST API handlers for the CMS.

Package api provides REST API handlers for the CMS.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func WriteBadRequest

func WriteBadRequest(w http.ResponseWriter, message string, details map[string]string)

WriteBadRequest writes a 400 Bad Request response.

func WriteCreated

func WriteCreated(w http.ResponseWriter, data any)

WriteCreated writes a 201 Created JSON response.

func WriteError

func WriteError(w http.ResponseWriter, statusCode int, code, message string, details map[string]string)

WriteError writes an error JSON response.

func WriteForbidden

func WriteForbidden(w http.ResponseWriter, message string)

WriteForbidden writes a 403 Forbidden response.

func WriteInternalError

func WriteInternalError(w http.ResponseWriter, message string)

WriteInternalError writes a 500 Internal Server Error response.

func WriteJSON

func WriteJSON(w http.ResponseWriter, statusCode int, data any)

WriteJSON writes a JSON response with the given status code.

func WriteNotFound

func WriteNotFound(w http.ResponseWriter, message string)

WriteNotFound writes a 404 Not Found response.

func WriteSuccess

func WriteSuccess(w http.ResponseWriter, data any, meta *Meta)

WriteSuccess writes a successful JSON response.

func WriteUnauthorized

func WriteUnauthorized(w http.ResponseWriter, message string)

WriteUnauthorized writes a 401 Unauthorized response.

func WriteValidationError

func WriteValidationError(w http.ResponseWriter, fieldErrors map[string]string)

WriteValidationError writes a 422 Unprocessable Entity response with field errors.

Types

type AuthorResponse

type AuthorResponse struct {
	ID    int64  `json:"id"`
	Name  string `json:"name"`
	Email string `json:"email"`
}

AuthorResponse represents an author in API responses.

type CategoryAPIResponse

type CategoryAPIResponse struct {
	ID           int64                  `json:"id"`
	Name         string                 `json:"name"`
	Slug         string                 `json:"slug"`
	Description  string                 `json:"description,omitempty"`
	ParentID     *int64                 `json:"parent_id,omitempty"`
	Position     int64                  `json:"position"`
	LanguageCode string                 `json:"language_code"`
	PageCount    int64                  `json:"page_count"`
	Children     []*CategoryAPIResponse `json:"children,omitempty"`
	CreatedAt    time.Time              `json:"created_at"`
	UpdatedAt    time.Time              `json:"updated_at"`
}

CategoryAPIResponse represents a category in API responses.

type CategoryResponse

type CategoryResponse struct {
	ID          int64  `json:"id"`
	Name        string `json:"name"`
	Slug        string `json:"slug"`
	Description string `json:"description,omitempty"`
}

CategoryResponse represents a category in API responses.

type CreateCategoryRequest

type CreateCategoryRequest struct {
	Name         string  `json:"name"`
	Slug         string  `json:"slug"`
	Description  string  `json:"description,omitempty"`
	ParentID     *int64  `json:"parent_id,omitempty"`
	Position     *int64  `json:"position,omitempty"`
	LanguageCode *string `json:"language_code,omitempty"`
}

CreateCategoryRequest represents the request body for creating a category.

func (CreateCategoryRequest) GetName

func (r CreateCategoryRequest) GetName() string

GetName returns the name field.

func (CreateCategoryRequest) GetSlug

func (r CreateCategoryRequest) GetSlug() string

GetSlug returns the slug field.

type CreatePageRequest

type CreatePageRequest struct {
	Title             string  `json:"title"`
	Slug              string  `json:"slug"`
	Body              string  `json:"body"`
	Status            string  `json:"status"`
	PageType          string  `json:"page_type,omitempty"`
	LanguageCode      *string `json:"language_code,omitempty"`
	FeaturedImageID   *int64  `json:"featured_image_id,omitempty"`
	HideFeaturedImage bool    `json:"hide_featured_image"`
	ExcludeFromLists  bool    `json:"exclude_from_lists"`
	MetaTitle         string  `json:"meta_title,omitempty"`
	MetaDescription   string  `json:"meta_description,omitempty"`
	MetaKeywords      string  `json:"meta_keywords,omitempty"`
	OGImageID         *int64  `json:"og_image_id,omitempty"`
	NoIndex           bool    `json:"no_index"`
	NoFollow          bool    `json:"no_follow"`
	CanonicalURL      string  `json:"canonical_url,omitempty"`
	ScheduledAt       *string `json:"scheduled_at,omitempty"`
	CategoryIDs       []int64 `json:"category_ids,omitempty"`
	TagIDs            []int64 `json:"tag_ids,omitempty"`
}

CreatePageRequest represents the request body for creating a page.

type CreateTagRequest

type CreateTagRequest struct {
	Name         string  `json:"name"`
	Slug         string  `json:"slug"`
	LanguageCode *string `json:"language_code,omitempty"`
}

CreateTagRequest represents the request body for creating a tag.

func (CreateTagRequest) GetName

func (r CreateTagRequest) GetName() string

GetName returns the name field.

func (CreateTagRequest) GetSlug

func (r CreateTagRequest) GetSlug() string

GetSlug returns the slug field.

type DocsConfig

type DocsConfig struct {
	DB         *sql.DB
	TemplateFS fs.FS
	IsDev      bool
}

DocsConfig holds configuration for the docs handler.

type DocsHandler

type DocsHandler struct {
	// contains filtered or unexported fields
}

DocsHandler handles API documentation rendering.

func NewDocsHandler

func NewDocsHandler(cfg DocsConfig) (*DocsHandler, error)

NewDocsHandler creates a new documentation handler.

func (*DocsHandler) ServeDocs

func (h *DocsHandler) ServeDocs(w http.ResponseWriter, r *http.Request)

ServeDocs serves the API documentation page.

type EntityFetcher

type EntityFetcher[T any] func(id int64) (T, error)

EntityFetcher is a function that fetches an entity by ID.

type ErrorDetail

type ErrorDetail struct {
	Code    string            `json:"code"`
	Message string            `json:"message"`
	Details map[string]string `json:"details,omitempty"`
}

ErrorDetail contains error information.

type ErrorResponse

type ErrorResponse struct {
	Error ErrorDetail `json:"error"`
}

ErrorResponse is the standard API error response.

type FolderResponse

type FolderResponse struct {
	ID        int64     `json:"id"`
	Name      string    `json:"name"`
	ParentID  *int64    `json:"parent_id,omitempty"`
	Position  int64     `json:"position"`
	CreatedAt time.Time `json:"created_at"`
}

FolderResponse represents a media folder in API responses.

type Handler

type Handler struct {
	// contains filtered or unexported fields
}

Handler holds shared dependencies for all API handlers.

func NewHandler

func NewHandler(db *sql.DB) *Handler

NewHandler creates a new API handler.

func (*Handler) AuthInfo

func (h *Handler) AuthInfo(w http.ResponseWriter, r *http.Request)

AuthInfo returns information about the authenticated API key.

func (*Handler) CreateCategory

func (h *Handler) CreateCategory(w http.ResponseWriter, r *http.Request)

CreateCategory handles POST /api/v1/categories Requires taxonomy:write permission

func (*Handler) CreatePage

func (h *Handler) CreatePage(w http.ResponseWriter, r *http.Request)

CreatePage handles POST /api/v1/pages Requires pages:write permission

func (*Handler) CreateTag

func (h *Handler) CreateTag(w http.ResponseWriter, r *http.Request)

CreateTag handles POST /api/v1/tags Requires taxonomy:write permission

func (*Handler) DeleteCategory

func (h *Handler) DeleteCategory(w http.ResponseWriter, r *http.Request)

DeleteCategory handles DELETE /api/v1/categories/{id} Requires taxonomy:write permission

func (*Handler) DeleteMedia

func (h *Handler) DeleteMedia(w http.ResponseWriter, r *http.Request)

DeleteMedia handles DELETE /api/v1/media/{id} Requires media:write permission

func (*Handler) DeletePage

func (h *Handler) DeletePage(w http.ResponseWriter, r *http.Request)

DeletePage handles DELETE /api/v1/pages/{id} Requires pages:write permission

func (*Handler) DeleteTag

func (h *Handler) DeleteTag(w http.ResponseWriter, r *http.Request)

DeleteTag handles DELETE /api/v1/tags/{id} Requires taxonomy:write permission

func (*Handler) GetCategory

func (h *Handler) GetCategory(w http.ResponseWriter, r *http.Request)

GetCategory handles GET /api/v1/categories/{id} Public: returns a single category with its children

func (*Handler) GetMedia

func (h *Handler) GetMedia(w http.ResponseWriter, r *http.Request)

GetMedia handles GET /api/v1/media/{id}

func (*Handler) GetPage

func (h *Handler) GetPage(w http.ResponseWriter, r *http.Request)

GetPage handles GET /api/v1/pages/{id} Public: returns only if published With API key: returns any page

func (*Handler) GetPageBySlug

func (h *Handler) GetPageBySlug(w http.ResponseWriter, r *http.Request)

GetPageBySlug handles GET /api/v1/pages/slug/{slug} Public: returns only published pages

func (*Handler) GetTag

func (h *Handler) GetTag(w http.ResponseWriter, r *http.Request)

GetTag handles GET /api/v1/tags/{id} Public: returns a single tag

func (*Handler) ListCategories

func (h *Handler) ListCategories(w http.ResponseWriter, r *http.Request)

ListCategories handles GET /api/v1/categories Public: returns all categories as a nested tree structure

func (*Handler) ListMedia

func (h *Handler) ListMedia(w http.ResponseWriter, r *http.Request)

ListMedia handles GET /api/v1/media Public: returns all media With API key: enhanced access (same for now, could add private media later)

func (*Handler) ListPages

func (h *Handler) ListPages(w http.ResponseWriter, r *http.Request)

ListPages handles GET /api/v1/pages Public: returns only published pages With API key: can filter by status

func (*Handler) ListTags

func (h *Handler) ListTags(w http.ResponseWriter, r *http.Request)

ListTags handles GET /api/v1/tags Public: returns all tags with page counts

func (*Handler) SetCacheManager added in v0.3.0

func (h *Handler) SetCacheManager(cm *cache.Manager)

SetCacheManager sets the cache manager for cache invalidation.

func (*Handler) Status

func (h *Handler) Status(w http.ResponseWriter, _ *http.Request)

Status returns the API status.

func (*Handler) UpdateCategory

func (h *Handler) UpdateCategory(w http.ResponseWriter, r *http.Request)

UpdateCategory handles PUT /api/v1/categories/{id} Requires taxonomy:write permission

func (*Handler) UpdateMedia

func (h *Handler) UpdateMedia(w http.ResponseWriter, r *http.Request)

UpdateMedia handles PUT /api/v1/media/{id} Requires media:write permission

func (*Handler) UpdatePage

func (h *Handler) UpdatePage(w http.ResponseWriter, r *http.Request)

UpdatePage handles PUT /api/v1/pages/{id} Requires pages:write permission

func (*Handler) UpdateTag

func (h *Handler) UpdateTag(w http.ResponseWriter, r *http.Request)

UpdateTag handles PUT /api/v1/tags/{id} Requires taxonomy:write permission

func (*Handler) UploadMedia

func (h *Handler) UploadMedia(w http.ResponseWriter, r *http.Request)

UploadMedia handles POST /api/v1/media Requires media:write permission Accepts multipart/form-data with file(s)

type MediaResponse

type MediaResponse struct {
	ID           int64                      `json:"id"`
	UUID         string                     `json:"uuid"`
	Filename     string                     `json:"filename"`
	MimeType     string                     `json:"mime_type"`
	Size         int64                      `json:"size"`
	Width        *int64                     `json:"width,omitempty"`
	Height       *int64                     `json:"height,omitempty"`
	Alt          string                     `json:"alt"`
	Caption      string                     `json:"caption"`
	FolderID     *int64                     `json:"folder_id,omitempty"`
	UploadedBy   int64                      `json:"uploaded_by"`
	CreatedAt    time.Time                  `json:"created_at"`
	UpdatedAt    time.Time                  `json:"updated_at"`
	URLs         *MediaURLs                 `json:"urls,omitempty"`
	Variants     []VariantResponse          `json:"variants,omitempty"`
	Folder       *FolderResponse            `json:"folder,omitempty"`
	Translations []MediaTranslationResponse `json:"translations,omitempty"`
}

MediaResponse represents a media item in API responses.

type MediaTranslationResponse

type MediaTranslationResponse struct {
	LanguageID   int64  `json:"language_id"`
	LanguageCode string `json:"language_code"`
	LanguageName string `json:"language_name"`
	Alt          string `json:"alt"`
	Caption      string `json:"caption"`
}

MediaTranslationResponse represents a media translation in API responses.

type MediaURLs

type MediaURLs struct {
	Original  string `json:"original"`
	Thumbnail string `json:"thumbnail,omitempty"`
	Medium    string `json:"medium,omitempty"`
	Large     string `json:"large,omitempty"`
}

MediaURLs contains URLs for different media sizes.

type Meta

type Meta struct {
	Total   int64 `json:"total,omitempty"`
	Page    int   `json:"page,omitempty"`
	PerPage int   `json:"per_page,omitempty"`
	Pages   int   `json:"pages,omitempty"`
}

Meta contains pagination and other metadata.

type PageResponse

type PageResponse struct {
	ID                int64              `json:"id"`
	Title             string             `json:"title"`
	Slug              string             `json:"slug"`
	Body              string             `json:"body"`
	Status            string             `json:"status"`
	PageType          string             `json:"page_type"`
	AuthorID          int64              `json:"author_id"`
	LanguageCode      string             `json:"language_code"`
	CreatedAt         time.Time          `json:"created_at"`
	UpdatedAt         time.Time          `json:"updated_at"`
	PublishedAt       *time.Time         `json:"published_at,omitempty"`
	FeaturedImageID   *int64             `json:"featured_image_id,omitempty"`
	HideFeaturedImage bool               `json:"hide_featured_image"`
	ExcludeFromLists  bool               `json:"exclude_from_lists"`
	MetaTitle         string             `json:"meta_title,omitempty"`
	MetaDescription   string             `json:"meta_description,omitempty"`
	MetaKeywords      string             `json:"meta_keywords,omitempty"`
	OGImageID         *int64             `json:"og_image_id,omitempty"`
	NoIndex           bool               `json:"no_index"`
	NoFollow          bool               `json:"no_follow"`
	CanonicalURL      string             `json:"canonical_url,omitempty"`
	ScheduledAt       *time.Time         `json:"scheduled_at,omitempty"`
	Author            *AuthorResponse    `json:"author,omitempty"`
	Categories        []CategoryResponse `json:"categories,omitempty"`
	Tags              []TagResponse      `json:"tags,omitempty"`
}

PageResponse represents a page in API responses.

type Response

type Response struct {
	Data any   `json:"data,omitempty"`
	Meta *Meta `json:"meta,omitempty"`
}

Response is the standard API response wrapper.

type SlugExistsChecker

type SlugExistsChecker func() (int64, error)

SlugExistsChecker is a function that checks if a slug exists (returns count and error).

type StatusResponse

type StatusResponse struct {
	Status  string `json:"status"`
	Version string `json:"version"`
}

StatusResponse contains API status information.

type TagAPIResponse

type TagAPIResponse struct {
	ID           int64     `json:"id"`
	Name         string    `json:"name"`
	Slug         string    `json:"slug"`
	LanguageCode string    `json:"language_code"`
	PageCount    int64     `json:"page_count"`
	CreatedAt    time.Time `json:"created_at"`
	UpdatedAt    time.Time `json:"updated_at"`
}

TagAPIResponse represents a tag in API responses.

type TagResponse

type TagResponse struct {
	ID   int64  `json:"id"`
	Name string `json:"name"`
	Slug string `json:"slug"`
}

TagResponse represents a tag in API responses.

type UpdateCategoryRequest

type UpdateCategoryRequest struct {
	Name        *string `json:"name,omitempty"`
	Slug        *string `json:"slug,omitempty"`
	Description *string `json:"description,omitempty"`
	ParentID    *int64  `json:"parent_id,omitempty"`
	Position    *int64  `json:"position,omitempty"`
}

UpdateCategoryRequest represents the request body for updating a category.

type UpdateMediaRequest

type UpdateMediaRequest struct {
	Filename *string `json:"filename,omitempty"`
	Alt      *string `json:"alt,omitempty"`
	Caption  *string `json:"caption,omitempty"`
	FolderID *int64  `json:"folder_id,omitempty"`
}

UpdateMediaRequest represents the request body for updating media metadata.

type UpdatePageRequest

type UpdatePageRequest struct {
	Title             *string  `json:"title,omitempty"`
	Slug              *string  `json:"slug,omitempty"`
	Body              *string  `json:"body,omitempty"`
	Status            *string  `json:"status,omitempty"`
	PageType          *string  `json:"page_type,omitempty"`
	FeaturedImageID   *int64   `json:"featured_image_id,omitempty"`
	HideFeaturedImage *bool    `json:"hide_featured_image,omitempty"`
	ExcludeFromLists  *bool    `json:"exclude_from_lists,omitempty"`
	MetaTitle         *string  `json:"meta_title,omitempty"`
	MetaDescription   *string  `json:"meta_description,omitempty"`
	MetaKeywords      *string  `json:"meta_keywords,omitempty"`
	OGImageID         *int64   `json:"og_image_id,omitempty"`
	NoIndex           *bool    `json:"no_index,omitempty"`
	NoFollow          *bool    `json:"no_follow,omitempty"`
	CanonicalURL      *string  `json:"canonical_url,omitempty"`
	ScheduledAt       *string  `json:"scheduled_at,omitempty"`
	CategoryIDs       *[]int64 `json:"category_ids,omitempty"`
	TagIDs            *[]int64 `json:"tag_ids,omitempty"`
}

UpdatePageRequest represents the request body for updating a page.

type UpdateTagRequest

type UpdateTagRequest struct {
	Name *string `json:"name,omitempty"`
	Slug *string `json:"slug,omitempty"`
}

UpdateTagRequest represents the request body for updating a tag.

type VariantResponse

type VariantResponse struct {
	ID        int64     `json:"id"`
	Type      string    `json:"type"`
	Width     int64     `json:"width"`
	Height    int64     `json:"height"`
	Size      int64     `json:"size"`
	URL       string    `json:"url"`
	CreatedAt time.Time `json:"created_at"`
}

VariantResponse represents a media variant in API responses.

Jump to

Keyboard shortcuts

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