diff --git a/app/boot_levels.go b/app/boot_levels.go index 618cdfd4d..89d76ae34 100644 --- a/app/boot_levels.go +++ b/app/boot_levels.go @@ -4,10 +4,9 @@ import ( "context" "crypto/tls" "fmt" - "time" - cmpService "github.com/cortezaproject/corteza-server/compose/service" cmpEvent "github.com/cortezaproject/corteza-server/compose/service/event" + fdrService "github.com/cortezaproject/corteza-server/federation/service" msgService "github.com/cortezaproject/corteza-server/messaging/service" msgEvent "github.com/cortezaproject/corteza-server/messaging/service/event" "github.com/cortezaproject/corteza-server/messaging/websocket" @@ -30,6 +29,7 @@ import ( sysEvent "github.com/cortezaproject/corteza-server/system/service/event" "go.uber.org/zap" gomail "gopkg.in/mail.v2" + "time" ) const ( @@ -238,6 +238,21 @@ func (app *CortezaApp) InitServices(ctx context.Context) (err error) { PingPeriod: app.Opt.Websocket.PingPeriod, }) + // Initializes federation services + // + // Note: this is a legacy approach, all services from all 3 apps + // will most likely be merged in the future + err = fdrService.Initialize(ctx, app.Log, app.Store, fdrService.Config{ + ActionLog: app.Opt.ActionLog, + }) + + if err != nil { + return + } + + // Initialize external authentication (from default settings) + external.Init() + app.lvl = bootLevelServicesInitialized return } diff --git a/federation/service/service.go b/federation/service/service.go new file mode 100644 index 000000000..546e9178d --- /dev/null +++ b/federation/service/service.go @@ -0,0 +1,121 @@ +package service + +import ( + "context" + "time" + + "github.com/cortezaproject/corteza-server/pkg/actionlog" + "github.com/cortezaproject/corteza-server/pkg/healthcheck" + "github.com/cortezaproject/corteza-server/pkg/id" + "github.com/cortezaproject/corteza-server/pkg/options" + "github.com/cortezaproject/corteza-server/pkg/store" + "github.com/cortezaproject/corteza-server/pkg/store/minio" + "github.com/cortezaproject/corteza-server/pkg/store/plain" + ngStore "github.com/cortezaproject/corteza-server/store" + "github.com/cortezaproject/corteza-server/system/types" + "go.uber.org/zap" +) + +type ( + Config struct { + ActionLog options.ActionLogOpt + Storage options.StorageOpt + } +) + +var ( + DefaultStore store.Store + + // DefaultNgStore is an interface to storage backend(s) + // ng (next-gen) is a temporary prefix + // so that we can differentiate between it and the file-only store + DefaultNgStore ngStore.Storable + + DefaultLogger *zap.Logger + + // CurrentSettings represents current system settings + CurrentSettings = &types.AppSettings{} + + DefaultActionlog actionlog.Recorder + + // wrapper around time.Now() that will aid service testing + now = func() time.Time { + return time.Now() + } + + // returns pointer to time.Time struct that is set to current time + nowPtr = func() *time.Time { + n := now() + return &n + } + + // wrapper around id.Next() that will aid service testing + nextID = func() uint64 { + return id.Next() + } +) + +func Initialize(ctx context.Context, log *zap.Logger, s ngStore.Storable, c Config) (err error) { + var ( + hcd = healthcheck.Defaults() + ) + + // we're doing conversion to avoid having + // store interface exposed or generated inside app package + DefaultNgStore = s + + DefaultLogger = log.Named("service") + + { + tee := zap.NewNop() + policy := actionlog.MakeProductionPolicy() + + if !c.ActionLog.Enabled { + policy = actionlog.MakeDisabledPolicy() + } else if c.ActionLog.Debug { + policy = actionlog.MakeDebugPolicy() + tee = log + } + + DefaultActionlog = actionlog.NewService(DefaultNgStore, log, tee, policy) + } + + if DefaultStore == nil { + const svcPath = "federation" + if c.Storage.MinioEndpoint != "" { + var bucket = svcPath + if c.Storage.MinioBucket != "" { + bucket = c.Storage.MinioBucket + "/" + svcPath + } + + DefaultStore, err = minio.New(bucket, minio.Options{ + Endpoint: c.Storage.MinioEndpoint, + Secure: c.Storage.MinioSecure, + Strict: c.Storage.MinioStrict, + AccessKeyID: c.Storage.MinioAccessKey, + SecretAccessKey: c.Storage.MinioSecretKey, + + ServerSideEncryptKey: []byte(c.Storage.MinioSSECKey), + }) + + log.Info("initializing minio", + zap.String("bucket", bucket), + zap.String("endpoint", c.Storage.MinioEndpoint), + zap.Error(err)) + } else { + path := c.Storage.Path + "/" + svcPath + DefaultStore, err = plain.New(path) + log.Info("initializing store", + zap.String("path", path), + zap.Error(err)) + } + + if err != nil { + return err + } + } + + hcd.Add(store.Healthcheck(DefaultStore), "Store/Federation") + + return +}