feat: embed react dist in go executable

This commit is contained in:
Roland Bewick 2024-01-10 12:40:15 +07:00
parent 2a3c02d942
commit 7ff1c1d222
4 changed files with 41 additions and 9 deletions

2
.dockerignore Normal file
View file

@ -0,0 +1,2 @@
frontend/node_modules
frontend/dist

View file

@ -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" ]

View file

@ -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 <http://localhost:5173>.
### 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)

View file

@ -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),
}))
}