Skip to content

Commit

Permalink
Make overrides public
Browse files Browse the repository at this point in the history
  • Loading branch information
infogulch committed Mar 10, 2024
1 parent 01f22e3 commit e91291c
Show file tree
Hide file tree
Showing 6 changed files with 9 additions and 13 deletions.
2 changes: 1 addition & 1 deletion TODO.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
- [x] Reorganize to expose different layers:
- Config: Configure xtemplate
- Instance: Serves as the local context and http.Handler
- Server: Manages instances and lets you live reload instances.
- Server: Manages instances and lets you live reload live.

## v0.3 - Mar 2024

Expand Down
12 changes: 6 additions & 6 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,33 +98,33 @@ func (config *Config) Defaults() *Config {
return config
}

type override func(*Config)
type ConfigOverride func(*Config)

func WithTemplateFS(fs fs.FS) override {
func WithTemplateFS(fs fs.FS) ConfigOverride {
return func(c *Config) {
c.Template.FS = fs
}
}

func WithContextFS(fs fs.FS) override {
func WithContextFS(fs fs.FS) ConfigOverride {
return func(c *Config) {
c.Context.FS = fs
}
}

func WithDB(db *sql.DB) override {
func WithDB(db *sql.DB) ConfigOverride {
return func(c *Config) {
c.Database.DB = db
}
}

func WithLogger(logger *slog.Logger) override {
func WithLogger(logger *slog.Logger) ConfigOverride {
return func(c *Config) {
c.Logger = logger
}
}

func WithFuncMaps(fm ...template.FuncMap) override {
func WithFuncMaps(fm ...template.FuncMap) ConfigOverride {
return func(c *Config) {
c.FuncMaps = append(c.FuncMaps, fm...)
}
Expand Down
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ require (
github.com/tdewolff/minify/v2 v2.20.18
github.com/yuin/goldmark v1.7.0
github.com/yuin/goldmark-highlighting/v2 v2.0.0-20230729083705-37449abec8cc
golang.org/x/exp v0.0.0-20240222234643-814bf88cf225
gopkg.in/yaml.v3 v3.0.1
)

Expand Down
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,6 @@ golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5y
golang.org/x/crypto v0.3.0/go.mod h1:hebNnKkNXi2UzZN1eVRvBB7co0a+JxK6XbPiWVs/3J4=
golang.org/x/crypto v0.19.0 h1:ENy+Az/9Y1vSrlrvBSyna3PITt4tiZLf7sgCjZBX7Wo=
golang.org/x/crypto v0.19.0/go.mod h1:Iy9bg/ha4yyC70EfRS8jz+B6ybOBKMaSxLj6P6oBDfU=
golang.org/x/exp v0.0.0-20240222234643-814bf88cf225 h1:LfspQV/FYTatPTr/3HzIcmiUFH7PGP+OQ6mgDYo3yuQ=
golang.org/x/exp v0.0.0-20240222234643-814bf88cf225/go.mod h1:CxmFvTBINI24O/j8iY7H1xHzx2i4OsyguNBmN/uPtqc=
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
Expand Down
1 change: 0 additions & 1 deletion instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,6 @@ func (config Config) instance() (*xinstance, error) {
}

inst.Logger.Info("instance loaded",
slog.Int64("id", inst.id),
slog.Duration("load_time", time.Since(start)),
slog.Group("stats",
slog.Int("routes", inst.stats.Routes),
Expand Down
4 changes: 2 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ func parseflags() (f flags) {
// Main can be called from your func main() if you want your program to act like
// the default xtemplate cli, or use it as a reference for making your own.
// Provide configs to override the defaults like: `xtemplate.Main(xtemplate.WithFooConfig())`
func Main(overrides ...override) {
func Main(overrides ...ConfigOverride) {
flags := parseflags()
log := slog.New(slog.NewTextHandler(os.Stdout, &slog.HandlerOptions{Level: slog.Level(flags.config.LogLevel)}))
flags.config.Logger = log
Expand Down Expand Up @@ -123,7 +123,7 @@ func Main(overrides ...override) {
}
}

server.Serve(flags.listen_addr)
log.Info("server stopped", server.Serve(flags.listen_addr))
}

// Implement flag.Value.String to use UserConfigs as a flag.
Expand Down

0 comments on commit e91291c

Please sign in to comment.