multi: replace statik with go1.16 embed directive

We can now simply use the //go:embed directive to compile everything in
the app/build directory into our root package.
This commit is contained in:
Oliver Gugger 2021-02-18 13:45:45 +01:00
parent b1d820c5ca
commit 90f08a6467
No known key found for this signature in database
GPG key ID: 8E4256593F177720
8 changed files with 27 additions and 47 deletions

3
.gitignore vendored
View file

@ -4,8 +4,5 @@
litd-debug
/lightning-terminal-*
# go code generated by statik
/statik/statik.go
# MacOS junk
.DS_Store

View file

@ -46,7 +46,6 @@ ENV GO111MODULE on
RUN apk add --no-cache --update alpine-sdk \
make \
&& cd /go/src/github.com/lightninglabs/lightning-terminal \
&& make statik-only \
&& make go-install \
&& make go-install-cli

View file

@ -7,13 +7,11 @@ POOL_PKG := github.com/lightninglabs/pool
LINT_PKG := github.com/golangci/golangci-lint/cmd/golangci-lint
GOVERALLS_PKG := github.com/mattn/goveralls
GOACC_PKG := github.com/ory/go-acc
STATIK_PKG := github.com/rakyll/statik
GO_BIN := ${GOPATH}/bin
GOVERALLS_BIN := $(GO_BIN)/goveralls
LINT_BIN := $(GO_BIN)/golangci-lint
GOACC_BIN := $(GO_BIN)/go-acc
STATIK_BIN := $(GO_BIN)/statik
COMMIT := $(shell git describe --abbrev=40 --dirty --tags)
COMMIT_HASH := $(shell git rev-parse HEAD)
@ -94,10 +92,6 @@ $(GOACC_BIN):
@$(call print, "Fetching go-acc")
$(DEPGET) $(GOACC_PKG)@$(GOACC_COMMIT)
$(STATIK_BIN):
@$(call print, "Fetching statik")
$(DEPGET) $(STATIK_PKG)
yarn-install:
@$(call print, "Installing app dependencies with yarn")
cd app; yarn
@ -105,14 +99,9 @@ yarn-install:
# ============
# INSTALLATION
# ============
statik-only: $(STATIK_BIN)
@$(call print, "Building statik package.")
statik -src=app/build
statik-build: app-build statik-only
build: statik-build go-build
install: statik-build go-install
build: app-build go-build
install: app-build go-install
go-build:
@$(call print, "Building lightning-terminal.")
@ -133,7 +122,7 @@ app-build: yarn-install
@$(call print, "Building production app.")
cd app; yarn build
release: statik-build
release: app-build
@$(call print, "Creating release of lightning-terminal.")
./release.sh build-release "$(VERSION_TAG)" "$(BUILD_SYSTEM)" "$(LND_RELEASE_TAGS)" "$(RELEASE_LDFLAGS)"
@ -214,4 +203,3 @@ clean:
@$(call print, "Cleaning source.$(NC)")
$(RM) ./lightning-terminal-debug
$(RM) coverage.txt
$(RM) -r statik

View file

@ -27,7 +27,6 @@ ENV GO111MODULE on
RUN apk add --no-cache --update alpine-sdk \
make \
&& cd /go/src/github.com/lightninglabs/lightning-terminal \
&& make statik-only \
&& make go-install \
&& make go-install-cli

1
go.mod
View file

