-
Notifications
You must be signed in to change notification settings - Fork 2
/
app.go
103 lines (86 loc) · 2.33 KB
/
app.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
// This file has been created by "go generate" as initial code. go generate will never update it, EXCEPT if you remove it.
// So, update it for your need.
package main
import (
"gopkg.in/alecthomas/kingpin.v2"
"log"
"net"
"net/http"
"os"
"os/signal"
"path"
"syscall"
"time"
)
func (a *BitbucketApp) start_server() {
Start := true // Move server to up status
server_chan := make(chan bool, 1)
for {
a.server_set()
int_sig := make(chan os.Signal, 1)
signal.Notify(int_sig, syscall.SIGINT, syscall.SIGTERM)
ln, err := net.Listen("unix", a.socket)
if err != nil {
log.Fatal("listen error:", err)
}
// Interruption handler.
go func() {
log.Print("Interruption handler started.")
if _, ok := <-int_sig; !ok {
log.Print("Exiting interruption handler...")
signal.Stop(int_sig)
return // Interruption handler aborted.
}
log.Print("Exiting and closing socket...")
Start = false // Move server to down status
ln.Close()
}()
//ln, err := net.Listen("tcp", ":8081")
go a.listen_and_serve(ln, server_chan, &Start)
time.Sleep(2 * time.Millisecond)
for {
_, err = os.Stat(a.socket)
if err != nil {
break
}
time.Sleep(5 * time.Millisecond)
}
if Start {
log.Printf("Issue with the socket. %s. Closing it.", err)
ln.Close()
close(int_sig)
}
<-server_chan
log.Print("http server is NOW off.")
if !Start {
os.Exit(0)
}
log.Print("Restarting the http server.")
time.Sleep(2)
}
}
func (a *BitbucketApp) listen_and_serve(ln net.Listener, server_chan chan bool, Start *bool) {
log.Printf("httpd server: Starting service on socket '%s'", a.socket)
router := NewRouter()
srv := http.Server{Handler: router}
err := srv.Serve(ln)
if !*Start {
log.Print("httpd server: Exiting...")
} else {
log.Printf("httpd server: Error detected: %s", err)
}
server_chan <- *Start
}
func (a *BitbucketApp) server_set() {
if _, err := os.Stat(*a.params.socket_path); err != nil {
if os.IsNotExist(err) {
if err = os.MkdirAll(*a.params.socket_path, 0755) ; err != nil {
kingpin.FatalIfError(err, "Unable to create '%s'\n", *a.params.socket_path)
}
log.Printf("Path %s created.", *a.params.socket_path)
} else {
kingpin.FatalIfError(err, "Unable to create '%s'\n", *a.params.socket_path)
}
}
a.socket = path.Join(*a.params.socket_path, *a.params.socket_file)
}