-
Notifications
You must be signed in to change notification settings - Fork 111
Expand file tree
/
Copy pathmain.go
More file actions
79 lines (67 loc) · 2.05 KB
/
main.go
File metadata and controls
79 lines (67 loc) · 2.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package main
import (
"context"
"fmt"
nethttp "net/http"
"os"
"os/signal"
"syscall"
"time"
"github.com/getAlby/hub/http"
"github.com/getAlby/hub/logger"
"github.com/getAlby/hub/service"
"github.com/labstack/echo/v4"
log "github.com/sirupsen/logrus"
)
func main() {
log.Info("AlbyHub Starting in HTTP mode")
// Create a channel to receive OS signals.
osSignalChannel := make(chan os.Signal, 1)
// Notify the channel on os.Interrupt, syscall.SIGTERM. os.Kill cannot be caught.
signal.Notify(osSignalChannel, os.Interrupt, syscall.SIGTERM, syscall.SIGPIPE)
ctx, cancel := context.WithCancel(context.Background())
var signal os.Signal
go func() {
for {
// wait for exit signal
signal = <-osSignalChannel
logger.Logger.WithField("signal", signal).Info("Received OS signal")
if signal == syscall.SIGPIPE {
logger.Logger.WithField("signal", signal).Warn("Ignoring SIGPIPE signal")
continue
}
cancel()
break
}
}()
svc, err := service.NewService(ctx)
if err != nil {
log.WithError(err).Fatal("Failed to create service")
return
}
e := echo.New()
//register shared routes
httpSvc := http.NewHttpService(svc, svc.GetEventPublisher())
httpSvc.RegisterSharedRoutes(e)
//start Echo server
go func() {
if err := e.Start(fmt.Sprintf(":%v", svc.GetConfig().GetEnv().Port)); err != nil && err != nethttp.ErrServerClosed {
logger.Logger.WithError(err).Error("echo server failed to start")
cancel()
}
}()
//handle graceful shutdown
<-ctx.Done()
logger.Logger.WithField("signal", signal).Info("Context Done")
logger.Logger.Info("Shutting down echo server...")
ctx, cancel = context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
err = e.Shutdown(ctx)
if err != nil {
logger.Logger.WithError(err).Error("Failed to shutdown echo server")
}
logger.Logger.Info("Echo server exited")
svc.Shutdown()
logger.Logger.Info("Service exited")
logger.Logger.Info("Alby Hub needs to stay online to send and receive transactions. Channels may be closed if your hub stays offline for an extended period of time.")
}