Skip to content

Commit

Permalink
#43: web - add prebuild script
Browse files Browse the repository at this point in the history
  • Loading branch information
iignatevich committed Jun 6, 2024
1 parent 33b7d0b commit 4116e69
Show file tree
Hide file tree
Showing 11 changed files with 287 additions and 65 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
*.tar
*.gz
dist/
/swagger-ui
/.husky/_
.idea
assets/
swagger-ui/
36 changes: 28 additions & 8 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ else
LDFLAGS_EXTRA=-s -w
BUILD_OPTS=-trimpath
endif
EMBED?=0
ifeq ($(EMBED), 1)
BUILD_OPTS+=-tags embed
DEV?=0
ifeq ($(DEV), 1)
BUILD_OPTS+=-tags dev
endif

BUILD_ENVPARMS:=CGO_ENABLED=0
Expand All @@ -33,10 +33,18 @@ LOCAL_BIN:=$(CURDIR)/bin
GOLANGCI_BIN:=$(LOCAL_BIN)/golangci-lint
GOLANGCI_TAG:=1.55.2

SWAGGER_UI_DIR:=./swagger-ui
SWAGGER_UI_DIR:=cmd/launchr/assets/github.com/launchrctl/web/swagger-ui
DIST_DIR_SOURCE:=./client/dist
DIST_DIR_DEST:=cmd/launchr/assets/github.com/launchrctl/web

.PHONY: all
all: deps test build
all: clean deps copy-front-build test build

# clean assets folder
.PHONY: clean
clean:
$(info Cleaning assets folder...)
@sudo rm -rf cmd/launchr/assets

# Install go dependencies
.PHONY: deps
Expand All @@ -47,7 +55,7 @@ deps:
echo "Downloading Swagger UI..."; \
curl -Ss https://api.github.com/repos/swagger-api/swagger-ui/releases/latest | grep tarball_url | cut -d '"' -f 4 |\
xargs curl -LsS -o swagger-ui.tar.gz; \
rm -rf $(SWAGGER_UI_DIR) $(SWAGGER_UI_DIR)-tmp && mkdir $(SWAGGER_UI_DIR)-tmp; \
rm -rf $(SWAGGER_UI_DIR) $(SWAGGER_UI_DIR)-tmp && mkdir -p $(SWAGGER_UI_DIR)-tmp; \
tar xzf swagger-ui.tar.gz -C $(SWAGGER_UI_DIR)-tmp --strip=1; \
mv $(SWAGGER_UI_DIR)-tmp/dist $(SWAGGER_UI_DIR) && rm -rf $(SWAGGER_UI_DIR)-tmp && rm swagger-ui.tar.gz; \
sed -i.bkp "s|https://petstore.swagger.io/v2/swagger.json|/api/swagger.json|g" $(SWAGGER_UI_DIR)/swagger-initializer.js; \
Expand All @@ -63,6 +71,9 @@ test:
.PHONY: build
build:
$(info Building launchr...)
ifeq ($(DEV),1)
@echo "development mode"
endif
# Application related information available on build time.
$(eval LDFLAGS:=-X '$(GOPKG).name=launchr' -X '$(GOPKG).version=$(APP_VERSION)' $(LDFLAGS_EXTRA))
$(eval BIN?=$(LOCAL_BIN)/launchr)
Expand Down Expand Up @@ -92,13 +103,22 @@ endif
$(info Running lint...)
$(GOLANGCI_BIN) run --fix ./...


# Front tasks.
front-install:
docker run --rm -it -v $(PWD)/client:/usr/src/app -w /usr/src/app node:$(NODE_TAG) sh -c "corepack install && corepack enable && yarn install"

front-build:
docker run --rm -it -v $(PWD)/client:/usr/src/app -w /usr/src/app node:$(NODE_TAG) sh -c "corepack install && corepack enable && yarn build"
docker run --rm -it -v $(PWD)/client:/usr/src/app -w /usr/src/app node:$(NODE_TAG) sh -c "corepack install && corepack enable && yarn build" \

copy-front-build:
ifeq ($(DEV),0)
$(info Copying front-build into assets...)
@if [ ! -d "$(DIST_DIR_DEST)" ]; then \
echo "Creating assets folder"; \
mkdir -p "$(DIST_DIR_DEST)"; \
fi
@sudo cp -r "$(DIST_DIR_SOURCE)" "$(DIST_DIR_DEST)";
endif

front-dev:
docker run --rm -it -v $(PWD)/client:/usr/src/app -p 5173:5173 -w /usr/src/app node:$(NODE_TAG) sh -c "corepack install && corepack enable && yarn dev -- --host"
Expand Down
4 changes: 3 additions & 1 deletion cmd/launchr/launchr.go → cmd/launchr/launchr.dev.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//go:build dev

// Package executes Launchr application.
package main

Expand All @@ -9,5 +11,5 @@ import (
)

