Skip to content

Commit f2f5a08

Browse files
committed
feat: use config dir for Bleve index and ensure directory
1 parent ac2ee3b commit f2f5a08

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

internal/nginx_log/modern_services.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,16 @@ package nginx_log
33
import (
44
"context"
55
"fmt"
6+
"os"
7+
"path/filepath"
68
"sync"
79

810
"github.com/0xJacky/Nginx-UI/internal/nginx_log/analytics"
911
"github.com/0xJacky/Nginx-UI/internal/nginx_log/indexer"
1012
"github.com/0xJacky/Nginx-UI/internal/nginx_log/searcher"
1113
"github.com/blevesearch/bleve/v2"
1214
"github.com/uozi-tech/cosy/logger"
15+
cSettings "github.com/uozi-tech/cosy/settings"
1316
)
1417

1518
// Global instances for new services
@@ -72,6 +75,8 @@ func initializeWithDefaults(ctx context.Context) error {
7275

7376
// Initialize parallel indexer with shard manager
7477
indexerConfig := indexer.DefaultIndexerConfig()
78+
// Use config directory for index path
79+
indexerConfig.IndexPath = getConfigDirIndexPath()
7580
shardManager := indexer.NewDefaultShardManager(indexerConfig)
7681
globalIndexer = indexer.NewParallelIndexer(indexerConfig, shardManager)
7782

@@ -94,6 +99,28 @@ func initializeWithDefaults(ctx context.Context) error {
9499
return nil
95100
}
96101

102+
// getConfigDirIndexPath returns the index path relative to the config file directory
103+
func getConfigDirIndexPath() string {
104+
// Get the config file path from cosy settings
105+
if cSettings.ConfPath != "" {
106+
configDir := filepath.Dir(cSettings.ConfPath)
107+
indexPath := filepath.Join(configDir, "log-index")
108+
109+
// Ensure the directory exists
110+
if err := os.MkdirAll(indexPath, 0755); err != nil {
111+
logger.Warnf("Failed to create index directory at %s: %v, using default", indexPath, err)
112+
return "./log-index"
113+
}
114+
115+
logger.Infof("Using index path: %s", indexPath)
116+
return indexPath
117+
}
118+
119+
// Fallback to default relative path
120+
logger.Warn("Config file path not available, using default index path")
121+
return "./log-index"
122+
}
123+
97124
// GetModernSearcher returns the global searcher instance
98125
func GetModernSearcher() searcher.Searcher {
99126
servicesMutex.RLock()

0 commit comments

Comments
 (0)