config

package
v0.8.0 Latest Latest
Warning

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

Go to latest
Published: Aug 2, 2026 License: MIT Imports: 12 Imported by: 0

Documentation

Index

Constants

View Source
const (
	PublicFieldResourceUser    = "user"
	PublicFieldResourceContent = "content"
)

PublicFieldResourceUser and PublicFieldResourceContent are the two supported values for PublicField.Resource. They map to the two public query endpoints: the authors endpoint (users) and the content_items endpoint (content).

View Source
const (
	PublicFieldOperationSort   = "sort"
	PublicFieldOperationFilter = "filter"
	PublicFieldOperationExpose = "expose"
)

PublicFieldOperationSort, PublicFieldOperationFilter, and PublicFieldOperationExpose are the supported values for PublicField.Operations. A field can be allowlisted for any combination.

Variables

This section is empty.

Functions

func LoadLanguages

func LoadLanguages(cfg *Config) ([]string, error)

LoadLanguages loads the supported languages from the config.toml file. Returns at least ["en"] — if the config file is missing or has no languages defined, "en" is used as the primary language.

func LoadPostTypes

func LoadPostTypes(cfg *Config) (*posttype.Service, error)

LoadPostTypes loads custom post types from the config directory If the config file doesn't exist, it returns a service with default post types

func LoadThumbnails

func LoadThumbnails(cfg *Config) (*thumbnail.Service, error)

LoadThumbnails loads thumbnail configurations from the config file (config.toml). If the file does not exist, it returns a service with default configs.

func PrimaryLanguage

func PrimaryLanguage(languages []string) string

PrimaryLanguage returns the first language in the slice, or "en" if empty.

Types

type CSPConfig added in v0.7.0

type CSPConfig struct {
	Disable         bool              `toml:"disable"`
	ReportOnly      bool              `toml:"report_only"`
	ScriptSrc       []string          `toml:"script_src"`
	StyleSrc        []string          `toml:"style_src"`
	ImgSrc          []string          `toml:"img_src"`
	FontSrc         []string          `toml:"font_src"`
	ConnectSrc      []string          `toml:"connect_src"`
	FrameSrc        []string          `toml:"frame_src"`
	MediaSrc        []string          `toml:"media_src"`
	ObjectSrc       []string          `toml:"object_src"`
	WorkerSrc       []string          `toml:"worker_src"`
	ExtraDirectives map[string]string `toml:"extra_directives"`
	Policy          string            `toml:"policy"`
}

CSPConfig holds the optional [csp] block from config.toml. All fields have zero-value defaults that are backward compatible. Operators append sources to individual directives; they never replace.

func LoadCSPConfig added in v0.7.0

func LoadCSPConfig(cfg *Config) (CSPConfig, error)

LoadCSPConfig reads the optional [csp] block from config.toml. When the file, section, or directory is missing it returns a zero-value CSPConfig (fully backward compatible — Build() returns the default policy).

func (CSPConfig) Build added in v0.7.0

func (c CSPConfig) Build() (headerName, headerValue string)

Build returns the CSP header name and value for this configuration. Empty header value means "do not emit a CSP header".

type Config

type Config struct {
	Host               string
	Port               int
	DBPath             string
	DBDriver           string
	DBDSN              string
	DBPoolMaxConns     int
	JWTSecret          string
	LogLevel           string
	SMTPHost           string
	SMTPPort           int
	SMTPUser           string
	SMTPPassword       string
	SMTPFrom           string
	ConfigDir          string
	ConfigFile         string
	CORSAllowedOrigins string
	SiteURL            string
	DevMode            bool
	AdminDevURL        string
	ThemeDir           string
	PostPerPage        int

	// ServerReadHeaderTimeout is the maximum duration to read request headers
	// (Slowloris protection). Read via SERVER_READ_HEADER_TIMEOUT.
	ServerReadHeaderTimeout time.Duration

	// ServerReadTimeout is the maximum duration to read the entire request
	// including the body. A zero value means no timeout — body size is capped
	// per-handler via MaxBytesReader / maxBodySizeMiddleware. Read via
	// SERVER_READ_TIMEOUT.
	ServerReadTimeout time.Duration

	// ServerWriteTimeout is the maximum duration to write the response after
	// the request headers have been read. A zero value means no timeout. Read
	// via SERVER_WRITE_TIMEOUT.
	ServerWriteTimeout time.Duration

	RateLimitEnabled         bool
	RateLimitAuthPerMinute   int
	RateLimitAPIPerMinute    int
	RateLimitPublicPerMinute int

	AIImageGenerationAPIKey      string
	AIImageGenerationModel       string
	AIImageGenerationSize        string
	AIImageGenerationAspectRatio string

	AITextGenerationAPIKey  string
	AITextGenerationBaseURL string
	AITextGenerationModel   string

	APIKeyPepper string

	// ImportMaxSizeMB is the ceiling (in megabytes) for any importer upload
	// (WordPress WXR now; future importers reuse the same cap). Read via
	// IMPORT_MAX_SIZE_MB. Use ImportMaxSize() for the byte value.
	ImportMaxSizeMB int

	// WordPressImportTimeout is the maximum duration a WordPress import job may
	// run after the HTTP response has been sent (the job runs in a background
	// goroutine). Read via WORDPRESS_IMPORT_TIMEOUT.
	WordPressImportTimeout time.Duration

	// HugoImportTimeout is the maximum duration a Hugo import job may run after
	// the HTTP response has been sent (the job runs in a background goroutine).
	// Read via HUGO_IMPORT_TIMEOUT.
	HugoImportTimeout time.Duration
}

