From fb0777e6d3b7a5132d1734b8d09630944016993b Mon Sep 17 00:00:00 2001 From: x1unix Date: Tue, 18 Aug 2020 19:58:14 +0300 Subject: [PATCH 1/3] allow custom playground URL --- cmd/playground/main.go | 7 ++++++- pkg/goplay/client.go | 6 +++--- pkg/goplay/client_test.go | 4 ++-- pkg/langserver/server.go | 4 ++-- 4 files changed, 13 insertions(+), 8 deletions(-) diff --git a/cmd/playground/main.go b/cmd/playground/main.go index 1a47997b..8346305c 100644 --- a/cmd/playground/main.go +++ b/cmd/playground/main.go @@ -14,6 +14,7 @@ import ( "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/goplay" "github.com/x1unix/go-playground/pkg/langserver" "go.uber.org/zap" ) @@ -23,6 +24,7 @@ var Version = "testing" type appArgs struct { packagesFile string + playgroundUrl string addr string debug bool buildDir string @@ -39,6 +41,7 @@ func main() { flag.StringVar(&args.addr, "addr", ":8080", "TCP Listen address") flag.StringVar(&args.buildDir, "wasm-build-dir", os.TempDir(), "Directory for WASM builds") flag.StringVar(&args.cleanupInterval, "clean-interval", "10m", "Build directory cleanup interval") + flag.StringVar(&args.playgroundUrl, "playground-url", goplay.DefaultPlaygroundURL, "Go Playground URL") flag.BoolVar(&args.debug, "debug", false, "Enable debug mode") goRoot, ok := os.LookupEnv("GOROOT") @@ -80,6 +83,7 @@ func start(goRoot string, args appArgs) error { zap.S().Info("Server version: ", Version) zap.S().Infof("GOROOT is %q", goRoot) + zap.S().Infof("Playground url: %q", args.playgroundUrl) zap.S().Infof("Packages file is %q", args.packagesFile) zap.S().Infof("Cleanup interval is %s", cleanInterval.String()) analyzer.SetRoot(goRoot) @@ -98,7 +102,8 @@ func start(goRoot string, args appArgs) error { go store.StartCleaner(ctx, cleanInterval, nil) r := mux.NewRouter() - langserver.New(Version, packages, compiler.NewBuildService(zap.S(), store)). + pg := goplay.NewClient(args.playgroundUrl, goplay.DefaultUserAgent, 15*time.Second) + langserver.New(Version, pg, packages, compiler.NewBuildService(zap.S(), store)). Mount(r.PathPrefix("/api").Subrouter()) r.PathPrefix("/").Handler(langserver.SpaFileServer("./public")) diff --git a/pkg/goplay/client.go b/pkg/goplay/client.go index a114e3c8..0ee5c00a 100644 --- a/pkg/goplay/client.go +++ b/pkg/goplay/client.go @@ -13,8 +13,8 @@ import ( ) const ( - defaultUserAgent = "goplay.tools/1.0 (http://goplay.tools/)" - playgroundUrl = "https://play.golang.org" + DefaultUserAgent = "goplay.tools/1.0 (http://goplay.tools/)" + DefaultPlaygroundURL = "https://play.golang.org" // maxSnippetSize value taken from // https://github.com/golang/playground/blob/master/app/goplay/share.go @@ -50,7 +50,7 @@ func NewClient(baseUrl, userAgent string, timeout time.Duration) *Client { // NewDefaultClient returns Go Playground client with defaults func NewDefaultClient() *Client { - return NewClient(playgroundUrl, defaultUserAgent, 15*time.Second) + return NewClient(DefaultPlaygroundURL, DefaultUserAgent, 15*time.Second) } func (c *Client) newRequest(ctx context.Context, method, queryPath string, body io.Reader) (*http.Request, error) { diff --git a/pkg/goplay/client_test.go b/pkg/goplay/client_test.go index 28375cbf..c7bb7ebe 100644 --- a/pkg/goplay/client_test.go +++ b/pkg/goplay/client_test.go @@ -8,6 +8,6 @@ import ( func TestNewDefaultClient(t *testing.T) { c := NewDefaultClient() - require.Equal(t, c.baseUrl, playgroundUrl) - require.Equal(t, c.userAgent, defaultUserAgent) + require.Equal(t, c.baseUrl, DefaultPlaygroundURL) + require.Equal(t, c.userAgent, DefaultUserAgent) } diff --git a/pkg/langserver/server.go b/pkg/langserver/server.go index cdd96372..efa62dff 100644 --- a/pkg/langserver/server.go +++ b/pkg/langserver/server.go @@ -39,11 +39,11 @@ type Service struct { } // New is Service constructor -func New(version string, packages []*analyzer.Package, builder compiler.BuildService) *Service { +func New(version string, playground *goplay.Client, packages []*analyzer.Package, builder compiler.BuildService) *Service { return &Service{ compiler: builder, version: version, - playground: goplay.NewDefaultClient(), + playground: playground, log: zap.S().Named("langserver"), index: analyzer.BuildPackageIndex(packages), limiter: rate.NewLimiter(rate.Every(frameTime), compileRequestsPerFrame), From 02e16be04384ec93c1645dd17c44cb857e6a04dc Mon Sep 17 00:00:00 2001 From: x1unix Date: Tue, 18 Aug 2020 20:14:36 +0300 Subject: [PATCH 2/3] allow custom playground URL --- build/Dockerfile | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/build/Dockerfile b/build/Dockerfile index 01385b2b..ab2458ae 100644 --- a/build/Dockerfile +++ b/build/Dockerfile @@ -24,10 +24,12 @@ 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 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/wasm_exec.js ./public EXPOSE 8000 -ENTRYPOINT /opt/playground/server -f=/opt/playground/data/packages.json -addr=:8000 -clean-interval=${APP_CLEAN_INTERVAL} -debug=${APP_DEBUG} \ No newline at end of file +ENTRYPOINT /opt/playground/server -f=/opt/playground/data/packages.json -addr=:8000\ + -clean-interval=${APP_CLEAN_INTERVAL} -debug=${APP_DEBUG} -playground-url=${APP_PLAYGROUND_URL} \ No newline at end of file From 8a023fef2f3bd65b64733bd3f8e326701cf1f41a Mon Sep 17 00:00:00 2001 From: x1unix Date: Tue, 18 Aug 2020 20:15:21 +0300 Subject: [PATCH 3/3] pass custom playground URL in Dockerfile --- build/Dockerfile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/build/Dockerfile b/build/Dockerfile index ab2458ae..645ae980 100644 --- a/build/Dockerfile +++ b/build/Dockerfile @@ -31,5 +31,5 @@ COPY --from=build /tmp/playground/server . COPY --from=build /tmp/playground/worker.wasm ./public COPY --from=build /tmp/playground/wasm_exec.js ./public EXPOSE 8000 -ENTRYPOINT /opt/playground/server -f=/opt/playground/data/packages.json -addr=:8000\ +ENTRYPOINT /opt/playground/server -f=/opt/playground/data/packages.json -addr=:8000 \ -clean-interval=${APP_CLEAN_INTERVAL} -debug=${APP_DEBUG} -playground-url=${APP_PLAYGROUND_URL} \ No newline at end of file