WIP: context for clean shutdown
This commit is contained in:
parent
984f744548
commit
43bab58f0a
|
@ -1,8 +1,12 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"os"
|
"os"
|
||||||
|
"os/signal"
|
||||||
|
"syscall"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/h44z/wg-portal/internal/server"
|
"github.com/h44z/wg-portal/internal/server"
|
||||||
"github.com/sirupsen/logrus"
|
"github.com/sirupsen/logrus"
|
||||||
|
@ -11,16 +15,38 @@ import (
|
||||||
func main() {
|
func main() {
|
||||||
_ = setupLogger(logrus.StandardLogger())
|
_ = setupLogger(logrus.StandardLogger())
|
||||||
|
|
||||||
|
c := make(chan os.Signal, 1)
|
||||||
|
signal.Notify(c, syscall.SIGINT, syscall.SIGTERM, syscall.SIGHUP)
|
||||||
|
|
||||||
logrus.Infof("Starting WireGuard Portal Server...")
|
logrus.Infof("Starting WireGuard Portal Server...")
|
||||||
|
|
||||||
|
// Context for clean shutdown
|
||||||
|
ctx, cancel := context.WithCancel(context.Background())
|
||||||
|
defer cancel()
|
||||||
|
|
||||||
service := server.Server{}
|
service := server.Server{}
|
||||||
if err := service.Setup(); err != nil {
|
if err := service.Setup(ctx); err != nil {
|
||||||
logrus.Fatalf("Setup failed: %v", err)
|
logrus.Fatalf("Setup failed: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
service.Run()
|
// Attach signal handlers to context
|
||||||
|
go func() {
|
||||||
|
osCall := <-c
|
||||||
|
logrus.Tracef("received system call: %v", osCall)
|
||||||
|
cancel() // cancel the context
|
||||||
|
}()
|
||||||
|
|
||||||
|
// Start main process in background
|
||||||
|
go service.Run()
|
||||||
|
|
||||||
|
<-ctx.Done() // Wait until the context gets canceled
|
||||||
|
|
||||||
|
// Give goroutines some time to stop gracefully
|
||||||
|
logrus.Info("Stopping WireGuard Portal Server...")
|
||||||
|
time.Sleep(2 * time.Second)
|
||||||
|
|
||||||
logrus.Infof("Stopped WireGuard Portal Server...")
|
logrus.Infof("Stopped WireGuard Portal Server...")
|
||||||
|
logrus.Exit(0)
|
||||||
}
|
}
|
||||||
|
|
||||||
func setupLogger(logger *logrus.Logger) error {
|
func setupLogger(logger *logrus.Logger) error {
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
package server
|
package server
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
"encoding/gob"
|
"encoding/gob"
|
||||||
"errors"
|
"errors"
|
||||||
"html/template"
|
"html/template"
|
||||||
|
@ -64,6 +65,7 @@ type StaticData struct {
|
||||||
|
|
||||||
type Server struct {
|
type Server struct {
|
||||||
// Core components
|
// Core components
|
||||||
|
ctx context.Context
|
||||||
config *common.Config
|
config *common.Config
|
||||||
server *gin.Engine
|
server *gin.Engine
|
||||||
users *UserManager
|
users *UserManager
|
||||||
|
@ -79,7 +81,7 @@ type Server struct {
|
||||||
ldapCacheUpdater *ldap.UserCache
|
ldapCacheUpdater *ldap.UserCache
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Server) Setup() error {
|
func (s *Server) Setup(ctx context.Context) error {
|
||||||
dir := s.getExecutableDirectory()
|
dir := s.getExecutableDirectory()
|
||||||
rDir, _ := filepath.Abs(filepath.Dir(os.Args[0]))
|
rDir, _ := filepath.Abs(filepath.Dir(os.Args[0]))
|
||||||
logrus.Infof("Real working directory: %s", rDir)
|
logrus.Infof("Real working directory: %s", rDir)
|
||||||
|
|
Loading…
Reference in New Issue