Skip to content

Commit

Permalink
feat: support zero config run
Browse files Browse the repository at this point in the history
  • Loading branch information
MuZhou233 committed Feb 14, 2024
1 parent 54e8daa commit 34c2bc7
Show file tree
Hide file tree
Showing 30 changed files with 559 additions and 399 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ Config file is required. The config template is provided in [configs](configs)

### Command line arguments

- `config` path, required, eg: --conf config.yaml
- `data` path, required, eg: --data /opt/librarian/data
- `config` path, eg: --conf config.yaml
- `data` path, eg: --data /opt/librarian/data

### Environment variables

Expand Down
25 changes: 23 additions & 2 deletions app/searcher/internal/data/data.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ package data

import (
"context"
"errors"
"time"

"github.com/tuihub/librarian/app/searcher/internal/biz"
"github.com/tuihub/librarian/internal/lib/logger"
"github.com/tuihub/librarian/internal/model"

"github.com/blevesearch/bleve/v2"
"github.com/google/wire"
Expand All @@ -31,7 +32,22 @@ func NewSearcherRepo(
search: b,
}, nil
}
return nil, errors.New("no valid search backend")
logger.Warnf("no valid search backend, search function will not work")
return &defaultSearcherRepo{sf: sf}, nil
}

type defaultSearcherRepo struct {
sf *sonyflake.Sonyflake
}

func (d defaultSearcherRepo) DescribeID(
context.Context, model.InternalID, biz.Index, bool, string) error {
return nil // search disabled
}

func (d defaultSearcherRepo) SearchID(
context.Context, biz.Index, model.Paging, string) ([]*biz.SearchResult, error) {
return nil, nil // search disabled
}

func NewSnowFlake() *sonyflake.Sonyflake {
Expand All @@ -44,6 +60,11 @@ func NewSnowFlake() *sonyflake.Sonyflake {
})
}

func (d defaultSearcherRepo) NewID(ctx context.Context) (int64, error) {
id, err := d.sf.NextID()
return int64(id), err
}