func main() {
os.Exit(launchr.Run())
os.Exit(launchr.Run(&launchr.AppOptions{}))
}
19 changes: 19 additions & 0 deletions cmd/launchr/launchr.os.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
//go:build !dev

// Package executes Launchr application.
package main

import (
"embed"
"os"

"github.com/launchrctl/launchr"
_ "github.com/launchrctl/web"
)

//go:embed assets/*
var assets embed.FS

func main() {
os.Exit(launchr.Run(&launchr.AppOptions{AssetsFs: assets}))
}
22 changes: 22 additions & 0 deletions files.dev.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
//go:build dev

package web

import (
"github.com/launchrctl/web/server"
"io/fs"
"os"
)

func prepareRunOption(_ *Plugin, opts *server.RunOptions) {
opts.SwaggerUIFS = defaultSwaggerUIFS()
opts.ClientFS = defaultClientFS()
}

func defaultSwaggerUIFS() fs.FS {
return os.DirFS("./cmd/launchr/assets/github.com/launchrctl/web/swagger-ui/swagger-ui")
}

func defaultClientFS() fs.FS {
return os.DirFS("./client/dist")
}
30 changes: 0 additions & 30 deletions files.embed.go

This file was deleted.

27 changes: 21 additions & 6 deletions files.os.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,31 @@
//go:build !embed
//go:build !dev

package web

import (
"io/fs"
"os"

"github.com/launchrctl/web/server"
)

