Skip to content

Commit

Permalink
Use method to set defaults on config
Browse files Browse the repository at this point in the history
  • Loading branch information
infogulch committed Mar 6, 2024
1 parent a54a404 commit 71af297
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 19 deletions.
16 changes: 4 additions & 12 deletions build.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
}
Expand Down
34 changes: 29 additions & 5 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down Expand Up @@ -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
Expand Down
5 changes: 3 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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)
Expand All @@ -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 {
Expand Down

0 comments on commit 71af297

Please sign in to comment.