From af6e07323b1dbb8c9b45b1256669cd6b23994fb6 Mon Sep 17 00:00:00 2001 From: Prad Nukala Date: Mon, 7 Oct 2024 21:15:25 -0400 Subject: [PATCH] feat: integrate nebula UI with worker-assets-gen --- Makefile | 12 +++--- cmd/hway/commands/root.go | 10 ----- cmd/hway/commands/start.go | 18 --------- cmd/hway/main.go | 13 ------ cmd/hway/server/server.go | 40 ------------------- deploy/process-compose.yaml | 9 ----- go.mod | 1 + go.sum | 2 + internal/channel/broadcast.go | 18 +++++++++ internal/channel/middleware.go | 35 ++++++++++++++++ internal/{auth => swtkn}/macaroon.go | 2 +- internal/{auth => swtkn}/middleware.go | 2 +- cmd/motr/motr.go => pkg/dwn/wasm/main.go | 0 pkg/nebula/global/state/credentials_templ.go | 2 +- pkg/nebula/package.json | 2 +- web/package.json | 23 +++++++++++ .../handlers => web/provider}/search.go | 0 .../server/handlers => web/provider}/sync.go | 0 .../server/handlers => web/provider}/txns.go | 0 .../server/handlers => web/provider}/wss.go | 0 web/server.go | 20 ++++++++++ web/wrangler.toml | 6 +++ 22 files changed, 114 insertions(+), 101 deletions(-) delete mode 100644 cmd/hway/commands/root.go delete mode 100644 cmd/hway/commands/start.go delete mode 100644 cmd/hway/main.go delete mode 100644 cmd/hway/server/server.go create mode 100644 internal/channel/broadcast.go create mode 100644 internal/channel/middleware.go rename internal/{auth => swtkn}/macaroon.go (98%) rename internal/{auth => swtkn}/middleware.go (99%) rename cmd/motr/motr.go => pkg/dwn/wasm/main.go (100%) create mode 100644 web/package.json rename {cmd/hway/server/handlers => web/provider}/search.go (100%) rename {cmd/hway/server/handlers => web/provider}/sync.go (100%) rename {cmd/hway/server/handlers => web/provider}/txns.go (100%) rename {cmd/hway/server/handlers => web/provider}/wss.go (100%) create mode 100644 web/server.go create mode 100644 web/wrangler.toml diff --git a/Makefile b/Makefile index 5e7dc2b17..25215da59 100644 --- a/Makefile +++ b/Makefile @@ -289,11 +289,7 @@ testnet-basic: setup-testnet sh-testnet: mod-tidy CHAIN_ID="sonr-testnet-1" BLOCK_TIME="1000ms" CLEAN=true sh scripts/test_node.sh -start-hway: hway - @echo "(start-proxy) Starting proxy server" - ./build/hway start - -.PHONY: setup-testnet set-testnet-configs testnet testnet-basic sh-testnet start-hway +.PHONY: setup-testnet set-testnet-configs testnet testnet-basic sh-testnet ############################################################################### @@ -323,11 +319,13 @@ gen-pkl: build-hway: gen-templ gen-pkl @echo "(motr) Building Highway gateway" - go build -o ./build/hway ./cmd/hway + go run github.com/syumai/workers/cmd/workers-assets-gen@v0.23.1 -o ./web/build -mode go + GOOS=js GOARCH=wasm go build -o ./web/build/app.wasm ./web/server.go + build-motr: gen-templ gen-pkl @echo "(dwn) Building motr.wasm -> Service Worker IPFS Vault" - GOOS=js GOARCH=wasm go build -o ./pkg/dwn/app.wasm ./cmd/motr/motr.go + GOOS=js GOARCH=wasm go build -o ./pkg/dwn/app.wasm ./pkg/dwn/wasm/main.go build-nebula: @echo "(nebula) Building nebula" diff --git a/cmd/hway/commands/root.go b/cmd/hway/commands/root.go deleted file mode 100644 index 980f786ac..000000000 --- a/cmd/hway/commands/root.go +++ /dev/null @@ -1,10 +0,0 @@ -package commands - -import "github.com/spf13/cobra" - -func NewRootCmd() *cobra.Command { - return &cobra.Command{ - Use: "motr", - Short: "Manage a local DWN instance for the Sonr blockchain", - } -} diff --git a/cmd/hway/commands/start.go b/cmd/hway/commands/start.go deleted file mode 100644 index e7f447e64..000000000 --- a/cmd/hway/commands/start.go +++ /dev/null @@ -1,18 +0,0 @@ -package commands - -import ( - "github.com/spf13/cobra" - - "github.com/onsonr/sonr/cmd/hway/server" -) - -func NewStartCmd() *cobra.Command { - return &cobra.Command{ - Use: "start", - Short: "Starts the DWN proxy server for the local IPFS node", - Run: func(cmd *cobra.Command, args []string) { - s := server.New() - s.Start() - }, - } -} diff --git a/cmd/hway/main.go b/cmd/hway/main.go deleted file mode 100644 index d5dd547ad..000000000 --- a/cmd/hway/main.go +++ /dev/null @@ -1,13 +0,0 @@ -package main - -import ( - "github.com/onsonr/sonr/cmd/hway/commands" -) - -func main() { - rootCmd := commands.NewRootCmd() - rootCmd.AddCommand(commands.NewStartCmd()) - if err := rootCmd.Execute(); err != nil { - panic(err) - } -} diff --git a/cmd/hway/server/server.go b/cmd/hway/server/server.go deleted file mode 100644 index 4e4cabc52..000000000 --- a/cmd/hway/server/server.go +++ /dev/null @@ -1,40 +0,0 @@ -package server - -import ( - "net/http" - - "github.com/labstack/echo/v4" - "github.com/labstack/gommon/log" - - "github.com/onsonr/sonr/internal/session" - "github.com/onsonr/sonr/pkg/nebula" - "github.com/onsonr/sonr/pkg/nebula/routes" -) - -type Server struct { - *echo.Echo -} - -func New() *Server { - s := &Server{Echo: echo.New()} - s.Logger.SetLevel(log.INFO) - s.Use(session.UseSession) - - // Configure the server - if err := nebula.UseAssets(s.Echo); err != nil { - s.Logger.Fatal(err) - } - - s.GET("/", routes.Home) - s.GET("/login", routes.LoginStart) - s.GET("/register", routes.RegisterStart) - // s.GET("/profile", router.Profile) - // s.GET("/authorize", router.Authorize) - return s -} - -func (s *Server) Start() { - if err := s.Echo.Start(":1323"); err != http.ErrServerClosed { - log.Fatal(err) - } -} diff --git a/deploy/process-compose.yaml b/deploy/process-compose.yaml index ce9aac967..275db47f3 100644 --- a/deploy/process-compose.yaml +++ b/deploy/process-compose.yaml @@ -18,15 +18,6 @@ processes: depends: - ipfs - hway: - namespace: testnet - command: "devbox run start:hway" - restart: on_failure - max_restarts: 1 - depends: - - ipfs - - sonr - tunnel: namespace: public command: "cloudflared tunnel run --token $TUNNEL_TOKEN" diff --git a/go.mod b/go.mod index dc4ba2341..8acae13d3 100644 --- a/go.mod +++ b/go.mod @@ -82,6 +82,7 @@ require ( github.com/strangelove-ventures/poa v0.50.0 github.com/strangelove-ventures/tokenfactory v0.50.0 github.com/stretchr/testify v1.9.0 + github.com/syumai/workers v0.26.3 golang.org/x/crypto v0.26.0 google.golang.org/genproto/googleapis/api v0.0.0-20240617180043-68d350f18fd4 google.golang.org/grpc v1.64.0 diff --git a/go.sum b/go.sum index 94e1ffc63..29da40697 100644 --- a/go.sum +++ b/go.sum @@ -2048,6 +2048,8 @@ github.com/subosito/gotenv v1.6.0 h1:9NlTDc1FTs4qu0DDq7AEtTPNw6SVm7uBMsUCUjABIf8 github.com/subosito/gotenv v1.6.0/go.mod h1:Dk4QP5c2W3ibzajGcXpNraDfq2IrhjMIvMSWPKKo0FU= github.com/syndtr/goleveldb v1.0.1-0.20210819022825-2ae1ddf74ef7 h1:epCh84lMvA70Z7CTTCmYQn2CKbY8j86K7/FAIr141uY= github.com/syndtr/goleveldb v1.0.1-0.20210819022825-2ae1ddf74ef7/go.mod h1:q4W45IWZaF22tdD+VEXcAWRA037jwmWEB5VWYORlTpc= +github.com/syumai/workers v0.26.3 h1:AF+IBaRccbR4JIj2kNJLJblruPFMD/pAbzkopejGcP8= +github.com/syumai/workers v0.26.3/go.mod h1:ZnqmdiHNBrbxOLrZ/HJ5jzHy6af9cmiNZk10R9NrIEA= github.com/tendermint/go-amino v0.16.0 h1:GyhmgQKvqF82e2oZeuMSp9JTN0N09emoSZlb2lyGa2E= github.com/tendermint/go-amino v0.16.0/go.mod h1:TQU0M1i/ImAo+tYpZi73AU3V/dKeCoMC9Sphe2ZwGME= github.com/tidwall/btree v1.7.0 h1:L1fkJH/AuEh5zBnnBbmTwQ5Lt+bRJ5A8EWecslvo9iI= diff --git a/internal/channel/broadcast.go b/internal/channel/broadcast.go new file mode 100644 index 000000000..283cf3cb3 --- /dev/null +++ b/internal/channel/broadcast.go @@ -0,0 +1,18 @@ +//go:build js && wasm + +package channel + +import ( + "syscall/js" + + "github.com/labstack/echo/v4" +) + +type BroadcastContext struct { + echo.Context + Channel js.Value +} + +func (c *BroadcastContext) BroadcastMessage(message string) { + c.Channel.Call("postMessage", message) +} diff --git a/internal/channel/middleware.go b/internal/channel/middleware.go new file mode 100644 index 000000000..a700f43d3 --- /dev/null +++ b/internal/channel/middleware.go @@ -0,0 +1,35 @@ +//go:build js && wasm + +package channel + +import ( + "syscall/js" + + "github.com/labstack/echo/v4" +) + +type JSHandler func(this js.Value, args []js.Value) interface{} + +func UseBroadcastChannel(channelName string, handler JSHandler) echo.MiddlewareFunc { + var channel js.Value + + return func(next echo.HandlerFunc) echo.HandlerFunc { + return func(c echo.Context) error { + if channel.IsUndefined() { + channel = js.Global().Get("BroadcastChannel").New(channelName) + channel.Call("addEventListener", "message", handler) + } + + cc := &BroadcastContext{ + Context: c, + Channel: channel, + } + return next(cc) + } + } +} + +func PostBroadcastMessage(c echo.Context, message string) { + cc := c.(*BroadcastContext) + cc.BroadcastMessage(message) +} diff --git a/internal/auth/macaroon.go b/internal/swtkn/macaroon.go similarity index 98% rename from internal/auth/macaroon.go rename to internal/swtkn/macaroon.go index 829949c27..b1f0e45a1 100644 --- a/internal/auth/macaroon.go +++ b/internal/swtkn/macaroon.go @@ -1,4 +1,4 @@ -package auth +package swt import ( "fmt" diff --git a/internal/auth/middleware.go b/internal/swtkn/middleware.go similarity index 99% rename from internal/auth/middleware.go rename to internal/swtkn/middleware.go index 3a2b716fe..441c56228 100644 --- a/internal/auth/middleware.go +++ b/internal/swtkn/middleware.go @@ -1,4 +1,4 @@ -package auth +package swt import ( "net/http" diff --git a/cmd/motr/motr.go b/pkg/dwn/wasm/main.go similarity index 100% rename from cmd/motr/motr.go rename to pkg/dwn/wasm/main.go diff --git a/pkg/nebula/global/state/credentials_templ.go b/pkg/nebula/global/state/credentials_templ.go index 86c70be81..1bcc75958 100644 --- a/pkg/nebula/global/state/credentials_templ.go +++ b/pkg/nebula/global/state/credentials_templ.go @@ -87,7 +87,7 @@ func NavigatorCredentialsGetAll() templ.Component { templ_7745c5c3_Var3 = templ.NopComponent } ctx = templ.ClearChildren(ctx) - _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("") + _, templ_7745c5c3_Err = templ_7745c5c3_Buffer.WriteString("") if templ_7745c5c3_Err != nil { return templ_7745c5c3_Err } diff --git a/pkg/nebula/package.json b/pkg/nebula/package.json index 6e6fe4715..4ae6b4f02 100644 --- a/pkg/nebula/package.json +++ b/pkg/nebula/package.json @@ -1,5 +1,5 @@ { - "name": "nebula", + "name": "@onsonr/nebula", "version": "0.0.2", "scripts": { "fetch:deps": "bun run .deps.mjs", diff --git a/web/package.json b/web/package.json new file mode 100644 index 000000000..d260e8aa1 --- /dev/null +++ b/web/package.json @@ -0,0 +1,23 @@ +{ + "name": "@onsonr/sonr.id", + "version": "0.0.2", + "scripts": { + "fetch:deps": "bun run .deps.mjs", + "build:css": "bunx tailwindcss -i ./global/styles/globals.css -o ./assets/css/styles.css", + "build": "bun run fetch:deps && bun run build:css", + "watch": "bunx tailwindcss -i ./global/styles/globals.css -o ./assets/css/styles.css --watch" + }, + "devDependencies": { + "@tailwindcss/forms": "^0.5.7", + "@tailwindcss/typography": "^0.5.15", + "alpinejs": "^3.14.1", + "htmx.org": "^1.9.12", + "tailwindcss": "^3.3.0", + "tailwindcss-motion": "^0.3.0-beta", + "typescript": "^4.9.0" + }, + "dependencies": { + "node-fetch": "^3.3.2", + "sonr-es": "^0.5.3" + } +} diff --git a/cmd/hway/server/handlers/search.go b/web/provider/search.go similarity index 100% rename from cmd/hway/server/handlers/search.go rename to web/provider/search.go diff --git a/cmd/hway/server/handlers/sync.go b/web/provider/sync.go similarity index 100% rename from cmd/hway/server/handlers/sync.go rename to web/provider/sync.go diff --git a/cmd/hway/server/handlers/txns.go b/web/provider/txns.go similarity index 100% rename from cmd/hway/server/handlers/txns.go rename to web/provider/txns.go diff --git a/cmd/hway/server/handlers/wss.go b/web/provider/wss.go similarity index 100% rename from cmd/hway/server/handlers/wss.go rename to web/provider/wss.go diff --git a/web/server.go b/web/server.go new file mode 100644 index 000000000..6fa1a8fcc --- /dev/null +++ b/web/server.go @@ -0,0 +1,20 @@ +//go:build js && wasm + +package main + +import ( + "github.com/labstack/echo/v4" + "github.com/onsonr/sonr/internal/session" + "github.com/onsonr/sonr/pkg/nebula/routes" + "github.com/syumai/workers" +) + +func main() { + s := echo.New() + s.Use(session.UseSession) + + s.GET("/", routes.Home) + s.GET("/login", routes.LoginStart) + s.GET("/register", routes.RegisterStart) + workers.Serve(s) +} diff --git a/web/wrangler.toml b/web/wrangler.toml new file mode 100644 index 000000000..ea7559303 --- /dev/null +++ b/web/wrangler.toml @@ -0,0 +1,6 @@ +name = "sonr-id" +main = "./build/worker.mjs" +compatibility_date = "2024-10-07" + +[build] +command = "devbox run build:hway"