Skip to content

Commit

Permalink
Merge pull request #2 from ventsip/enhancement/server-startup
Browse files Browse the repository at this point in the history
feat: Update server.Serve to signal when the server is ready to recei…
  • Loading branch information
ventsip authored Aug 2, 2024
2 parents 80dac2d + 4fee717 commit cd0a385
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 8 deletions.
2 changes: 1 addition & 1 deletion cmd/cli/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func main() {
wg.Add(1)
go ph.Run(ctx, &wg)
wg.Add(1)
go server.Serve(ctx, &wg, ph, version)
go server.Serve(ctx, &wg, ph, version, make(chan<- struct{}))
wg.Wait()

if err := ph.SaveBalance(); err != nil {
Expand Down
4 changes: 2 additions & 2 deletions cmd/winsvc/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ func (m *myservice) Execute(args []string, r <-chan svc.ChangeRequest, changes c
wg.Add(1)
go ph.Run(ctx, &wg)
wg.Add(1)
go server.Serve(ctx, &wg, ph, version)
go server.Serve(ctx, &wg, ph, version, make(chan<- struct{}))

changes <- svc.Status{State: svc.Running, Accepts: cmdsAccepted}

Expand Down Expand Up @@ -117,7 +117,7 @@ func (m *myservice) Execute(args []string, r <-chan svc.ChangeRequest, changes c
wg.Add(1)
go ph.Run(ctx, &wg)
wg.Add(1)
go server.Serve(ctx, &wg, ph, version)
go server.Serve(ctx, &wg, ph, version, make(chan<- struct{}))
changes <- svc.Status{State: svc.Running, Accepts: cmdsAccepted}

default:
Expand Down
12 changes: 9 additions & 3 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,8 @@ var webFolder embed.FS
// this is needed, since go:embed does not support embedding files without the directory name
var webFS, _ = fs.Sub(webFolder, "webFiles")

// Serve serves web interface for ph
func Serve(ctx context.Context, wg *sync.WaitGroup, ph *engine.ProcessHunter, ver string) {
// Serve serves web interface for ph and signals a channel when the server is ready to receive client connections
func Serve(ctx context.Context, wg *sync.WaitGroup, ph *engine.ProcessHunter, ver string, ready chan<- struct{}) {
defer func() {
if wg != nil {
wg.Done()
Expand All @@ -129,7 +129,13 @@ func Serve(ctx context.Context, wg *sync.WaitGroup, ph *engine.ProcessHunter, ve
s := http.Server{Addr: port, Handler: mux}

log.Println("starting service")
go s.ListenAndServe()
go func() {
if err := s.ListenAndServe(); err != nil && err != http.ErrServerClosed {
log.Fatalf("Web service listen error: %s", err)
}
}()

ready <- struct{}{} // signal that the server is ready

<-ctx.Done()

Expand Down
8 changes: 6 additions & 2 deletions server/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,9 @@ func TestGetConfig(t *testing.T) {
wg.Add(1)
go ph.Run(ctx, &wg)
wg.Add(1)
go Serve(ctx, &wg, ph, "test")
ready := make(chan struct{})
go Serve(ctx, &wg, ph, "test", ready)
<-ready

r, err := http.Get("http://localhost:8080/config")
if err != nil {
Expand Down Expand Up @@ -210,7 +212,9 @@ func quickTestGetJSON(t *testing.T, url string, ctype string) {
wg.Add(1)
go ph.Run(ctx, &wg)
wg.Add(1)
go Serve(ctx, &wg, ph, "test")
ready := make(chan struct{})
go Serve(ctx, &wg, ph, "test", ready)
<-ready

r, err := http.Get(url)
if err != nil {
Expand Down

0 comments on commit cd0a385

Please sign in to comment.