Documentation
¶
Overview ¶
Package config provides common configuration options used across the GoBlog generator and outputter packages.
The package implements the functional options pattern for configuration, allowing packages to accept both required positional parameters and optional configuration via option functions.
Functional Options Pattern ¶
Configuration is applied through Option values that can be passed to constructors. Each option function modifies specific configuration fields:
gen := generator.New(postsFS, config.WithRawOutput())
writer := outputter.NewDirectoryWriter("output/", config.WithRawOutput())
Options are implemented as structs containing functions that modify embedded configuration types. This pattern allows for:
- Backward compatibility when adding new options
- Clear, self-documenting API calls
- Optional parameters without function overloading
Available Options ¶
WithRawOutput() enables raw HTML output mode without template wrapping. When enabled in the generator, markdown is converted to HTML without inserting it into page templates. When enabled in the outputter, the tags directory is not created.
WithTemplatesDir(fs.FS) specifies a custom filesystem containing templates to use for rendering blog pages. This allows you to provide your own template files instead of using the defaults.
WithBlogRoot(path string) sets the root path for the blog when deploying at a subdirectory rather than domain root. For example, use "/blog/" when deploying at example.com/blog/. This ensures all generated links in templates use the correct base path. Default is "/" for root deployment.
Usage Examples ¶
Basic usage with a single option:
fsys := os.DirFS("posts/")
gen := generator.New(fsys, config.WithRawOutput())
Multiple options can be combined:
templateFS := os.DirFS("templates/")
gen := generator.New(fsys,
config.WithRawOutput(),
config.WithTemplatesDir(templateFS),
)
Configuring blog root for subdirectory deployment:
gen := generator.New(fsys,
config.WithBlogRoot("/blog/"),
)
Concurrency ¶
Option values are safe to create and use concurrently. Configuration structs that embed RawOutput and TemplatesDir are safe to read concurrently once created, but should not be modified after construction.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type BaseOption ¶
type BaseOption struct {
WithBlogRootFunc func(v *BlogRoot)
}
BaseOption represents a configuration option that can be applied to many different instances during construction.
Options use the functional options pattern, where each option function returns an BaseOption struct containing one or more function pointers that modify specific configuration fields.
This type should not be constructed directly by users. Instead, use the provided option functions like WithBlogRoot().
func WithBlogRoot ¶
func WithBlogRoot(root string) BaseOption
WithBlogRoot returns an Option that sets the blog's root path.
The blog root is used in generated HTML pages and templates.
Example usage:
gen := generator.New(fsys, renderer, config.WithBlogRoot("/blog/"))
type BaseServerOption ¶
type BaseServerOption struct {
BaseOption
WithPortFunc func(v *Port)
WithHostFunc func(v *Host)
WithMiddlewareFunc func(mw *[]middleware.Middleware)
}
func WithHost ¶
func WithHost(host string) BaseServerOption
func WithMiddleware ¶
func WithMiddleware(mw ...middleware.Middleware) BaseServerOption
WithMiddleware returns a BaseServerOption that adds HTTP middleware to the server.
Middleware are applied in the order provided. The first middleware in the list will be the outermost wrapper (executed first for requests, last for responses). Multiple calls to WithMiddleware append to the middleware chain.
Example usage:
import (
"github.com/harrydayexe/GoWebUtilities/logging"
"github.com/harrydayexe/GoWebUtilities/middleware"
)
cfg := config.ServerConfig{
Server: []config.BaseServerOption{
config.WithPort(8080),
config.WithMiddleware(
logging.New(logger), // Built-in logging
customAuthMiddleware, // Custom middleware
),
},
}
All middleware must be safe for concurrent use by multiple goroutines.
func WithPort ¶
func WithPort(port int) BaseServerOption
type BlogRoot ¶
type BlogRoot string
BlogRoot is a configuration type that holds the blog's root path
This type is typically embedded in generator configuration structs and should be set using the WithBlogRoot() option function.
func (BlogRoot) AsOption ¶
func (o BlogRoot) AsOption() BaseOption
type GeneratorOption ¶
type GeneratorOption struct {
BaseOption
WithRawOutputFunc func(v *RawOutput)
WithSiteTitleFunc func(v *SiteTitle)
}
GeneratorOption represents a configuration option that can be applied to generator or outputter instances during construction.
Options use the functional options pattern, where each option function returns an GeneratorOption struct containing one or more function pointers that modify specific configuration fields.
This type should not be constructed directly by users. Instead, use the provided option functions like WithRawOutput() and WithTemplatesDir().
func WithBaseOption ¶
func WithBaseOption(baseOption BaseOption) GeneratorOption
func WithRawOutput ¶
func WithRawOutput() GeneratorOption
WithRawOutput returns an Option that enables raw HTML output mode.
When this option is applied to a generator, it will produce HTML content without template wrapping - only the Markdown-to-HTML conversion is performed. When applied to an outputter, it will skip creating the tags directory.
This is useful for scenarios where you want to integrate GoBlog's HTML output into your own templates or existing site structure.
Example usage:
gen := generator.New(fsys, config.WithRawOutput())
writer := outputter.NewDirectoryWriter("output/", config.WithRawOutput())
func WithSiteTitle ¶ added in v2.0.5
func WithSiteTitle(title string) GeneratorOption
WithSiteTitle returns an Option that sets the site title.
The site title is used in generated HTML pages and templates.
Example usage:
gen := generator.New(fsys, renderer, config.WithSiteTitle("My Blog"))
type RawOutput ¶ added in v2.0.5
type RawOutput struct{ RawOutput bool }
RawOutput is a configuration type that controls whether HTML output is generated with or without template wrapping.
When RawOutput is true:
- The generator produces only Markdown-to-HTML conversion without templates
- The outputter skips creating the tags directory
- Individual post files contain raw HTML fragments
This type is typically embedded in generator and outputter configuration structs and should be set using the WithRawOutput() option function.
func (RawOutput) AsOption ¶
func (o RawOutput) AsOption() GeneratorOption
type ServerConfig ¶
type ServerConfig struct {
Server []BaseServerOption
Gen []GeneratorOption
TemplateDir fs.FS
}
type SiteTitle ¶ added in v2.0.5
type SiteTitle struct{ SiteTitle string }
SiteTitle is a configuration type that holds the site's title.
This type is typically embedded in generator configuration structs and should be set using the WithSiteTitle() option function.
func (SiteTitle) AsOption ¶
func (o SiteTitle) AsOption() GeneratorOption