From 1507106d6014be5998286e5d20cd424c6e98d450 Mon Sep 17 00:00:00 2001 From: Prad N Date: Thu, 27 Mar 2025 20:39:14 -0400 Subject: [PATCH] refactor: simplify configuration and streamline build process --- Taskfile.yml => .taskfile.dist.yml | 0 Makefile | 10 +++++ app/app.go | 2 +- main.go | 25 +++++------ pkg/config/Config.pkl.go | 18 -------- pkg/config/Environment.pkl.go | 14 ------ pkg/config/Motr.pkl.go | 36 ---------------- pkg/config/Schema.pkl.go | 22 ---------- pkg/config/init.pkl.go | 11 ----- pkg/embed/codec.go | 68 ++++++++++++++++-------------- 10 files changed, 61 insertions(+), 145 deletions(-) rename Taskfile.yml => .taskfile.dist.yml (100%) create mode 100644 Makefile delete mode 100644 pkg/config/Config.pkl.go delete mode 100644 pkg/config/Environment.pkl.go delete mode 100644 pkg/config/Motr.pkl.go delete mode 100644 pkg/config/Schema.pkl.go delete mode 100644 pkg/config/init.pkl.go diff --git a/Taskfile.yml b/.taskfile.dist.yml similarity index 100% rename from Taskfile.yml rename to .taskfile.dist.yml diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..b8c413f --- /dev/null +++ b/Makefile @@ -0,0 +1,10 @@ + +VERSION := $(shell echo $(shell git describe --tags) | sed 's/^v//') +COMMIT := $(shell git log -1 --format='%H') + +.PHONY: all build generate test + +all: build + +build: + GOOS=js GOARCH=wasm go build -o bin/motr.wasm . diff --git a/app/app.go b/app/app.go index 20f58f0..e17e9b3 100644 --- a/app/app.go +++ b/app/app.go @@ -5,8 +5,8 @@ package vault import ( "github.com/labstack/echo/v4" echomiddleware "github.com/labstack/echo/v4/middleware" - motr "github.com/onsonr/motr/pkg/config" "github.com/onsonr/motr/pkg/context" + motr "github.com/onsonr/motr/pkg/embed" motrorm "github.com/onsonr/motr/pkg/models" ) diff --git a/main.go b/main.go index 8dc5da5..c6e41e1 100644 --- a/main.go +++ b/main.go @@ -7,7 +7,6 @@ import ( "bytes" "context" "database/sql" - "encoding/json" "fmt" "io" "log" @@ -21,7 +20,8 @@ import ( _ "github.com/ncruces/go-sqlite3/embed" vault "github.com/onsonr/motr/app" sink "github.com/onsonr/motr/internal/sink" - motr "github.com/onsonr/motr/pkg/config" + + // motr "github.com/onsonr/motr/pkg/config" motrorm "github.com/onsonr/motr/pkg/models" ) @@ -42,14 +42,14 @@ var ( ) func main() { - configString := "TODO" - config, _ := loadConfig(configString) + // configString := "TODO" + // config, _ := loadConfig(configString) dbq, err := createDB() if err != nil { log.Fatal(err) return } - e, err := vault.New(config, dbq) + e, err := vault.New(nil, dbq) if err != nil { log.Fatal(err) return @@ -57,13 +57,14 @@ func main() { serveFetch(e) } -// loadConfig loads the config from the given JSON string -func loadConfig(configString string) (*motr.Config, error) { - var config motr.Config - err := json.Unmarshal([]byte(configString), &config) - return &config, err -} - +// // loadConfig loads the config from the given JSON string +// +// func loadConfig(configString string) (*motr.Config, error) { +// var config motr.Config +// err := json.Unmarshal([]byte(configString), &config) +// return &config, err +// } +// // createDB initializes and returns a configured database connection func createDB() (*motrorm.Queries, error) { db, err := sql.Open("sqlite3", ":memory:") diff --git a/pkg/config/Config.pkl.go b/pkg/config/Config.pkl.go deleted file mode 100644 index 19fb117..0000000 --- a/pkg/config/Config.pkl.go +++ /dev/null @@ -1,18 +0,0 @@ -// Code generated from Pkl module `sonr.net.Motr`. DO NOT EDIT. -package config - -type Config struct { - IpfsGatewayUrl string `pkl:"ipfsGatewayUrl" json:"ipfsGatewayUrl,omitempty"` - - MotrToken string `pkl:"motrToken" json:"motrToken,omitempty"` - - MotrAddress string `pkl:"motrAddress" json:"motrAddress,omitempty"` - - SonrApiUrl string `pkl:"sonrApiUrl" json:"sonrApiUrl,omitempty"` - - SonrRpcUrl string `pkl:"sonrRpcUrl" json:"sonrRpcUrl,omitempty"` - - SonrChainId string `pkl:"sonrChainId" json:"sonrChainId,omitempty"` - - VaultSchema *Schema `pkl:"vaultSchema" json:"vaultSchema,omitempty"` -} diff --git a/pkg/config/Environment.pkl.go b/pkg/config/Environment.pkl.go deleted file mode 100644 index 734ca91..0000000 --- a/pkg/config/Environment.pkl.go +++ /dev/null @@ -1,14 +0,0 @@ -// Code generated from Pkl module `sonr.net.Motr`. DO NOT EDIT. -package config - -type Environment struct { - IsDevelopment bool `pkl:"isDevelopment" json:"isDevelopment,omitempty"` - - CacheVersion string `pkl:"cacheVersion" json:"cacheVersion,omitempty"` - - HttpserverPath string `pkl:"httpserverPath" json:"httpserverPath,omitempty"` - - WasmExecPath string `pkl:"wasmExecPath" json:"wasmExecPath,omitempty"` - - WasmPath string `pkl:"wasmPath" json:"wasmPath,omitempty"` -} diff --git a/pkg/config/Motr.pkl.go b/pkg/config/Motr.pkl.go deleted file mode 100644 index 0cac00e..0000000 --- a/pkg/config/Motr.pkl.go +++ /dev/null @@ -1,36 +0,0 @@ -// Code generated from Pkl module `sonr.net.Motr`. DO NOT EDIT. -package config - -import ( - "context" - - "github.com/apple/pkl-go/pkl" -) - -type Motr struct { -} - -// LoadFromPath loads the pkl module at the given path and evaluates it into a Motr -func LoadFromPath(ctx context.Context, path string) (ret *Motr, err error) { - evaluator, err := pkl.NewEvaluator(ctx, pkl.PreconfiguredOptions) - if err != nil { - return nil, err - } - defer func() { - cerr := evaluator.Close() - if err == nil { - err = cerr - } - }() - ret, err = Load(ctx, evaluator, pkl.FileSource(path)) - return ret, err -} - -// Load loads the pkl module at the given source and evaluates it with the given evaluator into a Motr -func Load(ctx context.Context, evaluator pkl.Evaluator, source *pkl.ModuleSource) (*Motr, error) { - var ret Motr - if err := evaluator.EvaluateModule(ctx, source, &ret); err != nil { - return nil, err - } - return &ret, nil -} diff --git a/pkg/config/Schema.pkl.go b/pkg/config/Schema.pkl.go deleted file mode 100644 index 7c17e31..0000000 --- a/pkg/config/Schema.pkl.go +++ /dev/null @@ -1,22 +0,0 @@ -// Code generated from Pkl module `sonr.net.Motr`. DO NOT EDIT. -package config - -type Schema struct { - Version int `pkl:"version"` - - Account string `pkl:"account" json:"account,omitempty"` - - Asset string `pkl:"asset" json:"asset,omitempty"` - - Chain string `pkl:"chain" json:"chain,omitempty"` - - Credential string `pkl:"credential" json:"credential,omitempty"` - - Jwk string `pkl:"jwk" json:"jwk,omitempty"` - - Grant string `pkl:"grant" json:"grant,omitempty"` - - Keyshare string `pkl:"keyshare" json:"keyshare,omitempty"` - - Profile string `pkl:"profile" json:"profile,omitempty"` -} diff --git a/pkg/config/init.pkl.go b/pkg/config/init.pkl.go deleted file mode 100644 index 290e991..0000000 --- a/pkg/config/init.pkl.go +++ /dev/null @@ -1,11 +0,0 @@ -// Code generated from Pkl module `sonr.net.Motr`. DO NOT EDIT. -package config - -import "github.com/apple/pkl-go/pkl" - -func init() { - pkl.RegisterMapping("sonr.net.Motr", Motr{}) - pkl.RegisterMapping("sonr.net.Motr#Config", Config{}) - pkl.RegisterMapping("sonr.net.Motr#Schema", Schema{}) - pkl.RegisterMapping("sonr.net.Motr#Environment", Environment{}) -} diff --git a/pkg/embed/codec.go b/pkg/embed/codec.go index 21f9dc2..c9ab441 100644 --- a/pkg/embed/codec.go +++ b/pkg/embed/codec.go @@ -1,13 +1,9 @@ package embed -import ( - "encoding/json" - - "github.com/ipfs/boxo/files" - motr "github.com/onsonr/motr/pkg/config" -) +// motr "github.com/onsonr/motr/pkg/config" const SchemaVersion = 1 + const ( AppManifestFileName = "app.webmanifest" DWNConfigFileName = "dwn.json" @@ -16,33 +12,43 @@ const ( ServiceWorkerFileName = "sw.js" ) -// spawnVaultDirectory creates a new directory with the default files -func NewVaultFS(cfg *motr.Config) (files.Directory, error) { - manifestBz, err := NewWebManifest() - if err != nil { - return nil, err - } - cnfBz, err := json.Marshal(cfg) - if err != nil { - return nil, err - } - return files.NewMapDirectory(map[string]files.Node{ - AppManifestFileName: files.NewBytesFile(manifestBz), - DWNConfigFileName: files.NewBytesFile(cnfBz), - IndexHTMLFileName: files.NewBytesFile(IndexHTML), - MainJSFileName: files.NewBytesFile(MainJS), - ServiceWorkerFileName: files.NewBytesFile(WorkerJS), - }), nil -} - +// // spawnVaultDirectory creates a new directory with the default files +// +// func NewVaultFS(cfg *Config) (files.Directory, error) { +// manifestBz, err := NewWebManifest() +// if err != nil { +// return nil, err +// } +// cnfBz, err := json.Marshal(cfg) +// if err != nil { +// return nil, err +// } +// return files.NewMapDirectory(map[string]files.Node{ +// AppManifestFileName: files.NewBytesFile(manifestBz), +// DWNConfigFileName: files.NewBytesFile(cnfBz), +// IndexHTMLFileName: files.NewBytesFile(IndexHTML), +// MainJSFileName: files.NewBytesFile(MainJS), +// ServiceWorkerFileName: files.NewBytesFile(WorkerJS), +// }), nil +// } +// // NewVaultConfig returns the default vault config -func NewVaultConfig(addr string, ucanCID string) *motr.Config { - return &motr.Config{ +func NewVaultConfig(addr string, ucanCID string) *Config { + return &Config{ MotrToken: ucanCID, MotrAddress: addr, - IpfsGatewayUrl: "http://localhost:80", - SonrApiUrl: "http://localhost:1317", - SonrRpcUrl: "http://localhost:26657", - SonrChainId: "sonr-testnet-1", + IpfsGatewayURL: "http://localhost:80", + SonrAPIURL: "http://localhost:1317", + SonrRPCURL: "http://localhost:26657", + SonrChainID: "sonr-testnet-1", } } + +type Config struct { + MotrToken string `json:"motrToken"` + MotrAddress string `json:"motrAddress"` + IpfsGatewayURL string `json:"ipfsGatewayUrl"` + SonrAPIURL string `json:"sonrApiUrl"` + SonrRPCURL string `json:"sonrRpcUrl"` + SonrChainID string `json:"sonrChainId"` +}