Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 21 additions & 13 deletions internal/glance/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ func serveApp(configPath string) error {
// TODO: refactor if this gets any more complex, the current implementation is
// difficult to reason about due to all of the callbacks and simultaneous operations,
// use a single goroutine and a channel to initiate synchronous changes to the server
exitChannel := make(chan struct{})
exitChannel := make(chan error, 1)
hadValidConfigOnStartup := false
var stopServer func() error

Expand All @@ -108,7 +108,7 @@ func serveApp(configPath string) error {
log.Printf("Config has errors: %v", err)

if !hadValidConfigOnStartup {
close(exitChannel)
reportExitError(exitChannel, fmt.Errorf("validating config file: %w", err))
}

return
Expand All @@ -119,7 +119,7 @@ func serveApp(configPath string) error {
log.Printf("Failed to create application: %v", err)

if !hadValidConfigOnStartup {
close(exitChannel)
reportExitError(exitChannel, fmt.Errorf("creating application: %w", err))
}

return
Expand All @@ -135,14 +135,9 @@ func serveApp(configPath string) error {
}
}

go func() {
var startServer func() error
startServer, stopServer = app.server()

if err := startServer(); err != nil {
log.Printf("Failed to start server: %v", err)
}
}()
var startServer func() error
startServer, stopServer = app.server()
go startServerAndReport(startServer, exitChannel)
}

onErr := func(err error) {
Expand Down Expand Up @@ -176,8 +171,21 @@ func serveApp(configPath string) error {
}
}

<-exitChannel
return nil
return <-exitChannel
}

func startServerAndReport(startServer func() error, exitChannel chan<- error) {
if err := startServer(); err != nil {
log.Printf("Failed to start server: %v", err)
reportExitError(exitChannel, fmt.Errorf("starting server: %w", err))
}
}

func reportExitError(exitChannel chan<- error, err error) {
select {
case exitChannel <- err:
default:
}
}

func serveUpdateNoticeIfConfigLocationNotMigrated(configPath string) bool {
Expand Down
20 changes: 20 additions & 0 deletions internal/glance/main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package glance

import (
"errors"
"testing"
)

func TestStartServerAndReportReturnsStartupError(t *testing.T) {
startErr := errors.New("address already in use")
exitChannel := make(chan error, 1)

startServerAndReport(func() error {
return startErr
}, exitChannel)

err := <-exitChannel
if !errors.Is(err, startErr) {
t.Fatalf("expected startup error to be reported, got %v", err)
}
}