Skip to content

Commit

Permalink
fix: ensure the socket is closed
Browse files Browse the repository at this point in the history
  • Loading branch information
ySnoopyDogy committed May 11, 2024
1 parent de86f58 commit c1b3366
Showing 1 changed file with 13 additions and 3 deletions.
16 changes: 13 additions & 3 deletions src/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ import (
"net"
"net/http"
"os"
"os/signal"
"strconv"
"syscall"
"time"

"github.com/MenheraBot/MenheraVanGOgh/src/controllers"
Expand Down Expand Up @@ -69,13 +71,21 @@ func main() {

if socketPath != "" {
go func() {
os.Remove(socketPath)
socket, err := net.Listen("unix", socketPath)

if err != nil {
log.Println("Error to setup unix socket")
log.Panic(err)
log.Println("Error to setup unix socket", err)
} else {
defer socket.Close()
sigc := make(chan os.Signal, 1)
signal.Notify(sigc, os.Interrupt, syscall.SIGTERM)

go func(c chan os.Signal) {
sig := <-c
log.Printf("Caught signal %s: Closing socket.", sig)
socket.Close()
os.Exit(0)
}(sigc)

log.Printf("Running unix socket on %s\n", socketPath)
http.Serve(socket, router.Handler())
Expand Down

0 comments on commit c1b3366

Please sign in to comment.