From 7ff1c1d22298506667b971454c850539275e0222 Mon Sep 17 00:00:00 2001 From: Roland Bewick Date: Wed, 10 Jan 2024 12:40:15 +0700 Subject: [PATCH] feat: embed react dist in go executable --- .dockerignore | 2 ++ Dockerfile | 11 +++++++---- README.md | 24 ++++++++++++++++++++++++ frontend/frontend.go | 13 ++++++++----- 4 files changed, 41 insertions(+), 9 deletions(-) create mode 100644 .dockerignore diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 00000000..8a9a009b --- /dev/null +++ b/.dockerignore @@ -0,0 +1,2 @@ +frontend/node_modules +frontend/dist \ No newline at end of file diff --git a/Dockerfile b/Dockerfile index b5003c87..e970f841 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,3 +1,8 @@ +FROM node:18-alpine as frontend +WORKDIR /build +COPY frontend ./frontend +RUN cd frontend && yarn install && yarn build + FROM golang:1.20-alpine as builder # Move to working directory /build @@ -11,7 +16,8 @@ RUN go mod download # Copy the code into the container COPY . . -# TODO: build react app? +# Copy frontend dist files into the container +COPY --from=frontend /build/frontend/dist ./frontend/dist # Build the application RUN go build -o main @@ -21,8 +27,5 @@ FROM alpine as final # Copy the binaries and entrypoint from the builder image. COPY --from=builder /build/main /bin/ -# NOTE: should not be needed - assets should be embedded in the go app -#COPY --from=builder /build/public /public/ -#COPY --from=builder /build/views /views/ ENTRYPOINT [ "/bin/main" ] diff --git a/README.md b/README.md index d304f615..b0a50fa3 100644 --- a/README.md +++ b/README.md @@ -50,6 +50,21 @@ Go to `/frontend` Follow standard LND instructions. After logging in, you will be redirected to the wrong port (8080), so manually re-open . +### Build and run locally + +`mkdir tmp` +`go build -o main` +`cp main tmp` +`cp .env tmp` +`cd tmp` +`./main` + +### Run dockerfile locally + +`docker build . -t nwc-local` + +`docker run --env-file .env -p 8080:8080 nwc-local` + ### Testing `go test` @@ -148,6 +163,7 @@ You can also contribute to our [bounty program](https://github.com/getAlby/light ✅ `get_balance` ✅ `pay_invoice` + - ⚠️ amount not supported (for amountless invoices) ✅ `pay_keysend` @@ -178,6 +194,7 @@ You can also contribute to our [bounty program](https://github.com/getAlby/light ✅ `get_balance` ✅ `pay_invoice` + - ⚠️ amount not supported (for amountless invoices) ✅ `pay_keysend` @@ -201,3 +218,10 @@ You can also contribute to our [bounty program](https://github.com/getAlby/light ❌ `multi_pay_invoice` ❌ `multi_pay_keysend (TBC)` + +## Node Distributions + +Run NWC on your own node! + +- [https://github.com/getAlby/umbrel-community-app-store](Umbrel) +- [https://github.com/horologger/nostr-wallet-connect-startos](Start9) (WIP) diff --git a/frontend/frontend.go b/frontend/frontend.go index 42c56a56..203dd9b1 100644 --- a/frontend/frontend.go +++ b/frontend/frontend.go @@ -1,23 +1,26 @@ package frontend import ( + "embed" + "net/http" + "github.com/labstack/echo/v4" "github.com/labstack/echo/v4/middleware" ) +//go:embed dist +var embeddedReactAssets embed.FS + func RegisterHandlers(e *echo.Echo) { e.Use(middleware.StaticWithConfig(middleware.StaticConfig{ Skipper: nil, // Root directory from where the static content is served. - Root: "frontend/dist", - // Index file for serving a directory. - // Optional. Default value "index.html". - Index: "index.html", + Root: "dist", // Enable HTML5 mode by forwarding all not-found requests to root so that // SPA (single-page application) can handle the routing. HTML5: true, Browse: false, IgnoreBase: false, - Filesystem: nil, + Filesystem: http.FS(embeddedReactAssets), })) }