Skip to content

Commit

Permalink
Startupfix (#99)
Browse files Browse the repository at this point in the history
Fix app startup.
  • Loading branch information
lexbritvin authored Jan 21, 2025
1 parent 907a30b commit 74e8870
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 11 deletions.
2 changes: 2 additions & 0 deletions plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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),
}
Expand Down
26 changes: 15 additions & 11 deletions web_runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 74e8870

Please sign in to comment.