-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
63 lines (55 loc) · 1.32 KB
/
main.go
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
package main
import (
"flag"
"fmt"
"log"
"net"
"net/http"
"os"
"os/signal"
"syscall"
)
func main() {
runWorker()
}
func runWorker() {
// Define a command-line flag for specifying the socket path.
socketPathPtr := flag.String("socket-path", "", "Unix domain socket path")
flag.Parse()
if *socketPathPtr != "" {
// Create a Unix domain socket and listen for incoming connections.
socket, err := net.Listen("unix", *socketPathPtr)
if err != nil {
panic(err)
}
// Cleanup the sockfile.
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt, syscall.SIGTERM)
go func() {
<-c
fmt.Println("Cleaning up socket file...")
if _, err := os.Stat(*socketPathPtr); err == nil {
os.Remove(*socketPathPtr)
}
os.Exit(1)
}()
handler := http.NewServeMux()
handler.HandleFunc("/execute", handleRequest)
handler.HandleFunc("/kill", handleKill)
handler.HandleFunc("/health", handleHealthCheck)
server := http.Server{
Handler: handler,
}
fmt.Println("Worker listening on socket ", *socketPathPtr)
err = server.Serve(socket)
if err != nil {
panic(err)
}
} else {
http.HandleFunc("/execute", handleRequest)
http.HandleFunc("/kill", handleKill)
http.HandleFunc("/health", handleHealthCheck)
log.Println("Listening on :8080...")
log.Fatal(http.ListenAndServe(":8080", nil))
}
}