diff --git a/build.go b/build.go index 1fc7d73..b0260f5 100644 --- a/build.go +++ b/build.go @@ -49,7 +49,7 @@ type xbuilder struct { } // Build creates a new xtemplate server instance, a `CancelHandler`, from an xtemplate.Config. -func Build(config *Config) (CancelHandler, error) { +func Build(config Config) (CancelHandler, error) { builder, err := newBuilder(config) if err != nil { return nil, err @@ -105,24 +105,16 @@ func Build(config *Config) (CancelHandler, error) { var nextInstanceIdentity int64 // newBuilder creates an empty xserver with all data structures initalized using the provided config. -func newBuilder(config *Config) (*xbuilder, error) { +func newBuilder(config Config) (*xbuilder, error) { + config.FillDefaults() server := &xserver{ - Config: *config, + Config: config, } if server.Template.FS == nil { - if server.Template.Path == "" { - server.Template.Path = "templates" - config.Template.Path = "templates" - } server.Template.FS = os.DirFS(server.Template.Path) } - if server.Template.TemplateExtension == "" { - server.Template.TemplateExtension = ".html" - config.Template.TemplateExtension = ".html" - } - if server.Context.FS == nil && server.Context.Path != "" { server.Context.FS = os.DirFS(server.Context.Path) } diff --git a/config.go b/config.go index 9bfb4b9..e3e3acf 100644 --- a/config.go +++ b/config.go @@ -11,9 +11,7 @@ import ( func New() (c *Config) { c = &Config{} - c.Template.Path = "templates" - c.UserConfig = make(map[string]string) - c.Template.TemplateExtension = ".html" + c.FillDefaults() return } @@ -70,14 +68,40 @@ type Config struct { LogLevel int `json:"log_level,omitempty"` } +// UserConfig are key-value pairs made available to the template context as .Config type UserConfig map[string]string -func (c *Config) WithTemplateFS(fs fs.FS) { - c.Template.FS = fs +// FillDefaults sets default values for unset fields +func (config *Config) FillDefaults() { + if config.Template.Path == "" { + config.Template.Path = "templates" + } + + if config.Template.TemplateExtension == "" { + config.Template.TemplateExtension = ".html" + } + + if config.Template.Delimiters.Left == "" { + config.Template.Delimiters.Left = "{{" + } + + if config.Template.Delimiters.Right == "" { + config.Template.Delimiters.Right = "}}" + } + + if config.UserConfig == nil { + config.UserConfig = make(map[string]string) + } } type override func(*Config) +func WithTemplateFS(fs fs.FS) override { + return func(c *Config) { + c.Template.FS = fs + } +} + func WithContextFS(fs fs.FS) override { return func(c *Config) { c.Context.FS = fs diff --git a/main.go b/main.go index df32edb..6cd6b37 100644 --- a/main.go +++ b/main.go @@ -57,6 +57,7 @@ var helptext = `xtemplate is a hypertext preprocessor and html templating http s ` func parseflags() (f flags) { + f.config = *New() flag.StringVar(&f.listen_addr, "listen", "0.0.0.0:8080", "Listen address") flag.StringVar(&f.config.Template.Path, "template-path", "templates", "Directory where templates are loaded from") flag.BoolVar(&f.watch_template_path, "watch-template", true, "Watch the template directory and reload if changed") @@ -91,7 +92,7 @@ func Main(overrides ...override) { for _, o := range overrides { o(&flags.config) } - handler, err := Build(&flags.config) + handler, err := Build(flags.config) if err != nil { log.Error("failed to load xtemplate", slog.Any("error", err)) os.Exit(2) @@ -113,7 +114,7 @@ func Main(overrides ...override) { if len(watchDirs) != 0 { _, err := watch.Watch(watchDirs, 200*time.Millisecond, log.WithGroup("fswatch"), func() bool { log := log.With(slog.Group("reload", slog.Int64("current_id", handler.Id()))) - temphandler, err := Build(&flags.config) + temphandler, err := Build(flags.config) if err != nil { log.Info("failed to reload xtemplate", slog.Any("error", err)) } else {