Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Release: v1.13.0 #221

Merged
merged 8 commits into from
May 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 4 additions & 4 deletions .github/workflows/pull_request.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,13 @@ jobs:
- name: Run ESLint
run: yarn lint
working-directory: ./web
- name: golangci-lint
uses: golangci/golangci-lint-action@v3.4.0
with:
version: v1.44.2
- name: Setup Go
uses: actions/setup-go@v4
with:
go-version: "^1.18.0"
- name: golangci-lint
uses: golangci/golangci-lint-action@v3.4.0
with:
version: v1.52.2
- run: go version
- run: go test ./...
6 changes: 5 additions & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ on:
repository_dispatch:
types: manual-deploy

env:
GO_VERSION: 1.19
PREV_GO_VERSION: 1.18

jobs:
build:
runs-on: ubuntu-latest
Expand Down Expand Up @@ -58,7 +62,7 @@ jobs:
uses: addnab/docker-run-action@v3
with:
image: node:lts-gallium
options: -v ${{ github.workspace }}/web:/work -e REACT_APP_VERSION=${{ env.RELEASE_VERSION }} -e REACT_APP_GITHUB_URL=https://github.com/x1unix/go-playground -e NODE_ENV=production
options: -v ${{ github.workspace }}/web:/work -e REACT_APP_VERSION=${{ env.RELEASE_VERSION }} -e REACT_APP_GITHUB_URL=https://github.com/x1unix/go-playground -e NODE_ENV=production -e REACT_APP_GO_VERSION=${{ env.GO_VERSION }} REACT_APP_PREV_GO_VERSION=${{ env.PREV_GO_VERSION }}
run: |
yarn --cwd='/work' install --silent && yarn --cwd='/work' build
- name: Build and push image
Expand Down
5 changes: 5 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,8 @@ run:
skip-dirs:
- pkg/worker
- cmd/webworker
issues:
exclude-rules:
- path: 'internal/gowasm/(.+)\.go'
linters:
- typecheck
1 change: 1 addition & 0 deletions .nvmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
v19.0.0
22 changes: 19 additions & 3 deletions build.mk
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ GOROOT ?= $(shell go env GOROOT)
TOOLS ?= ./tools
PUBLIC_DIR ?= $(UI)/public
WEBWORKER_PKG ?= ./cmd/webworker
INTERPRETER_PKG ?= ./cmd/go-repl

.PHONY: clean
clean:
Expand Down Expand Up @@ -44,15 +45,30 @@ build-ui:
@echo ":: Building UI..." && \
$(YARN) --cwd="$(UI)" build

.PHONY:copy-wasm-exec
copy-wasm-exec:
@cp "$(GOROOT)/misc/wasm/wasm_exec.js" $(PUBLIC_DIR)

.PHONY:build-webworker
build-webworker:
@echo ":: Building Go Webworker module..." && \
GOOS=js GOARCH=wasm $(GO) build -o $(PUBLIC_DIR)/worker.wasm $(WEBWORKER_PKG) && \
cp "$(GOROOT)/misc/wasm/wasm_exec.js" $(PUBLIC_DIR)
GOOS=js GOARCH=wasm $(GO) build -o $(PUBLIC_DIR)/worker.wasm $(WEBWORKER_PKG)

.PHONY:go-repl
go-repl:
@echo ":: Building Go interpreter module..." && \
GOOS=js GOARCH=wasm $(GO) build -o $(PUBLIC_DIR)/go-repl.wasm $(INTERPRETER_PKG)

.PHONY:build-wasm
build-wasm: copy-wasm-exec build-webworker go-repl

.PHONY: build
build: check-go check-yarn clean preinstall collect-meta build-server build-webworker build-ui
build: check-go check-yarn clean preinstall collect-meta build-server build-wasm build-ui
@echo ":: Copying assets..." && \
cp -rfv ./data $(TARGET)/data && \
mv -v $(UI)/build $(TARGET)/public && \
echo ":: Build done - $(TARGET)"

.PHONY:gen
gen:
@find . -name '*_js.go' -print0 | xargs -0 -n1 go generate
16 changes: 11 additions & 5 deletions build/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,14 @@ WORKDIR /tmp/web
ARG APP_VERSION=1.0.0
ARG GITHUB_URL='https://github.com/x1unix/go-playground'
RUN yarn install --silent && \
REACT_APP_VERSION=$APP_VERSION NODE_ENV=production REACT_APP_GITHUB_URL=$GITHUB_URL yarn build
REACT_APP_VERSION=$APP_VERSION \
NODE_ENV=production \
REACT_APP_GITHUB_URL=$GITHUB_URL \
REACT_APP_GO_VERSION=$GO_VERSION \
REACT_APP_PREV_GO_VERSION=$PREV_GO_VERSION \
yarn build

