diff --git a/cli/htmgo/watcher.go b/cli/htmgo/watcher.go index f2a0ee8..809e7e0 100644 --- a/cli/htmgo/watcher.go +++ b/cli/htmgo/watcher.go @@ -35,10 +35,37 @@ func startWatcher(cb func(version string, file []*fsnotify.Event)) { for { select { case event, ok := <-watcher.Events: + slog.Debug("event:", slog.String("name", event.Name), slog.String("op", event.Op.String())) + if !ok { return } - slog.Debug("event:", slog.String("name", event.Name), slog.String("op", event.Op.String())) + + if event.Has(fsnotify.Remove) { + err = watcher.Remove(event.Name) + if err != nil { + slog.Error("Error removing directory from watcher:", slog.String("path", event.Name), slog.String("error", err.Error())) + } else { + slog.Debug("Stopped watching directory:", slog.String("path", event.Name)) + } + } + + if event.Has(fsnotify.Create) { + info, err := os.Stat(event.Name) + if err != nil { + slog.Error("Error getting file info:", slog.String("path", event.Name), slog.String("error", err.Error())) + continue + } + if info.IsDir() { + err = watcher.Add(event.Name) + if err != nil { + slog.Error("Error adding directory to watcher:", slog.String("path", event.Name), slog.String("error", err.Error())) + } else { + slog.Debug("Watching directory:", slog.String("path", event.Name)) + } + } + } + if event.Has(fsnotify.Write) || event.Has(fsnotify.Remove) || event.Has(fsnotify.Rename) { events = append(events, &event) debouncer.Do(func() { diff --git a/framework/h/app.go b/framework/h/app.go index d369f7d..f18e385 100644 --- a/framework/h/app.go +++ b/framework/h/app.go @@ -29,14 +29,54 @@ type RequestContext struct { kv map[string]interface{} } +func GetRequestContext(r *http.Request) *RequestContext { + return r.Context().Value(RequestContextKey).(*RequestContext) +} + func (c *RequestContext) FormValue(key string) string { return c.Request.FormValue(key) } +func (c *RequestContext) Header(key string) string { + return c.Request.Header.Get(key) +} + +func (c *RequestContext) UrlParam(key string) string { + return chi.URLParam(c.Request, key) +} + func (c *RequestContext) QueryParam(key string) string { return c.Request.URL.Query().Get(key) } +func (c *RequestContext) IsBoosted() bool { + return c.isBoosted +} + +func (c *RequestContext) IsHxRequest() bool { + return c.isHxRequest +} + +func (c *RequestContext) HxPromptResponse() string { + return c.hxPromptResponse +} + +func (c *RequestContext) HxTargetId() string { + return c.hxTargetId +} + +func (c *RequestContext) HxTriggerName() string { + return c.hxTriggerName +} + +func (c *RequestContext) HxTriggerId() string { + return c.hxTriggerId +} + +func (c *RequestContext) HxCurrentBrowserUrl() string { + return c.CurrentBrowserUrl +} + func (c *RequestContext) Set(key string, value interface{}) { if c.kv == nil { c.kv = make(map[string]interface{}) @@ -78,7 +118,6 @@ func Start(opts AppOpts) { const RequestContextKey = "htmgo.request.context" func populateHxFields(cc *RequestContext) { - cc.isBoosted = cc.Request.Header.Get(hx.BoostedHeader) == "true" cc.isBoosted = cc.Request.Header.Get(hx.BoostedHeader) == "true" cc.CurrentBrowserUrl = cc.Request.Header.Get(hx.CurrentUrlHeader) cc.hxPromptResponse = cc.Request.Header.Get(hx.PromptResponseHeader) diff --git a/framework/h/base.go b/framework/h/base.go index 1ec82a1..ec0030f 100644 --- a/framework/h/base.go +++ b/framework/h/base.go @@ -85,7 +85,7 @@ func SwapManyXPartial(ctx *RequestContext, swaps ...SwapArg) *Partial { } func GetPartialPath(partial PartialFunc) string { - return runtime.FuncForPC(reflect.ValueOf(partial).Pointer()).Name() + return "/" + runtime.FuncForPC(reflect.ValueOf(partial).Pointer()).Name() } func GetPartialPathWithQs(partial func(ctx *RequestContext) *Partial, qs *Qs) string {