Skip to content

Commit

Permalink
Merge pull request #109 from onflow/bugfix/server
Browse files Browse the repository at this point in the history
Server Stopping Logic
  • Loading branch information
sideninja authored Mar 25, 2022
2 parents 8bb79d2 + 0669338 commit f2ac992
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 7 deletions.
6 changes: 1 addition & 5 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,5 @@ func main() {

fmt.Println("Server started on port 8701, make sure you have the latest version of bundle.zip build before starting this server")

err = srv.Start()
if err != nil {
panic(err)
}

srv.Start()
}
19 changes: 17 additions & 2 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@ import (
"encoding/json"
"fmt"
"net/http"
"os"
"os/signal"
"strings"
"syscall"
)

//go:embed bundle.zip
Expand Down Expand Up @@ -76,8 +79,20 @@ func devWalletHandler() func(writer http.ResponseWriter, request *http.Request)
}
}

func (s *server) Start() error {
return s.http.ListenAndServe()
func (s *server) Start() {
done := make(chan os.Signal, 1)
signal.Notify(done, os.Interrupt, syscall.SIGINT, syscall.SIGTERM)

go func() {
err := s.http.ListenAndServe()
if err != nil {
fmt.Printf("error starting up the server: %s\n", err)
done <- syscall.SIGTERM
}
}()

<-done
s.Stop()
}

func (s *server) Stop() {
Expand Down

0 comments on commit f2ac992

Please sign in to comment.