func (r *bleveSearcherRepo) NewID(ctx context.Context) (int64, error) {
id, err := r.sf.NextID()
return int64(id), err
Expand Down
6 changes: 3 additions & 3 deletions app/sephirah/cmd/sephirah/wire.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ import (

// wireApp init kratos application.
func wireApp(
*conf.Sephirah_Server,
*conf.Sephirah_Data,
*conf.Sephirah_Porter,
*conf.SephirahServer,
*conf.SephirahData,
*conf.Porter,
*conf.Auth,
*conf.MQ,
*conf.Cache,
Expand Down
14 changes: 7 additions & 7 deletions app/sephirah/cmd/sephirah/wire_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion app/sephirah/internal/data/binah.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ type binahRepo struct {
buckets map[bizbinah.Bucket]string
}

func NewBinahRepo(c *conf.Sephirah_Data) (bizbinah.BinahRepo, error) {
func NewBinahRepo(c *conf.SephirahData) (bizbinah.BinahRepo, error) {
if c == nil || c.GetS3() == nil {
return new(binahRepo), nil
}
Expand Down
28 changes: 24 additions & 4 deletions app/sephirah/internal/data/data.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@ import (
"context"
"errors"
"fmt"
"path"

"github.com/tuihub/librarian/app/sephirah/internal/data/internal/ent"
"github.com/tuihub/librarian/app/sephirah/internal/data/internal/ent/migrate"
"github.com/tuihub/librarian/internal/conf"
"github.com/tuihub/librarian/internal/lib/libapp"
"github.com/tuihub/librarian/internal/lib/logger"

"entgo.io/ent/dialect/sql"
Expand All @@ -31,6 +33,12 @@ var ProviderSet = wire.NewSet(
NewBinahRepo,
)

const (
driverMemory = "memory"
driverSQLite3 = "sqlite3"
driverPostgres = "postgres"
)

// Data .
type Data struct {
db *ent.Client
Expand All @@ -43,13 +51,22 @@ func NewData(db *ent.Client) *Data {
}
}

func NewSQLClient(c *conf.Sephirah_Data) (*ent.Client, func(), error) {
func NewSQLClient(c *conf.SephirahData, app *libapp.Settings) (*ent.Client, func(), error) {
var driverName, dataSourceName string
if c == nil {
c = new(conf.SephirahData)
}
driverName = c.GetDatabase().GetDriver()
if driverName == "" {
logger.Warnf("database driver is empty, using memory mode.")
driverName = driverMemory
}
switch driverName {
case "sqlite3":
case driverMemory:
dataSourceName = "file:ent?mode=memory&cache=shared&_fk=1"
case "postgres":
case driverSQLite3:
dataSourceName = fmt.Sprintf("file:%s?cache=shared&_fk=1", path.Join(app.DataPath, "librarian.db"))
case driverPostgres:
dataSourceName = fmt.Sprintf("host=%s port=%d user=%s dbname=%s password=%s",
c.GetDatabase().GetHost(),
c.GetDatabase().GetPort(),
Expand All @@ -63,9 +80,12 @@ func NewSQLClient(c *conf.Sephirah_Data) (*ent.Client, func(), error) {
default:
return nil, func() {}, errors.New("unsupported sql database")
}
if driverName == driverMemory {
driverName = driverSQLite3
}
client, err := ent.Open(driverName, dataSourceName)
if err != nil {
logger.Errorf("failed opening connection to postgres: %v", err)
logger.Errorf("failed opening connection to database: %v", err)
return nil, func() {}, err
}
// Run the auto migration tool.
Expand Down
4 changes: 2 additions & 2 deletions app/sephirah/internal/supervisor/supervisor.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,12 @@ type Supervisor struct {
}

func NewSupervisor(
c *conf.Sephirah_Porter,
c *conf.Porter,
auth *libauth.Auth,
porter *client.Porter,
) (*Supervisor, error) {
if c == nil {
c = new(conf.Sephirah_Porter)
c = new(conf.Porter)
}
return &Supervisor{
porter: porter,
Expand Down
4 changes: 2 additions & 2 deletions app/sephirah/pkg/service/wire.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ import (
)

func NewSephirahService(
*conf.Sephirah_Data,
*conf.Sephirah_Porter,
*conf.SephirahData,
*conf.Porter,
*conf.Consul,
*libauth.Auth,
*libmq.MQ,
Expand Down
10 changes: 5 additions & 5 deletions app/sephirah/pkg/service/wire_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 7 additions & 7 deletions cmd/librarian/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,16 +68,16 @@ func main() {

app, cleanup, err := wireApp(
bc.GetEnableServiceDiscovery(),
bc.GetSephirah().GetServer(),
bc.GetSephirah().GetData(),
bc.GetSephirah().GetPorter(),
bc.GetServer(),
bc.GetData(),
bc.GetPorter(),
bc.GetMapper().GetData(),
bc.GetSearcher().GetData(),
bc.GetMiner().GetData(),
bc.GetSephirah().GetAuth(),
bc.GetSephirah().GetMq(),
bc.GetSephirah().GetCache(),
bc.GetSephirah().GetConsul(),
bc.GetAuth(),
bc.GetMq(),
bc.GetCache(),
bc.GetConsul(),
appSettings,
)
if err != nil {
Expand Down
6 changes: 3 additions & 3 deletions cmd/librarian/wire.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ import (
// wireApp init kratos application.
func wireApp(
*conf.Librarian_EnableServiceDiscovery,
*conf.Sephirah_Server,
*conf.Sephirah_Data,
*conf.Sephirah_Porter,
*conf.SephirahServer,
*conf.SephirahData,
*conf.Porter,
*conf.Mapper_Data,
*conf.Searcher_Data,
*conf.Miner_Data,
Expand Down
8 changes: 4 additions & 4 deletions cmd/librarian/wire_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 34c2bc7

Please sign in to comment.