Skip to content

Commit

Permalink
Added webservice try_ports, if :10000 is not available
Browse files Browse the repository at this point in the history
  • Loading branch information
adrianrudnik committed Oct 30, 2023
1 parent e409f8d commit ba5e832
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 11 deletions.
2 changes: 1 addition & 1 deletion service/FyneApp.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ Website = "https://www.ablegram.app"
Name = "Ablegram"
ID = "com.ablegram.app"
Version = "1.0.0"
Build = 6
Build = 7
4 changes: 4 additions & 0 deletions service/internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ func newConfig() *Config {
SearchablePaths: make([]string, 0, 100),
ExcludeSystemFolders: true,
},

Webservice: WebserviceConfig{
TryPorts: []int{10000, 20000, 30000, 40000, 50000, 10001},
},
}
}

Expand Down
12 changes: 9 additions & 3 deletions service/internal/config/types.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package config

type Config struct {
Log LogConfig `yaml:"log"`
Behaviour BehaviourConfig `yaml:"behaviour"`
Collector CollectorConfig `yaml:"collector"`
Log LogConfig `yaml:"log"`
Behaviour BehaviourConfig `yaml:"behaviour"`
Collector CollectorConfig `yaml:"collector"`
Webservice WebserviceConfig `yaml:"webservice"`
}

type LogConfig struct {
Expand All @@ -22,3 +23,8 @@ type CollectorConfig struct {
SearchablePaths []string `yaml:"searchable_paths"`
ExcludeSystemFolders bool `yaml:"exclude_system_folders"`
}

type WebserviceConfig struct {
TryPorts []int `yaml:"try_ports"`
ChosenPort int `yaml:"-"`
}
2 changes: 0 additions & 2 deletions service/internal/indexer/worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,6 @@ func (p *Worker) doWork(progress *stats.ProcessProgress, m *stats.Metrics) {
continue
}

log.Debug().Str("document", msg.Id).Msg("Document indexed")

docCount, err := p.search.Index.DocCount()
if err != nil {
log.Warn().Err(err).Msg("Failed to retrieve document count from index")
Expand Down
6 changes: 4 additions & 2 deletions service/internal/ui/opener.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package ui

import (
"fmt"
"github.com/adrianrudnik/ablegram/internal/config"
"github.com/icza/gox/osx"
)

func OpenFrontend() {
err := osx.OpenDefault("http://localhost:10000")
func OpenFrontend(c *config.Config) {
err := osx.OpenDefault(fmt.Sprintf("http://localhost:%d", c.Webservice.ChosenPort))
if err != nil {
Logger.Warn().Err(err).Msg("Could not open default browser")
}
Expand Down
16 changes: 13 additions & 3 deletions service/main.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"fmt"
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/app"
"fyne.io/fyne/v2/canvas"
Expand All @@ -21,6 +22,7 @@ import (
"github.com/rs/zerolog/log"
"image/color"
"os"
"strings"
"time"
)

Expand Down Expand Up @@ -109,10 +111,18 @@ func main() {
}

time.Sleep(50 * time.Millisecond)
ui.OpenFrontend()
ui.OpenFrontend(appConfig)
}()

webservice.Serve(search, appPusher, ":10000")
for _, port := range appConfig.Webservice.TryPorts {
appConfig.Webservice.ChosenPort = port
err := webservice.Serve(search, appPusher, fmt.Sprintf(":%d", port))
if err != nil && strings.Contains(err.Error(), "bind: permission denied") {
log.Warn().Err(err).Int("port", port).Msg("Could not start webservice, trying other port")
continue
}
break
}
}()

if !appConfig.Behaviour.ShowGui {
Expand All @@ -131,7 +141,7 @@ func main() {

statusTxt := canvas.NewText("The service is processing files...", color.White)
quitBtn := widget.NewButton("Shut down service", func() { a.Quit() })
startBtn := widget.NewButton("Open results in browser", func() { ui.OpenFrontend() })
startBtn := widget.NewButton("Open results in browser", func() { ui.OpenFrontend(appConfig) })
progressBar := widget.NewProgressBarInfinite()

uiUpdater := ui.NewUiUpdater(statusTxt, progressBar)
Expand Down

0 comments on commit ba5e832

Please sign in to comment.