fix watcher to watch newly added dirs

add utility methods to RequestContext

Fix GetPartialPath
This commit is contained in:
maddalax 2024-10-09 10:28:41 -05:00
parent c4b1df8a6d
commit f3cb95960c
3 changed files with 69 additions and 3 deletions

View file

@ -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() {

View file

@ -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)

View file

@ -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 {