Skip to content
Snippets Groups Projects
Commit 8dacb768 authored by Wolfgang Welz's avatar Wolfgang Welz
Browse files

:loud_sound: Add error handling to analysis http server"

parent fc01c115
No related branches found
No related tags found
No related merge requests found
package httpserver
import (
"errors"
"net/http"
"sync"
"time"
"github.com/iotaledger/goshimmer/packages/shutdown"
"github.com/iotaledger/hive.go/daemon"
"github.com/iotaledger/hive.go/node"
"github.com/iotaledger/hive.go/logger"
"golang.org/x/net/context"
"golang.org/x/net/websocket"
)
var (
log *logger.Logger
httpServer *http.Server
router *http.ServeMux
)
func Configure(plugin *node.Plugin) {
const name = "Analysis HTTP Server"
func Configure() {
log = logger.NewLogger(name)
router = http.NewServeMux()
httpServer = &http.Server{Addr: ":80", Handler: router}
......@@ -24,12 +31,30 @@ func Configure(plugin *node.Plugin) {
router.HandleFunc("/", index)
}
func Run(plugin *node.Plugin) {
daemon.BackgroundWorker("Analysis HTTP Server", func(shutdownSignal <-chan struct{}) {
go httpServer.ListenAndServe()
<-shutdownSignal
ctx, cancel := context.WithTimeout(context.Background(), 0*time.Second)
defer cancel()
httpServer.Shutdown(ctx)
}, shutdown.ShutdownPriorityAnalysis)
func Run() {
if err := daemon.BackgroundWorker(name, start, shutdown.ShutdownPriorityAnalysis); err != nil {
log.Errorf("Error starting as daemon: %s", err)
}
}
func start(shutdownSignal <-chan struct{}) {
var wg sync.WaitGroup
wg.Add(1)
go func() {
defer wg.Done()
log.Infof(name+" started: address=%s", httpServer.Addr)
if err := httpServer.ListenAndServe(); err != nil && !errors.Is(err, http.ErrServerClosed) {
log.Warnf("Error listening: %s", err)
}
}()
<-shutdownSignal
log.Info("Stopping " + name + " ...")
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
if err := httpServer.Shutdown(ctx); err != nil {
log.Errorf("Error closing: %s", err)
}
wg.Wait()
log.Info("Stopping " + name + " ... done")
}
......@@ -7,10 +7,10 @@ import (
)
func Configure(plugin *node.Plugin) {
httpserver.Configure(plugin)
httpserver.Configure()
recordedevents.Configure(plugin)
}
func Run(plugin *node.Plugin) {
httpserver.Run(plugin)
httpserver.Run()
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment