Documentation
¶
Index ¶
- Constants
- Variables
- type BlogError
- type CORSConfig
- type EventHandlers
- type Options
- func (o Options) Validate() error
- func (o Options) WithCORS(cors *CORSConfig) Options
- func (o Options) WithCache(enabled bool, maxMB int64, ttl time.Duration) Options
- func (o Options) WithContentPath(path string) Options
- func (o Options) WithCustomCSS(css string) Options
- func (o Options) WithEventHandlers(handlers *EventHandlers) Options
- func (o Options) WithFeed(title, description, author, authorEmail, siteURL string) Options
- func (o Options) WithFileWatching(enabled bool) Options
- func (o Options) WithMiddleware(middleware ...func(http.Handler) http.Handler) Options
- func (o Options) WithPostsPerPage(count int) Options
- func (o Options) WithSearch(enabled bool, indexPath string, rebuild bool) Options
- func (o Options) WithVerbose(enabled bool) Options
- type Server
- func (s *Server) AttachRoutes(mux *http.ServeMux, basePath string)
- func (s *Server) Close() error
- func (s *Server) GetIndex() *search.Index
- func (s *Server) GetLoader() *content.Loader
- func (s *Server) Reload() error
- func (s *Server) RenderPost(slug string) (string, error)
- func (s *Server) RenderPostList(tags []string, page int) (string, error)
- func (s *Server) Stats() Stats
- type Stats
Constants ¶
const ( ErrCodeInvalidConfig = "INVALID_CONFIG" ErrCodePostNotFound = "POST_NOT_FOUND" ErrCodeIndexCorrupt = "INDEX_CORRUPT" ErrCodeCacheFailure = "CACHE_FAILURE" ErrCodeSearchFailure = "SEARCH_FAILURE" ErrCodeContentLoad = "CONTENT_LOAD_FAILURE" ErrCodeFileWatch = "FILE_WATCH_FAILURE" ErrCodeServerNotStarted = "SERVER_NOT_STARTED" )
Error codes for BlogError
Variables ¶
var ( // ErrInvalidContentPath is returned when content path is invalid ErrInvalidContentPath = errors.New("invalid content path") // ErrInvalidCacheSize is returned when cache size is invalid ErrInvalidCacheSize = errors.New("cache size must be at least 1MB") // ErrInvalidPostsPerPage is returned when posts per page is invalid ErrInvalidPostsPerPage = errors.New("posts per page must be at least 1") // ErrServerNotStarted is returned when server operations are attempted before starting ErrServerNotStarted = errors.New("server not started") // ErrPostNotFound is returned when a post cannot be found ErrPostNotFound = &BlogError{ Code: ErrCodePostNotFound, Message: "post not found", } // ErrIndexCorrupt is returned when the search index is corrupted ErrIndexCorrupt = &BlogError{ Code: ErrCodeIndexCorrupt, Message: "search index is corrupted", } // ErrCacheFailure is returned when cache operations fail ErrCacheFailure = &BlogError{ Code: ErrCodeCacheFailure, Message: "cache operation failed", } )
Predefined errors for common cases
Functions ¶
This section is empty.
Types ¶
type BlogError ¶ added in v0.5.0
type BlogError struct {
Code string // Machine-readable error code
Message string // Human-readable message
Err error // Underlying error
}
BlogError represents a structured error with context
func NewBlogError ¶ added in v0.5.0
NewBlogError creates a new BlogError
type CORSConfig ¶ added in v0.5.0
type CORSConfig struct {
// AllowedOrigins is a list of allowed origins (e.g., ["https://example.com"])
// Use ["*"] to allow all origins (not recommended for production)
AllowedOrigins []string
// AllowedMethods is a list of allowed HTTP methods
// Defaults to ["GET", "POST", "OPTIONS"]
AllowedMethods []string
// AllowedHeaders is a list of allowed headers
// Defaults to ["Content-Type"]
AllowedHeaders []string
// AllowCredentials indicates whether credentials are allowed
AllowCredentials bool
// MaxAge indicates how long (in seconds) the results of a preflight request can be cached
MaxAge int
}
CORSConfig configures Cross-Origin Resource Sharing
type EventHandlers ¶ added in v0.5.0
type EventHandlers struct {
// OnPostView is called when a post is viewed
OnPostView func(slug string, r *http.Request)
// OnSearch is called when a search is performed
OnSearch func(query string, resultCount int, r *http.Request)
// OnError is called when an error occurs
OnError func(err error, r *http.Request)
// OnReload is called when content is reloaded
OnReload func(postCount int)
}
EventHandlers contains callbacks for blog events
type Options ¶
type Options struct {
// ContentPath is the path to markdown posts (required)
ContentPath string `env:"GOBLOG_CONTENT_PATH" envDefault:"./posts"`
// Cache settings
EnableCache bool `env:"GOBLOG_CACHE_ENABLED" envDefault:"true"`
CacheMaxMB int64 `env:"GOBLOG_CACHE_MAX_MB" envDefault:"100"`
CacheTTL time.Duration `env:"GOBLOG_CACHE_TTL" envDefault:"60m"`
// Search settings
EnableSearch bool `env:"GOBLOG_SEARCH_ENABLED" envDefault:"true"`
SearchIndexPath string `env:"GOBLOG_SEARCH_INDEX_PATH" envDefault:"./blog.bleve"`
RebuildIndex bool `env:"GOBLOG_REBUILD_INDEX" envDefault:"false"`
// Blog settings
PostsPerPage int `env:"GOBLOG_POSTS_PER_PAGE" envDefault:"10"`
// File watching
WatchFiles bool `env:"GOBLOG_WATCH_FILES" envDefault:"false"`
// Logging
Verbose bool `env:"GOBLOG_VERBOSE" envDefault:"false"`
// Middleware - custom middleware to wrap all blog routes
Middleware []func(http.Handler) http.Handler
// CustomCSS - path to custom CSS file or inline CSS to inject
CustomCSS string
// CustomTemplates - custom template overrides (not implemented yet, reserved for future)
CustomTemplates map[string]string
// EventHandlers - callbacks for blog events
EventHandlers *EventHandlers
// CORS - Cross-Origin Resource Sharing configuration
CORS *CORSConfig
// Feed configuration
FeedTitle string // Title for RSS/Atom feeds
FeedDescription string // Description for RSS/Atom feeds
FeedAuthor string // Author for RSS/Atom feeds
FeedAuthorEmail string // Author email for Atom feeds
SiteURL string // Base URL of the site (e.g., "https://example.com")
}
Options configures the blog server
func LoadFromEnv ¶
LoadFromEnv loads options from environment variables Falls back to defaults for any unset variables
func MustLoadFromEnv ¶
func MustLoadFromEnv() Options
MustLoadFromEnv loads options from environment variables Panics if parsing fails
func (Options) WithCORS ¶ added in v0.5.0
func (o Options) WithCORS(cors *CORSConfig) Options
WithCORS sets CORS configuration
func (Options) WithContentPath ¶ added in v0.5.0
WithContentPath sets the content path
func (Options) WithCustomCSS ¶ added in v0.5.0
WithCustomCSS sets custom CSS
func (Options) WithEventHandlers ¶ added in v0.5.0
func (o Options) WithEventHandlers(handlers *EventHandlers) Options
WithEventHandlers sets event handlers
func (Options) WithFileWatching ¶ added in v0.5.0
WithFileWatching enables or disables file watching
func (Options) WithMiddleware ¶ added in v0.5.0
WithMiddleware adds middleware to the options
func (Options) WithPostsPerPage ¶ added in v0.5.0
WithPostsPerPage sets posts per page
func (Options) WithSearch ¶ added in v0.5.0
WithSearch configures search
func (Options) WithVerbose ¶ added in v0.5.0
WithVerbose enables or disables verbose logging
type Server ¶
type Server struct {
// contains filtered or unexported fields
}
Server represents the blog server
func (*Server) AttachRoutes ¶
AttachRoutes attaches blog routes to the given mux at the specified base path
Example:
mux := http.NewServeMux() server.AttachRoutes(mux, "/blog")
This will register:
GET /blog - Blog index
GET /blog/posts/{slug} - Individual post
GET /blog/tags/{tag} - Posts by tag
GET /blog/search - Search (HTMX partial)
func (*Server) RenderPost ¶ added in v0.5.0
RenderPost renders a single post as HTML (for custom layouts) Returns the HTML content that can be embedded in your own templates
func (*Server) RenderPostList ¶ added in v0.5.0
RenderPostList renders a list of posts as HTML (for custom layouts) Returns the HTML content that can be embedded in your own templates If tags is empty, returns all posts. Use page for pagination (0-indexed)