adjusted main to use one single context

This commit is contained in:
0xdcarns
2023-02-24 15:37:53 -05:00
parent 0335e258ad
commit 92d0d12e8f
5 changed files with 56 additions and 105 deletions

View File

@@ -4,11 +4,8 @@ import (
"context"
"fmt"
"net/http"
"os"
"os/signal"
"strings"
"sync"
"syscall"
"time"
"github.com/gorilla/handlers"
@@ -33,7 +30,7 @@ var HttpHandlers = []interface{}{
}
// HandleRESTRequests - handles the rest requests
func HandleRESTRequests(wg *sync.WaitGroup) {
func HandleRESTRequests(wg *sync.WaitGroup, ctx context.Context) {
defer wg.Done()
r := mux.NewRouter()
@@ -59,18 +56,14 @@ func HandleRESTRequests(wg *sync.WaitGroup) {
}()
logger.Log(0, "REST Server successfully started on port ", port, " (REST)")
// Relay os.Interrupt to our channel (os.Interrupt = CTRL+C)
// Ignore other incoming signals
ctx, stop := signal.NotifyContext(context.TODO(), syscall.SIGTERM, os.Interrupt)
defer stop()
// Block main routine until a signal is received
// As long as user doesn't press CTRL+C a message is not passed and our main routine keeps running
<-ctx.Done()
// After receiving CTRL+C Properly stop the server
logger.Log(0, "Stopping the REST server...")
if err := srv.Shutdown(context.TODO()); err != nil {
logger.Log(0, "REST shutdown error occurred -", err.Error())
}
logger.Log(0, "REST Server closed.")
logger.DumpFile(fmt.Sprintf("data/netmaker.log.%s", time.Now().Format(logger.TimeFormatDay)))
srv.Shutdown(context.TODO())
}