@ -18,7 +18,6 @@ require (
github.com/mwitkow/go-conntrack v0.0.0-20190716064945-2f068394615f
github.com/mwitkow/grpc-proxy v0.0.0-20181017164139-0f1106ef9c76
github.com/prometheus/client_golang v1.5.1 // indirect
github.com/rakyll/statik v0.1.7
github.com/rs/cors v1.7.0 // indirect
golang.org/x/crypto v0.0.0-20200709230013-948cd5f35899
golang.org/x/net v0.0.0-20200324143707-d3edc9973b7e // indirect

2
go.sum
View file

@ -314,8 +314,6 @@ github.com/prometheus/procfs v0.0.2/go.mod h1:TjEm7ze935MbeOT/UhFTIMYKhuLP4wbCsT
github.com/prometheus/procfs v0.0.8 h1:+fpWZdT24pJBiqJdAwYBjPSk+5YmQzYNPYzQsdzLkt8=
github.com/prometheus/procfs v0.0.8/go.mod h1:7Qr8sr6344vo1JqZ6HhLceV9o3AJ1Ff+GxbHq6oeK9A=
github.com/prometheus/tsdb v0.7.1/go.mod h1:qhTCs0VvXwvX/y3TZrWD7rabWM+ijKTux40TwIPHuXU=
github.com/rakyll/statik v0.1.7 h1:OF3QCZUuyPxuGEP7B4ypUa7sB/iHtqOTDYZXGM8KOdQ=
github.com/rakyll/statik v0.1.7/go.mod h1:AlZONWzMtEnMs7W4e/1LURLiI49pIMmp6V9Unghqrcc=
github.com/rogpeppe/fastuuid v1.2.0 h1:Ppwyp6VYCF1nvBTXL3trRso7mXMlRrw9ooo375wvi2s=
github.com/rogpeppe/fastuuid v1.2.0/go.mod h1:jVj6XXZzXRy/MSR5jhDC/2q6DgLz+nrA6LYCDYWNEvQ=
github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4=

View file

@ -1,13 +0,0 @@
/*
This package does not contain any source code by default other than this
comment.
The web application's static assets (HTML, CSS, JS, images, etc.) will
be packaged, compressed, then converted to go code and saved to a file called
`statik.go` in this package during the build process.
Run the command `make statik-build` in the root directory to explicitly trigger
that part of the build or simply run `make build` to build the entire
application.
*/
package statik

View file

@ -3,8 +3,10 @@ package terminal
import (
"context"
"crypto/tls"
"embed"
"errors"
"fmt"
"io/fs"
"net"
"net/http"
"os"
@ -29,15 +31,10 @@ import (
"github.com/lightningnetwork/lnd/lnrpc"
"github.com/lightningnetwork/lnd/lntest/wait"
"github.com/lightningnetwork/lnd/signal"
"github.com/rakyll/statik/fs"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"gopkg.in/macaroon-bakery.v2/bakery"
// Import generated go package that contains all static files for the
// UI in a compressed format.
_ "github.com/lightninglabs/lightning-terminal/statik"
)
const (
@ -50,6 +47,19 @@ var (
// maxMsgRecvSize is the largest message our REST proxy will receive. We
// set this to 200MiB atm.
maxMsgRecvSize = grpc.MaxCallRecvMsgSize(1 * 1024 * 1024 * 200)
// appBuildFS is an in-memory file system that contains all the static
// HTML/CSS/JS files of the UI. It is compiled into the binary with the
// go 1.16 embed directive below. Because the path is relative to the
// root package, all assets will have a path prefix of /app/build/ which
// we'll strip by giving a sub directory to the HTTP server.
//
//go:embed app/build/*
appBuildFS embed.FS
// appFilesDir is the sub directory of the above build directory which
// we pass to the HTTP server.
appFilesDir = "app/build"
)
// LightningTerminal is the main grand unified binary instance. Its task is to
@ -523,14 +533,14 @@ func (g *LightningTerminal) shutdown() error {
}
// startMainWebServer creates the main web HTTP server that delegates requests
// between the Statik HTTP server and the RPC proxy. An incoming request will
// between the embedded HTTP server and the RPC proxy. An incoming request will
// go through the following chain of components:
//
// Request on port 8443
// |
// v
// +---+----------------------+ other +----------------+
// | Main web HTTP server +------->+ Statik HTTP |
// | Main web HTTP server +------->+ Embedded HTTP |
// +---+----------------------+ +----------------+
// |
// v any RPC or REST call
@ -566,12 +576,15 @@ func (g *LightningTerminal) shutdown() error {
//
func (g *LightningTerminal) startMainWebServer() error {
// Initialize the in-memory file server from the content compiled by
// the statik library.
statikFS, err := fs.New()
// the go:embed directive. Since everything's relative to the root dir,
// we need to create an FS of the sub directory app/build.
buildDir, err := fs.Sub(appBuildFS, appFilesDir)
if err != nil {
return fmt.Errorf("could not load statik file system: %v", err)
return err
}
staticFileServer := http.FileServer(&ClientRouteWrapper{statikFS})
staticFileServer := http.FileServer(&ClientRouteWrapper{
assets: http.FS(buildDir),
})
// Both gRPC (web) and static file requests will come into through the
// main UI HTTP server. We use this simple switching handler to send the