Skip to content

Commit

Permalink
Refactor file handler, part 2
Browse files Browse the repository at this point in the history
  • Loading branch information
infogulch committed Oct 19, 2023
1 parent fdfe49e commit 2e219e8
Show file tree
Hide file tree
Showing 9 changed files with 324 additions and 259 deletions.
28 changes: 13 additions & 15 deletions caddy/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,9 @@ type XTemplateModule struct {

FuncsModules []string `json:"funcs_modules,omitempty"`

template *xtemplate.XTemplate
halt chan<- struct{}
handler http.Handler
db *sql.DB
halt chan<- struct{}
}

type FuncsProvider interface {
Expand Down Expand Up @@ -136,6 +137,7 @@ func (m *XTemplateModule) Provision(ctx caddy.Context) error {
return err
}
t.DB = db
m.db = db
}

if len(m.Delimiters) != 0 {
Expand All @@ -147,25 +149,25 @@ func (m *XTemplateModule) Provision(ctx caddy.Context) error {
}

{
err := t.Reload()
h, err := t.Build()

Check failure on line 152 in caddy/module.go

View workflow job for this annotation

GitHub Actions / build

t.Build undefined (type *xtemplate.XTemplate has no field or method Build)
if err != nil {
return err
}
m.handler = h
}

m.template = t

if len(watchDirs) > 0 {
changed, halt, err := watch.WatchDirs(watchDirs, 200*time.Millisecond)
if err != nil {
return err
}
m.halt = halt
watch.React(changed, halt, func() (halt bool) {
err := t.Reload()
newhandler, err := t.Build()

Check failure on line 166 in caddy/module.go

View workflow job for this annotation

GitHub Actions / build

t.Build undefined (type *xtemplate.XTemplate has no field or method Build)
if err != nil {
log.Info("failed to reload xtemplate", "error", err)
} else {
m.handler = newhandler
log.Info("reloaded templates after file changed")
}
return
Expand All @@ -175,7 +177,7 @@ func (m *XTemplateModule) Provision(ctx caddy.Context) error {
}

func (m *XTemplateModule) ServeHTTP(w http.ResponseWriter, r *http.Request, _ caddyhttp.Handler) error {
m.template.ServeHTTP(w, r)
m.handler.ServeHTTP(w, r)
return nil
}

Expand All @@ -186,14 +188,10 @@ func (m *XTemplateModule) Cleanup() error {
close(m.halt)
m.halt = nil
}
if m.template != nil {
var dberr error
if m.template.DB != nil {
dberr = m.template.DB.Close()
m.template.DB = nil
}
m.template = nil
return dberr
if m.db != nil {
err := m.db.Close()
m.db = nil
return err
}
return nil
}
Expand Down
7 changes: 4 additions & 3 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ func main() {
DB: db,
Log: log.WithGroup("xtemplate"),
}
err = x.Reload()
handler, err := x.Build()
if err != nil {
log.Error("failed to load xtemplate", "error", err)
os.Exit(2)
Expand All @@ -109,10 +109,11 @@ func main() {
os.Exit(4)
}
watch.React(changed, halt, func() (halt bool) {
err := x.Reload()
newhandler, err := x.Build()
if err != nil {
log.Info("failed to reload xtemplate", "error", err)
} else {
handler = newhandler
log.Info("reloaded templates after file changed")
}
return
Expand All @@ -121,7 +122,7 @@ func main() {
}

log.Info("serving", "address", flags.listen_addr)
fmt.Printf("server stopped: %v\n", http.ListenAndServe(flags.listen_addr, &x))
fmt.Printf("server stopped: %v\n", http.ListenAndServe(flags.listen_addr, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { handler.ServeHTTP(w, r) })))
}

type kv struct{ Key, Value string }
Expand Down
2 changes: 2 additions & 0 deletions integration/templates/subdir/file.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<!DOCTYPE html>
hello!
2 changes: 2 additions & 0 deletions integration/templates/subdir/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<!DOCTYPE html>
subdir
19 changes: 19 additions & 0 deletions integration/tests/routing.hurl
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# index
GET http://localhost:8080/

HTTP 200
[Asserts]
body contains "<p>Hello world!</p>"

# subdir
GET http://localhost:8080/subdir

HTTP 200
[Asserts]
body contains "subdir"

GET http://localhost:8080/subdir/file

HTTP 200
[Asserts]
body contains "hello!"
6 changes: 6 additions & 0 deletions integration/tests/templates.hurl
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# index
GET http://localhost:8080/visible

HTTP 200
[Asserts]
body contains "<p>You can't see me</p>"
Loading

0 comments on commit 2e219e8

Please sign in to comment.