Skip to content

Commit

Permalink
refactor: use http server to serve and graceful shutdown
Browse files Browse the repository at this point in the history
  • Loading branch information
andy89923 committed May 9, 2024
1 parent a4831ec commit ed39397
Showing 1 changed file with 31 additions and 6 deletions.
37 changes: 31 additions & 6 deletions backend/webui_service/webui_init.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
package webui_service

import (
"context"
"io"
"net/http"
"os"
"os/signal"
"runtime/debug"
"sync"
"syscall"
"time"

"github.com/gin-contrib/cors"
"github.com/sirupsen/logrus"
Expand All @@ -23,8 +26,8 @@ type WebuiApp struct {
cfg *factory.Config
webuiCtx *webui_context.WEBUIContext

wg *sync.WaitGroup

wg *sync.WaitGroup
server *http.Server
billingServer *billing.BillingDomain
}

Expand Down Expand Up @@ -155,23 +158,45 @@ func (a *WebuiApp) Start(tlsKeyLogPath string) {

router.NoRoute(ReturnPublic())

var addr string
if webServer != nil {
logger.InitLog.Infoln(router.Run(webServer.IP + ":" + webServer.PORT))
addr = webServer.IP + ":" + webServer.PORT
} else {
logger.InitLog.Infoln(router.Run(":5000"))
addr = ":5000"
}

a.server = &http.Server{
Addr: addr,
Handler: router,
}
go func() {
logger.MainLog.Infof("Http server listening on %+v", addr)
if err := a.server.ListenAndServe(); err != nil && err != http.ErrServerClosed {
logger.MainLog.Fatalf("listen: %s\n", err)
}
}()

logger.MainLog.Infoln("wait all routine stopped")
a.wg.Wait()
}

func (a *WebuiApp) Terminate() {
logger.InitLog.Infoln("Terminating WebUI-AF...")
logger.MainLog.Infoln("Terminating WebUI-AF...")

if a.billingServer != nil {
a.billingServer.Stop()
}

if a.server != nil {
logger.MainLog.Infoln("stopping HTTP server")

ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
defer cancel()
if err := a.server.Shutdown(ctx); err != nil {
logger.MainLog.Fatal("HTTP server forced to shutdown: ", err)
}
}

// Deregister with NRF
if a.webuiCtx.IsRegistered {
problemDetails, err := webui_context.SendDeregisterNFInstance()
Expand All @@ -184,5 +209,5 @@ func (a *WebuiApp) Terminate() {
}
}

logger.InitLog.Infoln("WebUI-AF Terminated")
logger.MainLog.Infoln("WebUI-AF Terminated")
}

0 comments on commit ed39397

Please sign in to comment.