feat(controller,app): share module integration
arcad/emissary/pipeline/head This commit looks good Details

This commit is contained in:
wpetit 2023-04-21 13:10:03 +02:00
parent 87a45090e0
commit 541d30d74f
1 changed files with 40 additions and 16 deletions

View File

@ -3,13 +3,11 @@ package app
import ( import (
"bytes" "bytes"
"context" "context"
"database/sql"
"net" "net"
"path/filepath" "path/filepath"
"text/template" "text/template"
"forge.cadoles.com/Cadoles/emissary/internal/agent/controller/app/spec" "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/Cadoles/emissary/internal/jwk"
"forge.cadoles.com/arcad/edge/pkg/app" "forge.cadoles.com/arcad/edge/pkg/app"
"forge.cadoles.com/arcad/edge/pkg/bus" "forge.cadoles.com/arcad/edge/pkg/bus"
@ -21,6 +19,9 @@ import (
"forge.cadoles.com/arcad/edge/pkg/module/cast" "forge.cadoles.com/arcad/edge/pkg/module/cast"
fetchModule "forge.cadoles.com/arcad/edge/pkg/module/fetch" fetchModule "forge.cadoles.com/arcad/edge/pkg/module/fetch"
netModule "forge.cadoles.com/arcad/edge/pkg/module/net" 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" "forge.cadoles.com/arcad/edge/pkg/storage/sqlite"
"github.com/Masterminds/sprig/v3" "github.com/Masterminds/sprig/v3"
"github.com/go-chi/chi/v5" "github.com/go-chi/chi/v5"
@ -29,6 +30,16 @@ import (
"gitlab.com/wpetit/goweb/logger" "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" 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) { 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) 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) keySet, err := getAuthKeySet(specs.Config)
if err != nil { if err != nil {
return nil, errors.Wrap(err, "could not retrieve auth key set") 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)) mounts = append(mounts, appModule.Mount(c.appRepository))
bus := memory.NewBus() deps := Dependencies{
modules := c.getAppModules(bus, db, specs, keySet) 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{ options := []edgeHTTP.HandlerOptionFunc{
edgeHTTP.WithBus(bus), edgeHTTP.WithBus(deps.Bus),
edgeHTTP.WithServerModules(modules...), edgeHTTP.WithServerModules(modules...),
edgeHTTP.WithHTTPMounts(mounts...), edgeHTTP.WithHTTPMounts(mounts...),
} }
@ -246,21 +272,19 @@ func createResolveAppURL(specs *spec.Spec) (ResolveAppURLFunc, error) {
}, nil }, nil
} }
func (c *Controller) getAppModules(bus bus.Bus, db *sql.DB, spec *appSpec.Spec, keySet jwk.Set) []app.ServerModuleFactory { func (c *Controller) getAppModules(deps Dependencies) []app.ServerModuleFactory {
ds := sqlite.NewDocumentStoreWithDB(db)
bs := sqlite.NewBlobStoreWithDB(db)
return []app.ServerModuleFactory{ return []app.ServerModuleFactory{
module.ContextModuleFactory(), module.ContextModuleFactory(),
module.ConsoleModuleFactory(), module.ConsoleModuleFactory(),
cast.CastModuleFactory(), cast.CastModuleFactory(),
module.LifecycleModuleFactory(), module.LifecycleModuleFactory(),
netModule.ModuleFactory(bus), netModule.ModuleFactory(deps.Bus),
module.RPCModuleFactory(bus), module.RPCModuleFactory(deps.Bus),
module.StoreModuleFactory(ds), module.StoreModuleFactory(deps.DocumentStore),
blob.ModuleFactory(bus, bs), blob.ModuleFactory(deps.Bus, deps.BlobStore),
authModuleFactory(keySet), authModuleFactory(deps.KeySet),
appModule.ModuleFactory(c.appRepository), appModule.ModuleFactory(deps.AppRepository),
fetchModule.ModuleFactory(bus), fetchModule.ModuleFactory(deps.Bus),
shareModule.ModuleFactory(deps.AppID, deps.ShareRepository),
} }
} }