Skip to content

Commit

Permalink
Fix nil tx; fix route path mapping
Browse files Browse the repository at this point in the history
  • Loading branch information
infogulch committed Oct 13, 2023
1 parent 1e382f5 commit d1caf07
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 7 deletions.
3 changes: 3 additions & 0 deletions bin/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ go build -o xtemplate

# build from repo root
go build -o xtemplate ./bin

# build with sqlite3 driver and json extensions
GOFLAGS='-tags="sqlite_json"' CGO_ENABLED=1 go build -o xtemplate
```

### Usage
Expand Down
6 changes: 4 additions & 2 deletions serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,10 @@ func (t *XTemplate) ServeHTTP(w http.ResponseWriter, r *http.Request) {
return
}
log.Info("error executing template", "error", err)
if dberr := tx.Rollback(); dberr != nil {
log.Info("failed to roll back transaction", "error", err)
if tx != nil {
if dberr := tx.Rollback(); dberr != nil {
log.Info("failed to roll back transaction", "error", dberr)
}
}
http.Error(w, "failed to render response", http.StatusInternalServerError)
return
Expand Down
10 changes: 5 additions & 5 deletions templates.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,12 +92,12 @@ func (t *XTemplate) Reload() error {
}
// add the route handler template
if !strings.HasPrefix(filepath.Base(path_), "_") {
path_ = strings.TrimSuffix(path_, filepath.Ext(path_))
if path.Base(path_) == "index" {
path_ = path.Dir(path_)
routePath := strings.TrimSuffix(path_, filepath.Ext(path_))
if path.Base(routePath) == "index" {
routePath = path.Dir(routePath)
}
route := "GET " + path_
log.Debug("adding filename route template", "route", route, "path", path_)
route := "GET " + routePath
log.Debug("adding filename route template", "route", route, "routePath", routePath, "path", path_)
_, err = templates.AddParseTree(route, newtemplates[path_])
if err != nil {
return fmt.Errorf("could not add parse tree from '%s': %v", path_, err)
Expand Down

0 comments on commit d1caf07

Please sign in to comment.