handler: fix exit

monorepo
Varun Patil 2023-10-21 11:45:30 -07:00
parent ea9e620de3
commit 4e2b1ddb18
2 changed files with 19 additions and 9 deletions

View File

@ -1,6 +1,7 @@
package go_vod package go_vod
import ( import (
"context"
"encoding/json" "encoding/json"
"io/ioutil" "io/ioutil"
"log" "log"
@ -8,6 +9,7 @@ import (
"os" "os"
"strings" "strings"
"sync" "sync"
"time"
) )
type Handler struct { type Handler struct {
@ -201,13 +203,16 @@ func (h *Handler) removeManager(streamid string) {
delete(h.managers, streamid) delete(h.managers, streamid)
} }
func (h *Handler) Start() { func (h *Handler) Start() int {
log.Println("Starting go-vod " + h.c.Version + " on " + h.c.Bind)
h.server = &http.Server{Addr: h.c.Bind, Handler: h} h.server = &http.Server{Addr: h.c.Bind, Handler: h}
go func() { go func() {
err := h.server.ListenAndServe() err := h.server.ListenAndServe()
if err != nil { if err == http.ErrServerClosed {
log.Fatal("Error starting server", err) log.Println("HTTP server closed")
} else if err != nil {
log.Fatal("Error starting server: ", err)
} }
}() }()
@ -220,11 +225,13 @@ func (h *Handler) Start() {
} }
// Stop server // Stop server
log.Println("Exiting VOD server") log.Println("Shutting down HTTP server")
h.server.Close() ctx, cancel := context.WithDeadline(context.TODO(), time.Now().Add(5*time.Second))
defer cancel()
h.server.Shutdown(ctx)
// Exit with correct status code // Return status code
os.Exit(h.exitCode) return h.exitCode
} }
func (h *Handler) Close() { func (h *Handler) Close() {

View File

@ -40,6 +40,9 @@ func main() {
c.AutoDetect() c.AutoDetect()
// Start server // Start server
log.Println("Starting go-vod " + VERSION + " on " + c.Bind) code := go_vod.NewHandler(c).Start()
go_vod.NewHandler(c).Start()
// Exit
log.Println("Exiting go-vod with status code", code)
os.Exit(code)
} }