diff --git a/internal/domain/content/entity/build.go b/internal/domain/content/entity/build.go index 0fcc2f95..3f76b063 100644 --- a/internal/domain/content/entity/build.go +++ b/internal/domain/content/entity/build.go @@ -141,10 +141,8 @@ func (c *Content) copyFiles(dir string, parentPath string, files []string) { defer wg.Done() if err := c.copyFile(src, dst); err != nil { - c.Log.Debugf("file %s copy failed: %v\n", src, err) return } - c.Log.Debugf("file %s copied to %s successfully\n", src, dst) }(src, dst) } diff --git a/internal/domain/content/entity/search.go b/internal/domain/content/entity/search.go index bfc8e2ef..eaa050ff 100644 --- a/internal/domain/content/entity/search.go +++ b/internal/domain/content/entity/search.go @@ -11,6 +11,7 @@ import ( "os" "path/filepath" "strings" + "sync" ) type TypeService interface { @@ -26,6 +27,7 @@ type Search struct { Repo repository.Repository Log loggers.Logger + mu sync.RWMutex IndicesMap map[string]map[string]bleve.Index } @@ -33,6 +35,9 @@ type Search struct { // and an error. If there is no search index for the typeName (Type) provided, // db.ErrNoIndex will be returned as the error func (s *Search) TypeQuery(typeName, query string, count, offset int) ([]content.Identifier, error) { + s.mu.RLock() + defer s.mu.Unlock() + s.setup() idx, ok := s.IndicesMap[s.getSearchDir(typeName)][typeName] @@ -54,12 +59,20 @@ func (s *Search) TypeQuery(typeName, query string, count, offset int) ([]content results = append(results, valueobject.CreateIndex(hit.ID)) } + if closeErr := idx.Close(); closeErr != nil { + s.Log.Printf("Failed to close index: %v", closeErr) + } + delete(s.IndicesMap, s.getSearchDir(typeName)) + return results, nil } // UpdateIndex sets data into a content type's search index at the given // identifier func (s *Search) UpdateIndex(ns, id string, data []byte) error { + s.mu.RLock() + defer s.mu.Unlock() + s.setup() idx, ok := s.IndicesMap[s.getSearchDir(ns)][ns] @@ -78,7 +91,14 @@ func (s *Search) UpdateIndex(ns, id string, data []byte) error { // add data to search index i := valueobject.NewIndex(ns, id) - return idx.Index(i.String(), p) + err = idx.Index(i.String(), p) + + if closeErr := idx.Close(); closeErr != nil { + s.Log.Printf("Failed to close index: %v", closeErr) + } + delete(s.IndicesMap, s.getSearchDir(ns)) + + return err } return nil @@ -87,6 +107,9 @@ func (s *Search) UpdateIndex(ns, id string, data []byte) error { // DeleteIndex removes data from a content type's search index at the // given identifier func (s *Search) DeleteIndex(id string) error { + s.mu.RLock() + defer s.mu.Unlock() + s.setup() // check if there is a search index to work with @@ -96,7 +119,14 @@ func (s *Search) DeleteIndex(id string) error { idx, ok := s.IndicesMap[s.getSearchDir(ns)][ns] if ok { // add data to search index - return idx.Delete(id) + err := idx.Delete(id) + + if closeErr := idx.Close(); closeErr != nil { + s.Log.Printf("Failed to close index: %v", closeErr) + } + delete(s.IndicesMap, s.getSearchDir(ns)) + + return err } return nil @@ -117,11 +147,6 @@ func (s *Search) adminSearchDir() string { return filepath.Join(s.Repo.AdminDataDir(), "Search") } -// Setup initializes Search Index for search to be functional -// This was moved out of db.Init and put to main(), because addon checker was initializing db together with -// search indexing initialisation in time when there were no item.Types defined so search index was always -// empty when using addons. We still have no guarentee whatsoever that item.Types is defined -// Should be called from a goroutine after SetContent is successful (SortContent requirement) func (s *Search) setup() { s.setupAdminIndices() s.setupUserIndices() @@ -138,6 +163,7 @@ func (s *Search) setupAdminIndices() { func (s *Search) setupIndices(dir string, typeNames []string) { _, ok := s.IndicesMap[dir] if ok { + s.Log.Debugln("[search] Setup found: Index already exists for ", dir) return } @@ -195,15 +221,16 @@ func (s *Search) mapIndex(typeName string) (bleve.Index, error) { if err != nil { return nil, err } - idx.SetName(idxName) + s.Log.Debugf("[search] Index new created for %s\n", typeName) } else { idx, err = bleve.Open(idxPath) if err != nil { return nil, err } + s.Log.Debugf("[search] Index open created for %s\n", typeName) } - s.Log.Debugf("[search] Index created for %s\n", typeName) + idx.SetName(idxName) return idx, nil } diff --git a/internal/domain/content/valueobject/domain.go b/internal/domain/content/valueobject/domain.go index 3a4feac9..9bc7bf30 100644 --- a/internal/domain/content/valueobject/domain.go +++ b/internal/domain/content/valueobject/domain.go @@ -3,7 +3,6 @@ package valueobject import ( "fmt" "github.com/gohugonet/hugoverse/pkg/editor" - "log" "net/http" ) @@ -92,9 +91,6 @@ func (d *Domain) BeforeAPICreate(res http.ResponseWriter, req *http.Request) err // The request has a context containing the databse 'target' affected by the // request. Ex. Song__pending:3 or Song:8 depending if Song implements api.Trustable func (d *Domain) AfterAPICreate(res http.ResponseWriter, req *http.Request) error { - addr := req.RemoteAddr - log.Println("[AfterAPICreate] Domain sent by:", addr, "sub domain:", req.PostFormValue("sub")) - return nil } diff --git a/internal/domain/content/valueobject/post.go b/internal/domain/content/valueobject/post.go index c034556b..ac0e0580 100644 --- a/internal/domain/content/valueobject/post.go +++ b/internal/domain/content/valueobject/post.go @@ -4,7 +4,6 @@ import ( "bytes" "fmt" "github.com/gohugonet/hugoverse/pkg/editor" - "log" "net/http" "text/template" ) @@ -111,9 +110,6 @@ func (s *Post) BeforeAPICreate(res http.ResponseWriter, req *http.Request) error // The request has a context containing the databse 'target' affected by the // request. Ex. Song__pending:3 or Song:8 depending if Song implements api.Trustable func (s *Post) AfterAPICreate(res http.ResponseWriter, req *http.Request) error { - addr := req.RemoteAddr - log.Println("[AfterAPICreate] Post sent by:", addr, "titled:", req.PostFormValue("title")) - return nil } diff --git a/internal/domain/content/valueobject/site.go b/internal/domain/content/valueobject/site.go index 97b1f9c2..99c4052e 100644 --- a/internal/domain/content/valueobject/site.go +++ b/internal/domain/content/valueobject/site.go @@ -4,7 +4,6 @@ import ( "bytes" "fmt" "github.com/gohugonet/hugoverse/pkg/editor" - "log" "net/http" "text/template" ) @@ -130,9 +129,6 @@ func (s *Site) BeforeAPICreate(res http.ResponseWriter, req *http.Request) error // The request has a context containing the databse 'target' affected by the // request. Ex. Song__pending:3 or Song:8 depending if Song implements api.Trustable func (s *Site) AfterAPICreate(res http.ResponseWriter, req *http.Request) error { - addr := req.RemoteAddr - log.Println("[AfterAPICreate] Site sent by:", addr, "titled:", req.PostFormValue("title")) - return nil } diff --git a/internal/domain/content/valueobject/sitelanguage.go b/internal/domain/content/valueobject/sitelanguage.go index 27244d33..f88a4a7b 100644 --- a/internal/domain/content/valueobject/sitelanguage.go +++ b/internal/domain/content/valueobject/sitelanguage.go @@ -3,7 +3,6 @@ package valueobject import ( "fmt" "github.com/gohugonet/hugoverse/pkg/editor" - "log" "net/http" "strings" ) @@ -124,9 +123,6 @@ func (s *SiteLanguage) BeforeAPICreate(res http.ResponseWriter, req *http.Reques // The request has a context containing the databse 'target' affected by the // request. Ex. Song__pending:3 or Song:8 depending if Song implements api.Trustable func (s *SiteLanguage) AfterAPICreate(res http.ResponseWriter, req *http.Request) error { - addr := req.RemoteAddr - log.Println("[AfterAPICreate] SiteLanguage sent by:", addr, "titled:", req.PostFormValue("title")) - return nil } diff --git a/internal/domain/content/valueobject/sitepost.go b/internal/domain/content/valueobject/sitepost.go index 2df741ed..7b923596 100644 --- a/internal/domain/content/valueobject/sitepost.go +++ b/internal/domain/content/valueobject/sitepost.go @@ -3,7 +3,6 @@ package valueobject import ( "fmt" "github.com/gohugonet/hugoverse/pkg/editor" - "log" "net/http" "strings" ) @@ -116,9 +115,6 @@ func (s *SitePost) BeforeAPICreate(res http.ResponseWriter, req *http.Request) e // The request has a context containing the databse 'target' affected by the // request. Ex. Song__pending:3 or Song:8 depending if Song implements api.Trustable func (s *SitePost) AfterAPICreate(res http.ResponseWriter, req *http.Request) error { - addr := req.RemoteAddr - log.Println("AfterAPICreate: SitePost sent by:", addr, "titled:", req.PostFormValue("title")) - return nil } diff --git a/internal/domain/content/valueobject/theme.go b/internal/domain/content/valueobject/theme.go index 86c4a2a9..4759b8c5 100644 --- a/internal/domain/content/valueobject/theme.go +++ b/internal/domain/content/valueobject/theme.go @@ -3,7 +3,6 @@ package valueobject import ( "fmt" "github.com/gohugonet/hugoverse/pkg/editor" - "log" "net/http" ) @@ -115,9 +114,6 @@ func (s *Theme) BeforeAPICreate(res http.ResponseWriter, req *http.Request) erro // The request has a context containing the databse 'target' affected by the // request. Ex. Song__pending:3 or Song:8 depending if Song implements api.Trustable func (s *Theme) AfterAPICreate(res http.ResponseWriter, req *http.Request) error { - addr := req.RemoteAddr - log.Println("[AfterAPICreate] Theme sent by:", addr, "titled:", req.PostFormValue("title")) - return nil } diff --git a/internal/domain/template/entity/template.go b/internal/domain/template/entity/template.go index 32ee440e..dde7de01 100644 --- a/internal/domain/template/entity/template.go +++ b/internal/domain/template/entity/template.go @@ -3,7 +3,6 @@ package entity import ( "bytes" "embed" - "fmt" "github.com/gohugonet/hugoverse/internal/domain/fs" "github.com/gohugonet/hugoverse/internal/domain/template" "github.com/gohugonet/hugoverse/internal/domain/template/valueobject" @@ -216,7 +215,7 @@ func (t *Template) addTemplate(name string, tinfo valueobject.TemplateInfo) erro t.Main.addTemplate(tinfo.Name, state) if err := t.Parser.Transform(t.Main, state); err != nil { - fmt.Println(tinfo.ErrWithFileContext("ast transform parse failed", err)) + return tinfo.ErrWithFileContext("ast transform parse failed", err) } return nil diff --git a/internal/domain/template/factory/builder.go b/internal/domain/template/factory/builder.go index 566e0dd8..25dd2e60 100644 --- a/internal/domain/template/factory/builder.go +++ b/internal/domain/template/factory/builder.go @@ -68,6 +68,7 @@ func (b *builder) withCfs(cfs template.CustomizedFunctions) *builder { } func (b *builder) buildFunctions() *builder { + valueobject.ResetTemplateFuncsNamespaceRegistry() valueobject.RegisterNamespaces() valueobject.RegisterCallbackNamespaces(b.tmpl.Execute) valueobject.RegisterExtendedNamespaces(b.cfs) diff --git a/internal/domain/template/valueobject/nsreg.go b/internal/domain/template/valueobject/nsreg.go index 670b2a9c..ba866116 100644 --- a/internal/domain/template/valueobject/nsreg.go +++ b/internal/domain/template/valueobject/nsreg.go @@ -8,6 +8,10 @@ import ( var TemplateFuncsNamespaceRegistry []func() *TemplateFuncsNamespace +func ResetTemplateFuncsNamespaceRegistry() { + TemplateFuncsNamespaceRegistry = nil +} + func AddTemplateFuncsNamespace(ns func() *TemplateFuncsNamespace) { TemplateFuncsNamespaceRegistry = append(TemplateFuncsNamespaceRegistry, ns) } diff --git a/internal/interfaces/api/database/db.go b/internal/interfaces/api/database/db.go index 50455ce8..305a8347 100644 --- a/internal/interfaces/api/database/db.go +++ b/internal/interfaces/api/database/db.go @@ -125,8 +125,6 @@ func (d *Database) PutContent(ci any, data []byte) error { bucket = fmt.Sprintf("%s%s", ns, bucketNameWithPrefix(string(status))) } - d.log.Printf(" === bucket: %s\n", bucket) - if err := d.getStore(ns).Set( &item{ bucket: bucket, diff --git a/internal/interfaces/api/handler/handlecontent.go b/internal/interfaces/api/handler/handlecontent.go index 9dbae8fe..314b82fe 100644 --- a/internal/interfaces/api/handler/handlecontent.go +++ b/internal/interfaces/api/handler/handlecontent.go @@ -229,7 +229,6 @@ func (s *Handler) postContent(res http.ResponseWriter, req *http.Request) { req.PostForm.Set("updated", ts) urlPaths, err := s.StoreFiles(req) - fmt.Printf("=== post form: %v\n", urlPaths) if err != nil { s.log.Errorf("Error storing files: %v", err) res.WriteHeader(http.StatusInternalServerError) @@ -303,7 +302,6 @@ func (s *Handler) postContent(res http.ResponseWriter, req *http.Request) { } req.PostForm.Set("namespace", t) - s.log.Printf("PostForm: %+v", req.PostForm) id, err := s.contentApp.NewContent(t, req.PostForm) if err != nil { diff --git a/internal/interfaces/api/handler/handleedit.go b/internal/interfaces/api/handler/handleedit.go index cd9f80ad..a5240ee3 100644 --- a/internal/interfaces/api/handler/handleedit.go +++ b/internal/interfaces/api/handler/handleedit.go @@ -187,7 +187,6 @@ func (s *Handler) EditHandler(res http.ResponseWriter, req *http.Request) { } req.PostForm.Set("namespace", pt) - s.log.Printf("PostForm: %+v", req.PostForm) if cid == "-1" { id, err := s.contentApp.NewContent(pt, req.PostForm) @@ -478,7 +477,6 @@ func (s *Handler) ApproveContentHandler(res http.ResponseWriter, req *http.Reque req.PostForm.Set("namespace", t) req.PostForm.Set("status", "public") - s.log.Printf("PostForm: %+v", req.PostForm) // Store the content in the bucket t id, err := s.contentApp.NewContent(t, req.PostForm) diff --git a/internal/interfaces/cli/vercurr.go b/internal/interfaces/cli/vercurr.go index 2e5e4f52..89b55df1 100644 --- a/internal/interfaces/cli/vercurr.go +++ b/internal/interfaces/cli/vercurr.go @@ -3,6 +3,6 @@ package cli var CurrentVersion = Version{ Major: 0, Minor: 0, - PatchLevel: 9, + PatchLevel: 10, Suffix: "", } diff --git a/manifest.json b/manifest.json index c56dea96..c4a0923b 100644 --- a/manifest.json +++ b/manifest.json @@ -1,5 +1,5 @@ { - "version": "0.0.9", + "version": "0.0.10", "name": "Hugoverse", "description": "Headless CMS for Hugo", "author": "sunwei",