FROM golang:1.18-alpine as build
FROM golang:1.19-alpine as build
WORKDIR /tmp/playground
COPY cmd ./cmd
COPY pkg ./pkg
Expand All @@ -15,21 +20,22 @@ COPY go.sum .
ARG APP_VERSION=1.0.0
RUN echo "Building server with version $APP_VERSION" && \
go build -o server -ldflags="-X 'main.Version=$APP_VERSION'" ./cmd/playground && \
GOOS=js GOARCH=wasm go build -o ./go-repl.wasm ./cmd/go-repl && \
GOOS=js GOARCH=wasm go build -o ./worker.wasm ./cmd/webworker && \
cp $(go env GOROOT)/misc/wasm/wasm_exec.js .

FROM golang:1.18-alpine as production
FROM golang:1.19-alpine as production
WORKDIR /opt/playground
ENV GOROOT /usr/local/go
ENV APP_CLEAN_INTERVAL=10m
ENV APP_DEBUG=false
ENV APP_PLAYGROUND_URL='https://play.golang.org'
ENV APP_GOTIP_URL='https://gotipplay.golang.org'
ENV APP_PLAYGROUND_URL='https://go.dev/_'
ENV APP_GTAG_ID=''
COPY data ./data
COPY --from=ui-build /tmp/web/build ./public
COPY --from=build /tmp/playground/server .
COPY --from=build /tmp/playground/worker.wasm ./public
COPY --from=build /tmp/playground/go-repl.wasm ./public
COPY --from=build /tmp/playground/wasm_exec.js ./public
EXPOSE 8000
ENTRYPOINT /opt/playground/server \
Expand Down
9 changes: 5 additions & 4 deletions build/release.dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#
# Use Dockerfile for simple single-arch build process

FROM golang:1.18-alpine as build
FROM golang:1.19-alpine as build
WORKDIR /tmp/playground
COPY cmd ./cmd
COPY pkg ./pkg
Expand All @@ -17,20 +17,21 @@ ARG APP_VERSION=1.0.0
RUN echo "Building server with version $APP_VERSION" && \
go build -o server -ldflags="-X 'main.Version=$APP_VERSION'" ./cmd/playground && \
GOOS=js GOARCH=wasm go build -o ./worker.wasm ./cmd/webworker && \
GOOS=js GOARCH=wasm go build -o ./go-repl.wasm ./cmd/go-repl && \
cp $(go env GOROOT)/misc/wasm/wasm_exec.js .

FROM golang:1.18-alpine as production
FROM golang:1.19-alpine as production
WORKDIR /opt/playground
ENV GOROOT /usr/local/go
ENV APP_CLEAN_INTERVAL=10m
ENV APP_DEBUG=false
ENV APP_PLAYGROUND_URL='https://play.golang.org'
ENV APP_GOTIP_URL='https://gotipplay.golang.org'
ENV APP_PLAYGROUND_URL='https://go.dev/_'
ENV APP_GTAG_ID=''
COPY data ./data
COPY web/build ./public
COPY --from=build /tmp/playground/server .
COPY --from=build /tmp/playground/worker.wasm ./public
COPY --from=build /tmp/playground/go-repl.wasm ./public
COPY --from=build /tmp/playground/wasm_exec.js ./public
EXPOSE 8000
ENTRYPOINT /opt/playground/server \
Expand Down
36 changes: 36 additions & 0 deletions cmd/go-repl/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
//go:build js

//
// "go-repl" is a self-contained Go REPL with package downloader to be run in web browser as WASM binary.
//

package main

import (
"github.com/x1unix/go-playground/internal/gorepl"
"github.com/x1unix/go-playground/internal/gorepl/uihost"
"github.com/x1unix/go-playground/internal/gowasm"
"github.com/x1unix/go-playground/internal/gowasm/browserfs"
"github.com/x1unix/go-playground/internal/gowasm/packagedb"
"github.com/x1unix/go-playground/pkg/goproxy"
)

