Skip to content

Commit

Permalink
Add Config.Options() and Server.Stop()
Browse files Browse the repository at this point in the history
  • Loading branch information
infogulch committed Apr 26, 2024
1 parent 3716926 commit bc47455
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 11 deletions.
3 changes: 3 additions & 0 deletions TODO.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,9 @@
- [ ] Schema generator: https://gitlab.com/Screwtapello/sqlite-schema-diagram/-/blob/main/sqlite-schema-diagram.sql?ref_type=heads
- [ ] Add a way to register additional routes dynamically during init
- [ ] Organize docs according to https://diataxis.fr/
- [ ] Research alternative template loading strategies:
- https://github.com/claceio/clace/blob/898932c1766d3e063c67caf1b9a744777fa437c3/internal/app/app.go#L261
- https://github.com/unrolled/render

# DONE

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

func (c *Config) Options(options ...Option) (*Config, error) {
for _, o := range options {
if err := o(c); err != nil {
return nil, fmt.Errorf("failed to apply xtemplate config option: %w", err)
}
}
return c, nil
}

type Option func(*Config) error

func WithTemplateFS(fs fs.FS) Option {
Expand Down
7 changes: 2 additions & 5 deletions instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,8 @@ type Instance struct {
func (config Config) Instance(cfgs ...Option) (*Instance, *InstanceStats, []InstanceRoute, error) {
start := time.Now()

config.Defaults()
for _, c := range cfgs {
if err := c(&config); err != nil {
return nil, nil, nil, fmt.Errorf("failed to configure instance: %w", err)
}
if _, err := config.Defaults().Options(cfgs...); err != nil {
return nil, nil, nil, err
}

build := &builder{
Expand Down
19 changes: 13 additions & 6 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package xtemplate

import (
"context"
"fmt"
"log/slog"
"net/http"
"sync"
Expand Down Expand Up @@ -30,11 +29,8 @@ type Server struct {

// Build creates a new Server from an xtemplate.Config.
func (config Config) Server(cfgs ...Option) (*Server, error) {
config.Defaults()
for _, c := range cfgs {
if err := c(&config); err != nil {
return nil, fmt.Errorf("failed to configure server: %w", err)
}
if _, err := config.Defaults().Options(cfgs...); err != nil {
return nil, err
}

config.Logger = config.Logger.WithGroup("xtemplate")
Expand Down Expand Up @@ -107,3 +103,14 @@ func (x *Server) Reload(cfgs ...Option) error {
log.Info("rebuild succeeded", slog.Int64("new_id", new_.id), slog.Duration("rebuild_time", time.Since(start)))
return nil
}

func (x *Server) Stop() {
x.mutex.Lock()
defer x.mutex.Unlock()

if x.cancel != nil {
x.cancel()
}
x.cancel = nil
x.instance.Store(nil)
}

0 comments on commit bc47455

Please sign in to comment.