diff --git a/internal/agent/controller/app/app_handler.go b/internal/agent/controller/app/app_handler.go index 7b656a9..61cd8e7 100644 --- a/internal/agent/controller/app/app_handler.go +++ b/internal/agent/controller/app/app_handler.go @@ -3,13 +3,11 @@ package app import ( "bytes" "context" - "database/sql" "net" "path/filepath" "text/template" "forge.cadoles.com/Cadoles/emissary/internal/agent/controller/app/spec" - appSpec "forge.cadoles.com/Cadoles/emissary/internal/agent/controller/app/spec" "forge.cadoles.com/Cadoles/emissary/internal/jwk" "forge.cadoles.com/arcad/edge/pkg/app" "forge.cadoles.com/arcad/edge/pkg/bus" @@ -21,6 +19,9 @@ import ( "forge.cadoles.com/arcad/edge/pkg/module/cast" fetchModule "forge.cadoles.com/arcad/edge/pkg/module/fetch" netModule "forge.cadoles.com/arcad/edge/pkg/module/net" + shareModule "forge.cadoles.com/arcad/edge/pkg/module/share" + shareSqlite "forge.cadoles.com/arcad/edge/pkg/module/share/sqlite" + "forge.cadoles.com/arcad/edge/pkg/storage" "forge.cadoles.com/arcad/edge/pkg/storage/sqlite" "github.com/Masterminds/sprig/v3" "github.com/go-chi/chi/v5" @@ -29,6 +30,16 @@ import ( "gitlab.com/wpetit/goweb/logger" ) +type Dependencies struct { + Bus bus.Bus + DocumentStore storage.DocumentStore + BlobStore storage.BlobStore + KeySet jwk.Set + AppRepository appModule.Repository + AppID app.ID + ShareRepository shareModule.Repository +} + const defaultSQLiteParams = "?_pragma=foreign_keys(1)&_pragma=busy_timeout=60000" func (c *Controller) getHandlerOptions(ctx context.Context, appKey string, specs *spec.Spec) ([]edgeHTTP.HandlerOptionFunc, error) { @@ -43,6 +54,12 @@ func (c *Controller) getHandlerOptions(ctx context.Context, appKey string, specs return nil, errors.Wrapf(err, "could not open database file '%s'", dbFile) } + shareDBFile := filepath.Join(dataDir, "shared.sqlite") + shareDB, err := sqlite.Open(shareDBFile + defaultSQLiteParams) + if err != nil { + return nil, errors.Wrapf(err, "could not open database file '%s'", shareDBFile) + } + keySet, err := getAuthKeySet(specs.Config) if err != nil { return nil, errors.Wrap(err, "could not retrieve auth key set") @@ -61,11 +78,20 @@ func (c *Controller) getHandlerOptions(ctx context.Context, appKey string, specs mounts = append(mounts, appModule.Mount(c.appRepository)) - bus := memory.NewBus() - modules := c.getAppModules(bus, db, specs, keySet) + deps := Dependencies{ + Bus: memory.NewBus(), + DocumentStore: sqlite.NewDocumentStoreWithDB(db), + BlobStore: sqlite.NewBlobStoreWithDB(db), + KeySet: keySet, + AppRepository: c.appRepository, + AppID: app.ID(appKey), + ShareRepository: shareSqlite.NewRepositoryWithDB(shareDB), + } + + modules := c.getAppModules(deps) options := []edgeHTTP.HandlerOptionFunc{ - edgeHTTP.WithBus(bus), + edgeHTTP.WithBus(deps.Bus), edgeHTTP.WithServerModules(modules...), edgeHTTP.WithHTTPMounts(mounts...), } @@ -246,21 +272,19 @@ func createResolveAppURL(specs *spec.Spec) (ResolveAppURLFunc, error) { }, nil } -func (c *Controller) getAppModules(bus bus.Bus, db *sql.DB, spec *appSpec.Spec, keySet jwk.Set) []app.ServerModuleFactory { - ds := sqlite.NewDocumentStoreWithDB(db) - bs := sqlite.NewBlobStoreWithDB(db) - +func (c *Controller) getAppModules(deps Dependencies) []app.ServerModuleFactory { return []app.ServerModuleFactory{ module.ContextModuleFactory(), module.ConsoleModuleFactory(), cast.CastModuleFactory(), module.LifecycleModuleFactory(), - netModule.ModuleFactory(bus), - module.RPCModuleFactory(bus), - module.StoreModuleFactory(ds), - blob.ModuleFactory(bus, bs), - authModuleFactory(keySet), - appModule.ModuleFactory(c.appRepository), - fetchModule.ModuleFactory(bus), + netModule.ModuleFactory(deps.Bus), + module.RPCModuleFactory(deps.Bus), + module.StoreModuleFactory(deps.DocumentStore), + blob.ModuleFactory(deps.Bus, deps.BlobStore), + authModuleFactory(deps.KeySet), + appModule.ModuleFactory(deps.AppRepository), + fetchModule.ModuleFactory(deps.Bus), + shareModule.ModuleFactory(deps.AppID, deps.ShareRepository), } }