Config holds all application configuration

func Load

func Load() (*Config, error)

Load loads configuration from environment variables It tries to load .env file first, then reads from actual environment

func (*Config) ImportMaxSize added in v0.5.0

func (c *Config) ImportMaxSize() int64

ImportMaxSize returns the importer upload ceiling in bytes.

func (*Config) IsImageGenerationEnabled

func (c *Config) IsImageGenerationEnabled() bool

IsImageGenerationEnabled returns true if the Google Imagen API key is configured

func (*Config) IsTextGenerationEnabled

func (c *Config) IsTextGenerationEnabled() bool

IsTextGenerationEnabled returns true if the AI text generation API key is configured

func (*Config) ParseCORSOrigins

func (c *Config) ParseCORSOrigins() []string

ParseCORSOrigins parses the comma-separated CORS origins string into a slice It handles whitespace trimming and validates that each origin has a valid http/https scheme

type HomepageSection added in v0.5.0

type HomepageSection struct {
	PostType string `toml:"post_type"`
	Limit    int    `toml:"limit"`
	Offset   int    `toml:"offset"`
	Title    string `toml:"title"`
}

HomepageSection is one [[homepage_section]] block from config.toml. It tells the public homepage to render a per-post-type grouping in addition to (or instead of relying on) the flat latest-posts list. PostType is the only required field; Limit, Offset, and Title are optional overrides.

func LoadHomepageSections added in v0.5.0

func LoadHomepageSections(cfg *Config) ([]HomepageSection, error)

LoadHomepageSections reads the [[homepage_section]] blocks from the same config.toml that supplies post types. It mirrors LoadPostTypes: a missing config directory or file yields an empty slice (the homepage then renders the flat latest-posts list — fully backward compatible).

type PublicField added in v0.7.0

type PublicField struct {
	Resource   string   `toml:"resource"`
	Field      string   `toml:"field"`
	PostType   string   `toml:"post_type"`
	Operations []string `toml:"operations"`
}

PublicField is one [[public_field]] block from config.toml. It declares that a specific custom-field or system-field slug may be used in a public query (filter, sort, or expose) against one of the public query endpoints. Without an entry here, the public endpoints reject any cf_<field> / cf_<field>_min / cf_<field>_max / sort_by=cf:<field> parameter that references the field with a 400 field_not_queryable error. Fields with the "expose" operation are included in the response body (e.g. in the publicAuthors[:].publicFields map).

Admin endpoints (e.g. /api/v1/content_items) are NOT gated by this allowlist — they remain unrestricted, matching pre-existing behaviour.

type PublicFieldRegistry added in v0.7.0

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

PublicFieldRegistry is the immutable, lookup-optimised form of a slice of PublicField entries. Hand one of these to a handler and call IsQueryable to enforce the allowlist on incoming public queries.

func LoadPublicFields added in v0.7.0

func LoadPublicFields(cfg *Config) (*PublicFieldRegistry, error)

LoadPublicFields reads the [[public_field]] blocks from the same config.toml that supplies post types and homepage sections. It mirrors LoadSiteConfig: a missing config directory or file yields an empty registry (fully backward compatible — every public cf_*/sort_by=cf:* query is then rejected).

func (*PublicFieldRegistry) ExposedFields added in v0.7.0

func (r *PublicFieldRegistry) ExposedFields(resource, postType string) []string

ExposedFields returns the field slugs that are allowlisted with the "expose" operation for the given resource (and optionally postType). When resource is "content", postType is matched against the entry's PostType; an entry with an empty PostType matches every post type. When resource is "user", postType is ignored. An empty registry returns nil.

func (*PublicFieldRegistry) IsQueryable added in v0.7.0

func (r *PublicFieldRegistry) IsQueryable(resource, postType, field, operation string) bool

IsQueryable reports whether the named field may be used with the given operation against the named resource. When resource is "content", postType is matched against the entry's PostType; an entry with an empty PostType matches every post type. When resource is "user", postType is ignored.

An empty registry (no [[public_field]] blocks configured) returns false for every query — the public endpoints then reject every cf_*/sort_by=cf:* parameter. This is the safe default.

type SiteConfig added in v0.5.0

type SiteConfig struct {
	Name string `toml:"name"`
}

SiteConfig is the optional [site_config] block from config.toml. It carries the site's text and visual identity — the values that appear on every public page and that the handler otherwise bakes into the binary (notably the PageTitle suffix and the og:site_name). Both fields are optional; an absent block yields a zero-value SiteConfig, and the caller is expected to default Name to the application name when empty.

func LoadSiteConfig added in v0.5.0

func LoadSiteConfig(cfg *Config) (SiteConfig, error)

LoadSiteConfig reads the optional [site_config] block from the same config.toml that supplies post types and homepage sections. It mirrors LoadHomepageSections: a missing config directory or file yields a zero-value SiteConfig (fully backward compatible).

Jump to

Keyboard shortcuts

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