2023-02-09 12:16:36 +01:00
|
|
|
package app
|
|
|
|
|
|
|
|
import (
|
2023-03-22 20:48:09 +01:00
|
|
|
"context"
|
2023-03-20 16:40:08 +01:00
|
|
|
"encoding/json"
|
2023-03-22 20:48:09 +01:00
|
|
|
"fmt"
|
2023-04-06 11:52:04 +02:00
|
|
|
"net"
|
2023-02-09 12:16:36 +01:00
|
|
|
"net/http"
|
2023-03-20 16:40:08 +01:00
|
|
|
"os"
|
2023-02-09 12:16:36 +01:00
|
|
|
"path/filepath"
|
2023-04-18 17:57:16 +02:00
|
|
|
"strconv"
|
2023-03-20 16:40:08 +01:00
|
|
|
"strings"
|
2023-04-18 17:57:16 +02:00
|
|
|
"sync"
|
2023-02-09 12:16:36 +01:00
|
|
|
|
2023-02-24 14:40:28 +01:00
|
|
|
"forge.cadoles.com/arcad/edge/pkg/app"
|
|
|
|
"forge.cadoles.com/arcad/edge/pkg/bus"
|
2023-02-09 12:16:36 +01:00
|
|
|
"forge.cadoles.com/arcad/edge/pkg/bus/memory"
|
|
|
|
appHTTP "forge.cadoles.com/arcad/edge/pkg/http"
|
2023-09-29 07:41:01 +02:00
|
|
|
"forge.cadoles.com/arcad/edge/pkg/jwtutil"
|
2023-02-09 12:16:36 +01:00
|
|
|
"forge.cadoles.com/arcad/edge/pkg/module"
|
2023-03-22 20:48:09 +01:00
|
|
|
appModule "forge.cadoles.com/arcad/edge/pkg/module/app"
|
|
|
|
appModuleMemory "forge.cadoles.com/arcad/edge/pkg/module/app/memory"
|
2023-04-18 17:57:16 +02:00
|
|
|
authModule "forge.cadoles.com/arcad/edge/pkg/module/auth"
|
2023-03-20 16:40:08 +01:00
|
|
|
authHTTP "forge.cadoles.com/arcad/edge/pkg/module/auth/http"
|
2023-09-20 16:55:49 +02:00
|
|
|
authModuleMiddleware "forge.cadoles.com/arcad/edge/pkg/module/auth/middleware"
|
2023-03-23 19:01:20 +01:00
|
|
|
"forge.cadoles.com/arcad/edge/pkg/module/blob"
|
2023-02-17 10:38:45 +01:00
|
|
|
"forge.cadoles.com/arcad/edge/pkg/module/cast"
|
2023-04-02 17:59:33 +02:00
|
|
|
"forge.cadoles.com/arcad/edge/pkg/module/fetch"
|
2023-04-06 11:52:04 +02:00
|
|
|
netModule "forge.cadoles.com/arcad/edge/pkg/module/net"
|
2023-04-13 10:16:48 +02:00
|
|
|
shareModule "forge.cadoles.com/arcad/edge/pkg/module/share"
|
2023-02-24 14:40:28 +01:00
|
|
|
"forge.cadoles.com/arcad/edge/pkg/storage"
|
2023-02-09 12:16:36 +01:00
|
|
|
"gitlab.com/wpetit/goweb/logger"
|
|
|
|
|
|
|
|
"forge.cadoles.com/arcad/edge/pkg/bundle"
|
|
|
|
"github.com/go-chi/chi/v5"
|
|
|
|
"github.com/go-chi/chi/v5/middleware"
|
2023-03-20 16:40:08 +01:00
|
|
|
"github.com/lestrrat-go/jwx/v2/jwk"
|
2023-02-09 12:16:36 +01:00
|
|
|
"github.com/pkg/errors"
|
|
|
|
"github.com/urfave/cli/v2"
|
2023-03-20 16:40:08 +01:00
|
|
|
|
|
|
|
_ "embed"
|
|
|
|
|
|
|
|
_ "forge.cadoles.com/arcad/edge/pkg/module/auth/http/passwd/argon2id"
|
|
|
|
_ "forge.cadoles.com/arcad/edge/pkg/module/auth/http/passwd/plain"
|
2023-09-13 06:03:25 +02:00
|
|
|
|
|
|
|
// Register storage drivers
|
|
|
|
"forge.cadoles.com/arcad/edge/pkg/storage/driver"
|
|
|
|
_ "forge.cadoles.com/arcad/edge/pkg/storage/driver/rpc"
|
|
|
|
_ "forge.cadoles.com/arcad/edge/pkg/storage/driver/sqlite"
|
|
|
|
"forge.cadoles.com/arcad/edge/pkg/storage/share"
|
2023-02-09 12:16:36 +01:00
|
|
|
)
|
|
|
|
|
2023-09-29 07:41:01 +02:00
|
|
|
var dummySecret = []byte("not_so_secret")
|
|
|
|
|
2023-02-09 12:16:36 +01:00
|
|
|
func RunCommand() *cli.Command {
|
|
|
|
return &cli.Command{
|
|
|
|
Name: "run",
|
|
|
|
Usage: "Run the specified app bundle",
|
|
|
|
Flags: []cli.Flag{
|
2023-04-18 17:57:16 +02:00
|
|
|
&cli.StringSliceFlag{
|
2023-02-09 12:16:36 +01:00
|
|
|
Name: "path",
|
|
|
|
Usage: "use `PATH` as app bundle (zipped bundle or directory)",
|
|
|
|
Aliases: []string{"p"},
|
2023-04-18 17:57:16 +02:00
|
|
|
Value: cli.NewStringSlice("."),
|
2023-02-09 12:16:36 +01:00
|
|
|
},
|
|
|
|
&cli.StringFlag{
|
|
|
|
Name: "address",
|
2023-04-18 17:57:16 +02:00
|
|
|
Usage: "use `ADDRESS` as http server base listening address",
|
2023-02-09 12:16:36 +01:00
|
|
|
Aliases: []string{"a"},
|
|
|
|
Value: ":8080",
|
|
|
|
},
|
|
|
|
&cli.StringFlag{
|
|
|
|
Name: "log-format",
|
|
|
|
Usage: "use `LOG-FORMAT` ('json' or 'human')",
|
|
|
|
Value: "human",
|
|
|
|
},
|
|
|
|
&cli.IntFlag{
|
|
|
|
Name: "log-level",
|
|
|
|
Usage: "use `LOG-LEVEL` (0: debug -> 5: fatal)",
|
|
|
|
Value: 0,
|
|
|
|
},
|
|
|
|
&cli.StringFlag{
|
2023-09-13 06:03:25 +02:00
|
|
|
Name: "blobstore-dsn",
|
|
|
|
Usage: "use `DSN` for blob storage",
|
|
|
|
EnvVars: []string{"EDGE_BLOBSTORE_DSN"},
|
|
|
|
Value: "sqlite://.edge/%APPID%/data.sqlite?_pragma=foreign_keys(1)&_pragma=busy_timeout=60000",
|
|
|
|
},
|
|
|
|
&cli.StringFlag{
|
|
|
|
Name: "documentstore-dsn",
|
|
|
|
Usage: "use `DSN` for document storage",
|
|
|
|
EnvVars: []string{"EDGE_DOCUMENTSTORE_DSN"},
|
|
|
|
Value: "sqlite://.edge/%APPID%/data.sqlite?_pragma=foreign_keys(1)&_pragma=busy_timeout=60000",
|
2023-02-24 14:40:28 +01:00
|
|
|
},
|
2023-04-13 10:16:48 +02:00
|
|
|
&cli.StringFlag{
|
2023-09-13 06:03:25 +02:00
|
|
|
Name: "sharestore-dsn",
|
|
|
|
Usage: "use `DSN` for share storage",
|
|
|
|
EnvVars: []string{"EDGE_SHARESTORE_DSN"},
|
|
|
|
Value: "sqlite://.edge/share.sqlite?_pragma=foreign_keys(1)&_pragma=busy_timeout=60000",
|
2023-04-13 10:16:48 +02:00
|
|
|
},
|
2023-02-24 14:40:28 +01:00
|
|
|
&cli.StringFlag{
|
2023-03-20 16:40:08 +01:00
|
|
|
Name: "accounts-file",
|
|
|
|
Usage: "use `FILE` as local accounts",
|
|
|
|
Value: ".edge/%APPID%/accounts.json",
|
2023-02-24 14:40:28 +01:00
|
|
|
},
|
2023-02-09 12:16:36 +01:00
|
|
|
},
|
|
|
|
Action: func(ctx *cli.Context) error {
|
|
|
|
address := ctx.String("address")
|
2023-04-18 17:57:16 +02:00
|
|
|
paths := ctx.StringSlice("path")
|
2023-02-24 14:40:28 +01:00
|
|
|
|
2023-02-09 12:16:36 +01:00
|
|
|
logFormat := ctx.String("log-format")
|
|
|
|
logLevel := ctx.Int("log-level")
|
2023-09-13 06:03:25 +02:00
|
|
|
blobstoreDSN := ctx.String("blobstore-dsn")
|
|
|
|
documentstoreDSN := ctx.String("documentstore-dsn")
|
|
|
|
shareStoreDSN := ctx.String("sharestore-dsn")
|
2023-04-18 17:57:16 +02:00
|
|
|
accountsFile := ctx.String("accounts-file")
|
2023-02-24 14:40:28 +01:00
|
|
|
|
2023-02-09 12:16:36 +01:00
|
|
|
logger.SetFormat(logger.Format(logFormat))
|
|
|
|
logger.SetLevel(logger.Level(logLevel))
|
|
|
|
|
|
|
|
cmdCtx := ctx.Context
|
|
|
|
|
2023-04-18 17:57:16 +02:00
|
|
|
host, portStr, err := net.SplitHostPort(address)
|
2023-02-09 12:16:36 +01:00
|
|
|
if err != nil {
|
2023-04-18 17:57:16 +02:00
|
|
|
return errors.WithStack(err)
|
2023-02-09 12:16:36 +01:00
|
|
|
}
|
|
|
|
|
2023-04-18 17:57:16 +02:00
|
|
|
port, err := strconv.ParseUint(portStr, 10, 32)
|
2023-02-09 12:16:36 +01:00
|
|
|
if err != nil {
|
2023-04-18 17:57:16 +02:00
|
|
|
return errors.WithStack(err)
|
2023-02-09 12:16:36 +01:00
|
|
|
}
|
|
|
|
|
2023-04-18 17:57:16 +02:00
|
|
|
manifests := make([]*app.Manifest, len(paths))
|
|
|
|
for idx, pth := range paths {
|
|
|
|
bdl, err := bundle.FromPath(pth)
|
|
|
|
if err != nil {
|
|
|
|
return errors.WithStack(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
manifest, err := app.LoadManifest(bdl)
|
|
|
|
if err != nil {
|
|
|
|
return errors.WithStack(err)
|
|
|
|
}
|
2023-02-09 12:16:36 +01:00
|
|
|
|
2023-04-18 17:57:16 +02:00
|
|
|
manifests[idx] = manifest
|
2023-04-11 11:04:34 +02:00
|
|
|
}
|
|
|
|
|
2023-04-18 17:57:16 +02:00
|
|
|
var wg sync.WaitGroup
|
2023-02-09 12:16:36 +01:00
|
|
|
|
2023-04-18 17:57:16 +02:00
|
|
|
for idx, p := range paths {
|
|
|
|
wg.Add(1)
|
2023-03-22 20:48:09 +01:00
|
|
|
|
2023-04-18 17:57:16 +02:00
|
|
|
go func(path string, basePort uint64, appIndex int) {
|
|
|
|
defer wg.Done()
|
2023-02-09 12:16:36 +01:00
|
|
|
|
2023-04-18 17:57:16 +02:00
|
|
|
port := basePort + uint64(appIndex)
|
|
|
|
address := fmt.Sprintf("%s:%d", host, port)
|
|
|
|
appsRepository := newAppRepository(host, basePort, manifests...)
|
2023-02-09 12:16:36 +01:00
|
|
|
|
2023-04-18 17:57:16 +02:00
|
|
|
appCtx := logger.With(cmdCtx, logger.F("address", address))
|
|
|
|
|
2023-09-13 06:03:25 +02:00
|
|
|
if err := runApp(appCtx, path, address, documentstoreDSN, blobstoreDSN, shareStoreDSN, accountsFile, appsRepository); err != nil {
|
2023-04-18 17:57:16 +02:00
|
|
|
logger.Error(appCtx, "could not run app", logger.E(errors.WithStack(err)))
|
|
|
|
}
|
|
|
|
}(p, port, idx)
|
2023-02-09 12:16:36 +01:00
|
|
|
}
|
|
|
|
|
2023-04-18 17:57:16 +02:00
|
|
|
wg.Wait()
|
2023-03-20 16:40:08 +01:00
|
|
|
|
2023-04-18 17:57:16 +02:00
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
2023-03-20 16:40:08 +01:00
|
|
|
|
2023-09-13 06:03:25 +02:00
|
|
|
func runApp(ctx context.Context, path, address, documentStoreDSN, blobStoreDSN, shareStoreDSN, accountsFile string, appRepository appModule.Repository) error {
|
2023-04-18 17:57:16 +02:00
|
|
|
absPath, err := filepath.Abs(path)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrapf(err, "could not resolve path '%s'", path)
|
|
|
|
}
|
2023-03-20 16:40:08 +01:00
|
|
|
|
2023-04-18 17:57:16 +02:00
|
|
|
logger.Info(ctx, "opening app bundle", logger.F("path", absPath))
|
2023-03-20 16:40:08 +01:00
|
|
|
|
2023-04-18 17:57:16 +02:00
|
|
|
bundle, err := bundle.FromPath(path)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrapf(err, "could not open path '%s' as an app bundle", path)
|
|
|
|
}
|
2023-02-09 12:16:36 +01:00
|
|
|
|
2023-04-18 17:57:16 +02:00
|
|
|
manifest, err := app.LoadManifest(bundle)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "could not load manifest from app bundle")
|
|
|
|
}
|
2023-02-09 12:16:36 +01:00
|
|
|
|
2023-04-18 17:57:16 +02:00
|
|
|
if valid, err := manifest.Validate(manifestMetadataValidators...); !valid {
|
|
|
|
return errors.Wrap(err, "invalid app manifest")
|
|
|
|
}
|
2023-02-09 12:16:36 +01:00
|
|
|
|
2023-04-18 17:57:16 +02:00
|
|
|
ctx = logger.With(ctx, logger.F("appID", manifest.ID))
|
|
|
|
|
2023-04-13 10:16:48 +02:00
|
|
|
// Add auth handler
|
2023-09-29 07:41:01 +02:00
|
|
|
key, err := jwtutil.NewSymmetricKey(dummySecret)
|
2023-04-18 17:57:16 +02:00
|
|
|
if err != nil {
|
|
|
|
return errors.WithStack(err)
|
|
|
|
}
|
|
|
|
|
2023-04-13 10:16:48 +02:00
|
|
|
deps := &moduleDeps{}
|
|
|
|
funcs := []ModuleDepFunc{
|
|
|
|
initMemoryBus,
|
2023-09-13 06:03:25 +02:00
|
|
|
initDatastores(documentStoreDSN, blobStoreDSN, shareStoreDSN, manifest.ID),
|
2023-04-13 10:16:48 +02:00
|
|
|
initAccounts(accountsFile, manifest.ID),
|
|
|
|
initAppRepository(appRepository),
|
2023-04-18 17:57:16 +02:00
|
|
|
}
|
|
|
|
|
2023-04-13 10:16:48 +02:00
|
|
|
for _, fn := range funcs {
|
|
|
|
if err := fn(deps); err != nil {
|
|
|
|
return errors.WithStack(err)
|
|
|
|
}
|
2023-04-18 17:57:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
handler := appHTTP.NewHandler(
|
2023-04-13 10:16:48 +02:00
|
|
|
appHTTP.WithBus(deps.Bus),
|
|
|
|
appHTTP.WithServerModules(getServerModules(deps)...),
|
2023-04-18 17:57:16 +02:00
|
|
|
appHTTP.WithHTTPMounts(
|
|
|
|
appModule.Mount(appRepository),
|
|
|
|
authModule.Mount(
|
|
|
|
authHTTP.NewLocalHandler(
|
2023-09-29 07:41:01 +02:00
|
|
|
key,
|
2023-04-18 17:57:16 +02:00
|
|
|
authHTTP.WithRoutePrefix("/auth"),
|
2023-04-13 10:16:48 +02:00
|
|
|
authHTTP.WithAccounts(deps.Accounts...),
|
2023-04-18 17:57:16 +02:00
|
|
|
),
|
2023-09-29 07:41:01 +02:00
|
|
|
authModule.WithJWT(func() (jwk.Set, error) {
|
|
|
|
return jwtutil.NewSymmetricKeySet(dummySecret)
|
|
|
|
}),
|
2023-04-18 17:57:16 +02:00
|
|
|
),
|
|
|
|
),
|
2023-09-20 17:23:53 +02:00
|
|
|
appHTTP.WithHTTPMiddlewares(
|
2023-09-29 07:41:01 +02:00
|
|
|
authModuleMiddleware.AnonymousUser(key),
|
2023-09-20 17:23:53 +02:00
|
|
|
),
|
2023-04-18 17:57:16 +02:00
|
|
|
)
|
|
|
|
if err := handler.Load(bundle); err != nil {
|
|
|
|
return errors.Wrap(err, "could not load app bundle")
|
|
|
|
}
|
|
|
|
|
|
|
|
router := chi.NewRouter()
|
|
|
|
router.Use(middleware.Logger)
|
2023-04-20 10:36:20 +02:00
|
|
|
router.Use(middleware.Compress(5))
|
2023-04-18 17:57:16 +02:00
|
|
|
|
|
|
|
// Add app handler
|
|
|
|
router.Handle("/*", handler)
|
|
|
|
|
|
|
|
logger.Info(ctx, "listening", logger.F("address", address))
|
|
|
|
|
|
|
|
if err := http.ListenAndServe(address, router); err != nil {
|
|
|
|
return errors.WithStack(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2023-02-09 12:16:36 +01:00
|
|
|
}
|
2023-02-24 14:40:28 +01:00
|
|
|
|
2023-04-13 10:16:48 +02:00
|
|
|
type moduleDeps struct {
|
2023-09-13 06:03:25 +02:00
|
|
|
AppID app.ID
|
|
|
|
Bus bus.Bus
|
|
|
|
DocumentStore storage.DocumentStore
|
|
|
|
BlobStore storage.BlobStore
|
|
|
|
AppRepository appModule.Repository
|
|
|
|
ShareStore share.Store
|
|
|
|
Accounts []authHTTP.LocalAccount
|
2023-04-13 10:16:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
type ModuleDepFunc func(*moduleDeps) error
|
|
|
|
|
|
|
|
func getServerModules(deps *moduleDeps) []app.ServerModuleFactory {
|
2023-02-24 14:40:28 +01:00
|
|
|
return []app.ServerModuleFactory{
|
2023-04-24 12:16:30 +02:00
|
|
|
module.LifecycleModuleFactory(),
|
2023-02-24 14:40:28 +01:00
|
|
|
module.ContextModuleFactory(),
|
|
|
|
module.ConsoleModuleFactory(),
|
|
|
|
cast.CastModuleFactory(),
|
2023-04-13 10:16:48 +02:00
|
|
|
netModule.ModuleFactory(deps.Bus),
|
|
|
|
module.RPCModuleFactory(deps.Bus),
|
|
|
|
module.StoreModuleFactory(deps.DocumentStore),
|
|
|
|
blob.ModuleFactory(deps.Bus, deps.BlobStore),
|
2023-04-18 17:57:16 +02:00
|
|
|
authModule.ModuleFactory(
|
2023-09-29 07:41:01 +02:00
|
|
|
authModule.WithJWT(func() (jwk.Set, error) {
|
|
|
|
return jwtutil.NewSymmetricKeySet(dummySecret)
|
|
|
|
}),
|
2023-02-24 14:40:28 +01:00
|
|
|
),
|
2023-04-13 10:16:48 +02:00
|
|
|
appModule.ModuleFactory(deps.AppRepository),
|
|
|
|
fetch.ModuleFactory(deps.Bus),
|
2023-09-13 06:03:25 +02:00
|
|
|
shareModule.ModuleFactory(deps.AppID, deps.ShareStore),
|
2023-02-24 14:40:28 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-20 16:40:08 +01:00
|
|
|
func ensureDir(path string) error {
|
|
|
|
if err := os.MkdirAll(filepath.Dir(path), os.ModePerm); err != nil {
|
|
|
|
return errors.WithStack(err)
|
|
|
|
}
|
2023-02-24 14:40:28 +01:00
|
|
|
|
2023-03-20 16:40:08 +01:00
|
|
|
return nil
|
|
|
|
}
|
2023-02-24 14:40:28 +01:00
|
|
|
|
2023-03-20 16:40:08 +01:00
|
|
|
func injectAppID(str string, appID app.ID) string {
|
|
|
|
return strings.ReplaceAll(str, "%APPID%", string(appID))
|
|
|
|
}
|
2023-02-24 14:40:28 +01:00
|
|
|
|
2023-03-20 16:40:08 +01:00
|
|
|
//go:embed default-accounts.json
|
|
|
|
var defaultAccounts []byte
|
2023-02-24 14:40:28 +01:00
|
|
|
|
2023-03-20 16:40:08 +01:00
|
|
|
func loadLocalAccounts(path string) ([]authHTTP.LocalAccount, error) {
|
|
|
|
if err := ensureDir(path); err != nil {
|
|
|
|
return nil, errors.WithStack(err)
|
|
|
|
}
|
2023-02-24 14:40:28 +01:00
|
|
|
|
2023-09-13 06:03:25 +02:00
|
|
|
data, err := os.ReadFile(path)
|
2023-03-20 16:40:08 +01:00
|
|
|
if err != nil {
|
|
|
|
if errors.Is(err, os.ErrNotExist) {
|
2023-09-13 06:03:25 +02:00
|
|
|
if err := os.WriteFile(path, defaultAccounts, 0o640); err != nil {
|
2023-03-20 16:40:08 +01:00
|
|
|
return nil, errors.WithStack(err)
|
2023-02-24 14:40:28 +01:00
|
|
|
}
|
|
|
|
|
2023-03-20 16:40:08 +01:00
|
|
|
data = defaultAccounts
|
|
|
|
} else {
|
|
|
|
return nil, errors.WithStack(err)
|
2023-02-24 14:40:28 +01:00
|
|
|
}
|
2023-03-20 16:40:08 +01:00
|
|
|
}
|
2023-02-24 14:40:28 +01:00
|
|
|
|
2023-03-20 16:40:08 +01:00
|
|
|
var accounts []authHTTP.LocalAccount
|
|
|
|
|
|
|
|
if err := json.Unmarshal(data, &accounts); err != nil {
|
|
|
|
return nil, errors.WithStack(err)
|
2023-02-24 14:40:28 +01:00
|
|
|
}
|
2023-03-20 16:40:08 +01:00
|
|
|
|
|
|
|
return accounts, nil
|
2023-02-24 14:40:28 +01:00
|
|
|
}
|
2023-04-06 11:52:04 +02:00
|
|
|
|
|
|
|
func findMatchingDeviceAddress(ctx context.Context, from string, defaultAddr string) (string, error) {
|
|
|
|
if from == "" {
|
|
|
|
return defaultAddr, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
fromIP := net.ParseIP(from)
|
|
|
|
|
|
|
|
if fromIP == nil {
|
|
|
|
return defaultAddr, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
ifaces, err := net.Interfaces()
|
|
|
|
if err != nil {
|
|
|
|
return "", errors.WithStack(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, ifa := range ifaces {
|
|
|
|
addrs, err := ifa.Addrs()
|
|
|
|
if err != nil {
|
|
|
|
logger.Error(
|
|
|
|
ctx, "could not retrieve iface adresses",
|
|
|
|
logger.E(errors.WithStack(err)), logger.F("iface", ifa.Name),
|
|
|
|
)
|
|
|
|
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, addr := range addrs {
|
|
|
|
ip, network, err := net.ParseCIDR(addr.String())
|
|
|
|
if err != nil {
|
|
|
|
logger.Error(
|
|
|
|
ctx, "could not parse address",
|
|
|
|
logger.E(errors.WithStack(err)), logger.F("address", addr.String()),
|
|
|
|
)
|
|
|
|
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if !network.Contains(fromIP) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2023-04-20 13:57:09 +02:00
|
|
|
if ip.To4() == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
return ip.To4().String(), nil
|
2023-04-06 11:52:04 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return defaultAddr, nil
|
|
|
|
}
|
2023-04-18 17:57:16 +02:00
|
|
|
|
|
|
|
func newAppRepository(host string, basePort uint64, manifests ...*app.Manifest) *appModuleMemory.Repository {
|
2023-04-20 14:17:03 +02:00
|
|
|
if host == "" {
|
|
|
|
host = "127.0.0.1"
|
|
|
|
}
|
|
|
|
|
2023-04-18 17:57:16 +02:00
|
|
|
return appModuleMemory.NewRepository(
|
|
|
|
func(ctx context.Context, id app.ID, from string) (string, error) {
|
|
|
|
appIndex := 0
|
|
|
|
for i := 0; i < len(manifests); i++ {
|
|
|
|
if manifests[i].ID == id {
|
|
|
|
appIndex = i
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
addr, err := findMatchingDeviceAddress(ctx, from, host)
|
|
|
|
if err != nil {
|
|
|
|
return "", errors.WithStack(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return fmt.Sprintf("http://%s:%d", addr, int(basePort)+appIndex), nil
|
|
|
|
},
|
|
|
|
manifests...,
|
|
|
|
)
|
|
|
|
}
|
2023-04-13 10:16:48 +02:00
|
|
|
|
|
|
|
func initAppRepository(repo appModule.Repository) ModuleDepFunc {
|
|
|
|
return func(deps *moduleDeps) error {
|
|
|
|
deps.AppRepository = repo
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func initMemoryBus(deps *moduleDeps) error {
|
|
|
|
deps.Bus = memory.NewBus()
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-09-13 06:03:25 +02:00
|
|
|
func initDatastores(documentStoreDSN, blobStoreDSN, shareStoreDSN string, appID app.ID) ModuleDepFunc {
|
2023-04-13 10:16:48 +02:00
|
|
|
return func(deps *moduleDeps) error {
|
2023-09-13 06:03:25 +02:00
|
|
|
documentStoreDSN = injectAppID(documentStoreDSN, appID)
|
2023-04-13 10:16:48 +02:00
|
|
|
|
2023-09-13 06:03:25 +02:00
|
|
|
documentStore, err := driver.NewDocumentStore(documentStoreDSN)
|
|
|
|
if err != nil {
|
2023-04-13 10:16:48 +02:00
|
|
|
return errors.WithStack(err)
|
|
|
|
}
|
|
|
|
|
2023-09-13 06:03:25 +02:00
|
|
|
deps.DocumentStore = documentStore
|
|
|
|
|
|
|
|
blobStoreDSN = injectAppID(blobStoreDSN, appID)
|
|
|
|
|
|
|
|
blobStore, err := driver.NewBlobStore(blobStoreDSN)
|
|
|
|
if err != nil {
|
|
|
|
return errors.WithStack(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
deps.BlobStore = blobStore
|
|
|
|
|
|
|
|
shareStore, err := driver.NewShareStore(shareStoreDSN)
|
2023-04-13 10:16:48 +02:00
|
|
|
if err != nil {
|
|
|
|
return errors.WithStack(err)
|
|
|
|
}
|
|
|
|
|
2023-09-13 06:03:25 +02:00
|
|
|
deps.ShareStore = shareStore
|
2023-04-13 10:16:48 +02:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func initAccounts(accountsFile string, appID app.ID) ModuleDepFunc {
|
|
|
|
return func(deps *moduleDeps) error {
|
|
|
|
accountsFile = injectAppID(accountsFile, appID)
|
|
|
|
|
|
|
|
accounts, err := loadLocalAccounts(accountsFile)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "could not load local accounts")
|
|
|
|
}
|
|
|
|
|
|
|
|
deps.Accounts = accounts
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|