From 74e887004e665318e245fe48472f49c0211437e3 Mon Sep 17 00:00:00 2001 From: Aleksandr Britvin Date: Tue, 21 Jan 2025 17:03:44 +0100 Subject: [PATCH] Startupfix (#99) Fix app startup. --- plugin.go | 2 ++ web_runner.go | 26 +++++++++++++++----------- 2 files changed, 17 insertions(+), 11 deletions(-) diff --git a/plugin.go b/plugin.go index 9e0efd9..979612b 100644 --- a/plugin.go +++ b/plugin.go @@ -48,6 +48,7 @@ func (p *Plugin) OnAppInit(app launchr.App) error { type webFlags struct { Port int + IsPortSet bool ProxyClient string UseSwaggerUI bool PluginDir string @@ -63,6 +64,7 @@ func (p *Plugin) DiscoverActions(_ context.Context) ([]*action.Action, error) { webRunFlags := webFlags{ PluginDir: pluginTmpDir, Port: input.Opt("port").(int), + IsPortSet: input.IsOptChanged("port"), UseSwaggerUI: input.Opt("swagger-ui").(bool), ProxyClient: input.Opt("proxy-client").(string), } diff --git a/web_runner.go b/web_runner.go index dd49f00..a1a1573 100644 --- a/web_runner.go +++ b/web_runner.go @@ -34,7 +34,9 @@ func (p *Plugin) runWeb(ctx context.Context, webOpts webFlags) error { port := webOpts.Port if !isAvailablePort(port) { - launchr.Term().Warning().Printfln("The port %d you are trying to use for the web server is not available.", port) + if webOpts.IsPortSet { + return fmt.Errorf("requested port %d is not available", port) + } port, err = getAvailablePort(port) if err != nil { return err @@ -138,10 +140,10 @@ func stopWeb(pidFile, pluginDir string) (err error) { return nil } - if checkHealth(serverRunInfo.URL) { + if err = checkHealth(serverRunInfo.URL); err == nil { return fmt.Errorf("the web UI is currently running at %s\nPlease stop it through the user interface or terminate the process", serverRunInfo.URL) } - + cleanupPluginTemp(pluginDir) launchr.Term().Success().Println(onSuccess) return nil } @@ -223,14 +225,16 @@ func cleanupPluginTemp(dir string) { } // checkHealth helper to check if server is available by request. -func checkHealth(url string) bool { +func checkHealth(url string) error { resp, err := http.Head(url) //nolint G107 // @todo URL may come from user input, potential vulnerability. if err != nil { - // Error is thrown on an incorrect url. - panic(err) + return err } _ = resp.Body.Close() - return resp.StatusCode == http.StatusOK + if resp.StatusCode == http.StatusOK { + return nil + } + return fmt.Errorf("bad response code %d", resp.StatusCode) } // getServerInfo lookups server run info metadata and tries to get it from storage. @@ -278,8 +282,8 @@ func getExistingWeb(pidFile string, pluginDir string) (string, error) { return serverRunInfo.URL, nil } - if !checkHealth(serverRunInfo.URL) { - return serverRunInfo.URL, errors.New("web: unhealthy response") + if err = checkHealth(serverRunInfo.URL); err != nil { + return serverRunInfo.URL, fmt.Errorf("web unhealthy response: %w", err) } return serverRunInfo.URL, nil @@ -320,10 +324,10 @@ func isAvailablePort(port int) bool { func openInBrowserWhenReady(url string) error { // Wait until the service is healthy. retries := 0 - for !checkHealth(url) { + for err := checkHealth(url); err != nil; { time.Sleep(time.Second) if retries == 10 { - return errors.New("the service is unhealthy") + return fmt.Errorf("web is unhealthy: %w", err) } retries++ if retries == 3 {