Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions internal/bootstrap/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,8 @@ func InitConfig() {
convertAbsPath(&conf.Conf.Log.Name)
convertAbsPath(&conf.Conf.TempDir)
convertAbsPath(&conf.Conf.BleveDir)
convertAbsPath(&conf.Conf.Index115.DBFile)
convertAbsPath(&conf.Conf.Index115.BleveDir)
convertAbsPath(&conf.Conf.DistDir)

err := os.MkdirAll(conf.Conf.TempDir, 0o777)
Expand Down
58 changes: 58 additions & 0 deletions internal/bootstrap/index115.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package bootstrap

import (
"context"
"errors"
"time"

"github.com/OpenListTeam/OpenList/v4/internal/conf"
"github.com/OpenListTeam/OpenList/v4/internal/index115"
"github.com/OpenListTeam/OpenList/v4/internal/setting"
"github.com/OpenListTeam/OpenList/v4/server"
"github.com/OpenListTeam/OpenList/v4/server/handles"
log "github.com/sirupsen/logrus"
)

func InitIndex115() {
if err := InitIndex115Service(context.Background()); err != nil {
log.Errorf("init index115 error: %+v", err)
return
}
log.Info("index115 service initialized successfully")
}

func InitIndex115Service(ctx context.Context) error {
if conf.Conf == nil {
return errors.New("config not initialized")
}
if conf.Conf.Index115.DBFile == "" || conf.Conf.Index115.BleveDir == "" {
return errors.New("index115 paths not configured")
}
store, err := index115.OpenStoreRuntime(ctx, conf.Conf.Index115.DBFile)
if err != nil {
return err
}
searcher, err := index115.NewSearcher(ctx, store, conf.Conf.Index115.BleveDir)
if err != nil {
log.Warnf("index115 search disabled: %+v", err)
}
delay := time.Duration(index115DeleteDelaySeconds()) * time.Second
service := index115.NewService(
store,
searcher,
index115.NewLinkResolver(index115.NewDriver115ShareClient(), delay),
)
handles.SetIndex115Service(service)
server.SetIndex115BrowseService(service)
return nil
}

func index115DeleteDelaySeconds() int {
if conf.Conf == nil {
return 900
}
defer func() {
_ = recover()
}()
return setting.GetInt(conf.DeleteDelayTime, 900)
}
131 changes: 131 additions & 0 deletions internal/bootstrap/index115_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
package bootstrap

import (
"context"
"database/sql"
"os"
"path/filepath"
"testing"

"github.com/OpenListTeam/OpenList/v4/internal/conf"
"github.com/blevesearch/bleve/v2"
_ "github.com/glebarez/go-sqlite"
)

func TestInitIndex115ServiceReturnsErrorWhenManifestMissing(t *testing.T) {
conf.Conf = conf.DefaultConfig(t.TempDir())
conf.Conf.Index115.DBFile = filepath.Join(t.TempDir(), "missing.db")
conf.Conf.Index115.BleveDir = filepath.Join(t.TempDir(), "missing-bleve")

err := InitIndex115Service(context.Background())
if err == nil {
t.Fatal("expected init error")
}
}

func TestInitIndex115ServiceUsesConfiguredPaths(t *testing.T) {
rootDir := t.TempDir()
dbPath := filepath.Join(rootDir, "index.db")
bleveRoot := filepath.Join(rootDir, "bleve")
index, db := newRuntimeFixture(t, rootDir, dbPath)
defer func() { _ = db.Close() }()
if err := index.Close(); err != nil {
t.Fatalf("index.Close() error = %v", err)
}

conf.Conf = conf.DefaultConfig(rootDir)
conf.Conf.Index115.DBFile = dbPath
conf.Conf.Index115.BleveDir = bleveRoot

if err := InitIndex115Service(context.Background()); err != nil {
t.Fatalf("InitIndex115Service() error = %v", err)
}
}

func TestInitIndex115ServiceDegradesWhenBleveUnavailable(t *testing.T) {
rootDir := t.TempDir()
dbPath := filepath.Join(rootDir, "index.db")
db := openIndex115RuntimeDB(t, dbPath)
defer func() { _ = db.Close() }()
if _, err := db.Exec(`INSERT INTO index_manifest(id, version, index_path, status, built_at, file_count) VALUES (1, 1, ?, 'READY', 1, 1)`, "bleve/index_000001"); err != nil {
t.Fatalf("insert manifest error = %v", err)
}
if _, err := db.Exec(`INSERT INTO share(share_code, receive_code, share_title, status, last_crawled_at) VALUES ('sw1', 'rc1', 'Share One', 'ACTIVE', 1)`); err != nil {
t.Fatalf("insert share error = %v", err)
}

conf.Conf = conf.DefaultConfig(rootDir)
conf.Conf.Index115.DBFile = dbPath
conf.Conf.Index115.BleveDir = filepath.Join(rootDir, "bleve")

if err := InitIndex115Service(context.Background()); err != nil {
t.Fatalf("InitIndex115Service() error = %v", err)
}
}

func newRuntimeFixture(t *testing.T, rootDir, dbPath string) (closer interface{ Close() error }, db *sql.DB) {
t.Helper()

db = openIndex115RuntimeDB(t, dbPath)
indexDir := filepath.Join(rootDir, "bleve", "index_000001")
if _, err := db.Exec(`INSERT INTO index_manifest(id, version, index_path, status, built_at, file_count) VALUES (1, 1, ?, 'READY', 1, 1)`, "bleve/index_000001"); err != nil {
t.Fatalf("insert manifest error = %v", err)
}
if _, err := db.Exec(`INSERT INTO share(share_code, receive_code, share_title, status, last_crawled_at) VALUES ('sw1', 'rc1', 'Share One', 'ACTIVE', 1)`); err != nil {
t.Fatalf("insert share error = %v", err)
}
if err := os.MkdirAll(filepath.Dir(indexDir), 0o755); err != nil {
t.Fatalf("MkdirAll() error = %v", err)
}
index, err := bleve.New(indexDir, bleve.NewIndexMapping())
if err != nil {
t.Fatalf("bleve.New() error = %v", err)
}
return index, db
}

func openIndex115RuntimeDB(t *testing.T, dbPath string) *sql.DB {
t.Helper()
db, err := sql.Open("sqlite", dbPath)
if err != nil {
t.Fatalf("sql.Open() error = %v", err)
}
stmts := []string{
`CREATE TABLE share (
id INTEGER PRIMARY KEY AUTOINCREMENT,
share_code TEXT NOT NULL,
receive_code TEXT NOT NULL DEFAULT '',
share_title TEXT NOT NULL DEFAULT '',
status TEXT NOT NULL DEFAULT 'ACTIVE',
last_crawled_at INTEGER NOT NULL DEFAULT 0
);`,
`CREATE TABLE file (
file_id TEXT PRIMARY KEY,
share_code TEXT NOT NULL,
parent_id TEXT NOT NULL,
name TEXT NOT NULL,
path TEXT NOT NULL,
ext TEXT NOT NULL DEFAULT '',
size INTEGER NOT NULL DEFAULT 0,
is_dir INTEGER NOT NULL DEFAULT 0,
depth INTEGER NOT NULL DEFAULT 0,
sha1 TEXT NOT NULL DEFAULT '',
updated_at INTEGER,
crawled_at INTEGER NOT NULL DEFAULT 0
);`,
`CREATE TABLE index_manifest (
id INTEGER PRIMARY KEY CHECK (id = 1),
version INTEGER NOT NULL,
index_path TEXT NOT NULL,
status TEXT NOT NULL,
built_at INTEGER NOT NULL,
file_count INTEGER NOT NULL
);`,
}
for _, stmt := range stmts {
if _, err := db.Exec(stmt); err != nil {
t.Fatalf("db.Exec(%q) error = %v", stmt, err)
}
}
return db
}
1 change: 1 addition & 0 deletions internal/bootstrap/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ func Init() {
data.InitData()
InitStreamLimit()
InitIndex()
InitIndex115()
InitUpgradePatch()
}

Expand Down
11 changes: 11 additions & 0 deletions internal/conf/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,11 @@ type SFTP struct {
Listen string `json:"listen" env:"LISTEN"`
}

type Index115 struct {
DBFile string `json:"db_file" env:"DB_FILE"`
BleveDir string `json:"bleve_dir" env:"BLEVE_DIR"`
}

type Config struct {
Force bool `json:"force" env:"FORCE"`
SiteURL string `json:"site_url" env:"SITE_URL"`
Expand All @@ -130,13 +135,15 @@ type Config struct {
S3 S3 `json:"s3" envPrefix:"S3_"`
FTP FTP `json:"ftp" envPrefix:"FTP_"`
SFTP SFTP `json:"sftp" envPrefix:"SFTP_"`
Index115 Index115 `json:"index115" envPrefix:"INDEX115_"`
LastLaunchedVersion string `json:"last_launched_version"`
ProxyAddress string `json:"proxy_address" env:"PROXY_ADDRESS"`
}

func DefaultConfig(dataDir string) *Config {
tempDir := filepath.Join(dataDir, "temp")
indexDir := filepath.Join(dataDir, "bleve")
index115Dir := filepath.Join(dataDir, "index115")
logPath := filepath.Join(dataDir, "log/log.log")
dbPath := filepath.Join(dataDir, "data.db")
return &Config{
Expand All @@ -163,6 +170,10 @@ func DefaultConfig(dataDir string) *Config {
Index: "openlist",
},
BleveDir: indexDir,
Index115: Index115{
DBFile: filepath.Join(index115Dir, "index.db"),
BleveDir: filepath.Join(index115Dir, "bleve"),
},
Log: LogConfig{
Enable: true,
Name: logPath,
Expand Down
16 changes: 16 additions & 0 deletions internal/conf/index115_config_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package conf

import (
"path/filepath"
"testing"
)

func TestDefaultConfigInitializesIndex115Paths(t *testing.T) {
cfg := DefaultConfig("/tmp/openlist-data")
if cfg.Index115.DBFile != filepath.Join("/tmp/openlist-data", "index115", "index.db") {
t.Fatalf("unexpected index115 db path: %q", cfg.Index115.DBFile)
}
if cfg.Index115.BleveDir != filepath.Join("/tmp/openlist-data", "index115", "bleve") {
t.Fatalf("unexpected index115 bleve path: %q", cfg.Index115.BleveDir)
}
}
Loading
Loading