func main() {
// Go memory ballast, 10MiB
_ = make([]byte, 10<<20)

worker := gowasm.NewWorker()

vendorFS := browserfs.NewFS()
pkgIndex := packagedb.NewPackageIndex()
client := goproxy.NewClientFromEnv()
pmObserver := uihost.NewPackageDownloadObserver()
pmObserver.Start(worker.Context())

runner := gorepl.NewRunner(vendorFS, pkgIndex, client, pmObserver)
handler := gorepl.NewHandler(client, runner)
worker.Export("runProgram", handler.HandleRunProgram)
worker.Export("terminateProgram", handler.HandleTerminateProgram)
worker.Export("updateGoProxyAddress", handler.HandleUpdateGoProxyAddress)
worker.Run()
}
22 changes: 8 additions & 14 deletions cmd/playground/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ import (

"github.com/gorilla/mux"
"github.com/x1unix/foundation/app"
"github.com/x1unix/go-playground/pkg/analyzer"
"github.com/x1unix/go-playground/pkg/compiler"
"github.com/x1unix/go-playground/pkg/compiler/storage"
"github.com/x1unix/go-playground/pkg/config"
"github.com/x1unix/go-playground/internal/analyzer"
"github.com/x1unix/go-playground/internal/compiler"
"github.com/x1unix/go-playground/internal/compiler/storage"
"github.com/x1unix/go-playground/internal/config"
"github.com/x1unix/go-playground/internal/langserver"
"github.com/x1unix/go-playground/internal/langserver/webutil"
"github.com/x1unix/go-playground/pkg/goplay"
"github.com/x1unix/go-playground/pkg/langserver"
"github.com/x1unix/go-playground/pkg/langserver/webutil"
"github.com/x1unix/go-playground/pkg/util/cmdutil"
"github.com/x1unix/go-playground/pkg/util/osutil"
"go.uber.org/zap"
Expand Down Expand Up @@ -68,14 +68,8 @@ func start(goRoot string, logger *zap.Logger, cfg *config.Config) error {
go store.StartCleaner(ctx, cfg.Build.CleanupInterval, nil)

// Initialize services
pgClient := goplay.NewClient(cfg.Playground.PlaygroundURL, goplay.DefaultUserAgent,
playgroundClient := goplay.NewClient(cfg.Playground.PlaygroundURL, goplay.DefaultUserAgent,
cfg.Playground.ConnectTimeout)
goTipClient := goplay.NewClient(cfg.Playground.GoTipPlaygroundURL, goplay.DefaultUserAgent,
cfg.Playground.ConnectTimeout)
clients := &langserver.PlaygroundServices{
Default: pgClient,
GoTip: goTipClient,
}
buildCfg := compiler.BuildEnvironmentConfig{
IncludedEnvironmentVariables: osutil.SelectEnvironmentVariables(cfg.Build.BypassEnvVarsList...),
}
Expand All @@ -86,7 +80,7 @@ func start(goRoot string, logger *zap.Logger, cfg *config.Config) error {
// Initialize API endpoints
r := mux.NewRouter()
svcCfg := langserver.ServiceConfig{Version: Version}
langserver.New(svcCfg, clients, packages, buildSvc).
langserver.New(svcCfg, playgroundClient, packages, buildSvc).
Mount(r.PathPrefix("/api").Subrouter())

// Web UI routes
Expand Down
59 changes: 5 additions & 54 deletions cmd/webworker/webworker.go
Original file line number Diff line number Diff line change
@@ -1,68 +1,19 @@
//go:build js
// +build js

package main

import (
"fmt"
"os"
"syscall/js"

"github.com/x1unix/go-playground/pkg/analyzer/check"

"github.com/x1unix/go-playground/internal/analyzer/check"
"github.com/x1unix/go-playground/pkg/worker"
)

type void = struct{}

var (
done = make(chan void, 0)
onResult js.Value
)

func main() {
entrypoint, err := getEntrypointFunction()
if err != nil {
panic(err)
}

// prepare exports object
analyzeCodeCb := worker.FuncOf(analyzeCode)
exitCb := js.FuncOf(exit)
module := map[string]interface{}{
"analyzeCode": analyzeCodeCb,
"exit": exitCb,
}

defer analyzeCodeCb.Release()
defer exitCb.Release()

entrypoint.Invoke(js.ValueOf(module))
<-done
fmt.Println("Go: exit")
}

func getEntrypointFunction() (js.Value, error) {
if len(os.Args) < 2 {
return js.Value{}, fmt.Errorf("WASM module requires at least 2 arguments: 'js' and entrypoint function name")
}

entrypointName := os.Args[1]
entrypoint := js.Global().Get(entrypointName)
switch t := entrypoint.Type(); t {
case js.TypeFunction:
return entrypoint, nil
case js.TypeUndefined:
return js.Value{}, fmt.Errorf("function %q doesn't exists on global JS scope", entrypointName)
default:
return js.Value{}, fmt.Errorf("%q should be callable JS function, but got %d instead", entrypointName, t)
}
}

func exit(this js.Value, args []js.Value) interface{} {
go func() {
done <- void{}
}()
return nil
worker.ExportAndStart(worker.Exports{
"analyzeCode": analyzeCode,
})
}

func analyzeCode(this js.Value, args worker.Args) (interface{}, error) {
Expand Down
9 changes: 9 additions & 0 deletions docs/wasm/dirlist.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
js: registered callback handler {value: {…}, id: 1}
fs.Open - "/go/src/main/vendor/go.uber.org/zap"
fs.Open - "/go/src/main/go.uber.org/zap"
fs.Open - "/go/src/vendor/go.uber.org/zap"
fs.Open - "/go/src/go.uber.org/zap"
fs.Open - "/go/src/vendor/go.uber.org/zap"
fs.Open - "/go/src/go.uber.org/zap"
Result: <nil>
Error: 4:2: import "go.uber.org/zap" error: unable to find source related to: "go.uber.org/zap". Either the GOPATH environment variable, or the Interpreter.Options.GoPath needs to be set