func defaultSwaggerUIFS() fs.FS {
return os.DirFS("./swagger-ui")
func prepareRunOption(p *Plugin, opts *server.RunOptions) {
assetsFs := p.app.GetPluginAssets(p)
opts.SwaggerUIFS = defaultSwaggerUIFS(assetsFs)
opts.ClientFS = defaultClientFS(assetsFs)
}

func defaultSwaggerUIFS(assets fs.FS) fs.FS {
sub, err := fs.Sub(assets, "swagger-ui")
if err != nil {
panic(err)
}
return sub
}

func defaultClientFS() fs.FS {
return os.DirFS("./client/dist")
func defaultClientFS(assets fs.FS) fs.FS {
sub, err := fs.Sub(assets, "dist")
if err != nil {
panic(err)
}
return sub
}
8 changes: 4 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@ require (
github.com/go-chi/chi/v5 v5.0.11
github.com/go-chi/cors v1.2.1
github.com/go-chi/render v1.0.3
github.com/launchrctl/launchr v0.5.7
github.com/launchrctl/launchr v0.8.2-0.20240513162729-0023678afad2
github.com/oapi-codegen/nethttp-middleware v1.0.1
github.com/oapi-codegen/runtime v1.1.1
github.com/spf13/cobra v1.8.0
gopkg.in/yaml.v3 v3.0.1
)

require (
Expand Down Expand Up @@ -50,7 +51,7 @@ require (
github.com/mohae/deepcopy v0.0.0-20170929034955-c48cc78d4826 // indirect
github.com/morikuni/aec v1.0.0 // indirect
github.com/opencontainers/go-digest v1.0.0 // indirect
github.com/opencontainers/image-spec v1.1.0-rc6 // indirect
github.com/opencontainers/image-spec v1.1.0 // indirect
github.com/perimeterx/marshmallow v1.1.5 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/santhosh-tekuri/jsonschema/v5 v5.3.1 // indirect
Expand All @@ -64,7 +65,6 @@ require (
golang.org/x/mod v0.15.0 // indirect
golang.org/x/sys v0.17.0 // indirect
golang.org/x/text v0.14.0 // indirect
golang.org/x/tools v0.17.0 // indirect
golang.org/x/tools v0.18.0 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
16 changes: 8 additions & 8 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,8 @@ github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk=
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
github.com/launchrctl/launchr v0.5.7 h1:F02oAwxX/XM9oz6kw2Wvg1HXbLbYaN+znpgiS/1yt3c=
github.com/launchrctl/launchr v0.5.7/go.mod h1:mz7JSPdg/PqRX3iUZdln8n0kmq/B7vhC8sPeT9z5LHs=
github.com/launchrctl/launchr v0.8.2-0.20240513162729-0023678afad2 h1:FhZXR1ktpyBtZHHFbpAlYyersdS7twx+CQ4jJBIzz0A=
github.com/launchrctl/launchr v0.8.2-0.20240513162729-0023678afad2/go.mod h1:et+ykNbE3m7mMPydWKDV/6slFus1CD1vOaGH+j5uJ3M=
github.com/mailru/easyjson v0.7.7 h1:UGYAvKxe3sBsEDzO8ZeWOSlIQfWFlxbzLZe7hwFURr0=
github.com/mailru/easyjson v0.7.7/go.mod h1:xzfreul335JAWq5oZzymOObrkdz5UnU4kGfJJLY9Nlc=
github.com/moby/moby v25.0.3+incompatible h1:Uzxm7JQOHBY8kZY2fa95a9kg0aTOt1cBidSZ+LXCxC4=
Expand All @@ -110,8 +110,8 @@ github.com/oapi-codegen/runtime v1.1.1 h1:EXLHh0DXIJnWhdRPN2w4MXAzFyE4CskzhNLUmt
github.com/oapi-codegen/runtime v1.1.1/go.mod h1:SK9X900oXmPWilYR5/WKPzt3Kqxn/uS/+lbpREv+eCg=
github.com/opencontainers/go-digest v1.0.0 h1:apOUWs51W5PlhuyGyz9FCeeBIOUDA/6nW8Oi/yOhh5U=
github.com/opencontainers/go-digest v1.0.0/go.mod h1:0JzlMkj0TRzQZfJkVvzbP0HBR3IKzErnv2BNG4W4MAM=
github.com/opencontainers/image-spec v1.1.0-rc6 h1:XDqvyKsJEbRtATzkgItUqBA7QHk58yxX1Ov9HERHNqU=
github.com/opencontainers/image-spec v1.1.0-rc6/go.mod h1:W4s4sFTMaBeK1BQLXbG4AdM2szdn85PY75RI83NrTrM=
github.com/opencontainers/image-spec v1.1.0 h1:8SG7/vwALn54lVB/0yZ/MMwhFrPYtpEHQb2IpWsCzug=
github.com/opencontainers/image-spec v1.1.0/go.mod h1:W4s4sFTMaBeK1BQLXbG4AdM2szdn85PY75RI83NrTrM=
github.com/perimeterx/marshmallow v1.1.5 h1:a2LALqQ1BlHM8PZblsDdidgv1mWi1DgC2UmX50IvK2s=
github.com/perimeterx/marshmallow v1.1.5/go.mod h1:dsXbUu8CRzfYP5a87xpp0xq9S3u0Vchtcl8we9tYaXw=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
Expand Down Expand Up @@ -170,8 +170,8 @@ golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20200226121028-0de0cce0169b/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20201021035429-f5854403a974/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU=
golang.org/x/net v0.20.0 h1:aCL9BSgETF1k+blQaYUBx9hJ9LOGP3gAVemcZlf1Kpo=
golang.org/x/net v0.20.0/go.mod h1:z8BVo6PvndSri0LbOE3hAn0apkU+1YvI6E70E9jsnvY=
golang.org/x/net v0.21.0 h1:AQyQV4dYCvJ7vGmJyKki9+PBdyvhkSd8EIx/qb0AYv4=
golang.org/x/net v0.21.0/go.mod h1:bIjVDfnllIU7BJ2DNgfnXvpSvtn8VRwhlsaeUTyUS44=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
Expand All @@ -195,8 +195,8 @@ golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGm
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.0.0-20200619180055-7c47624df98f/go.mod h1:EkVYQZoAsY45+roYkvgYkIh4xh/qjgUK9TdY2XT94GE=
golang.org/x/tools v0.0.0-20210106214847-113979e3529a/go.mod h1:emZCQorbCU4vsT4fOWvOPXz4eW1wZW4PmDk9uLelYpA=
golang.org/x/tools v0.17.0 h1:FvmRgNOcs3kOa+T20R1uhfP9F6HgG2mfxDv1vrx1Htc=
golang.org/x/tools v0.17.0/go.mod h1:xsh6VxdV005rRVaS6SSAf9oiAqljS7UZUacMZ8Bnsps=
golang.org/x/tools v0.18.0 h1:k8NLag8AGHnn+PHbl7g43CtqZAwG60vZkLqgyZgIHgQ=
golang.org/x/tools v0.18.0/go.mod h1:GL7B4CwcLLeo59yx/9UWWuNOW1n3VZ4f5axWfML7Lcg=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
Expand Down
15 changes: 8 additions & 7 deletions plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,9 @@ package web
import (
"fmt"

"github.com/spf13/cobra"

"github.com/launchrctl/launchr"

"github.com/launchrctl/web/server"
"github.com/spf13/cobra"
)

// APIPrefix is a default api prefix on the server.
Expand Down Expand Up @@ -46,15 +44,18 @@ func (p *Plugin) CobraAddCommands(rootCmd *cobra.Command) error {
RunE: func(cmd *cobra.Command, args []string) error {
// Don't show usage help on a runtime error.
cmd.SilenceUsage = true
return server.Run(cmd.Context(), p.app, &server.RunOptions{

runOpts := &server.RunOptions{
Addr: fmt.Sprintf(":%s", port), // @todo use proper addr
APIPrefix: APIPrefix,
SwaggerJSON: useSwaggerUI,
SwaggerUIFS: defaultSwaggerUIFS(),
ClientFS: defaultClientFS(),
ProxyClient: proxyClient,
// @todo use embed fs for client or provide path ?
})
}

prepareRunOption(p, runOpts)

return server.Run(cmd.Context(), p.app, runOpts)
},
}
cmd.Flags().StringVarP(&port, "port", "p", "8080", `Web server port`)
Expand Down
Loading

0 comments on commit 4116e69

Please sign in to comment.