-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
71 lines (54 loc) · 1.34 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
64
65
66
67
68
69
70
package main
import (
"fmt"
"log"
"net/http"
"os"
"os/signal"
"net"
)
func handleRoot(w http.ResponseWriter, req *http.Request) {
w.WriteHeader(http.StatusOK)
}
func getListener() (net.Listener, error) {
listener, err := net.Listen("tcp", "127.0.0.1:0") // random available port
if err != nil {
return nil, fmt.Errorf("failed to obtain open port: %s", err)
}
return listener, nil
}
func startHTTPServer(ch chan error, l net.Listener) {
addr := l.Addr().String()
srv := http.Server{
Handler: http.HandlerFunc(handleRoot),
}
go func() {
log.Printf("starting an HTTP server on %s", addr)
if err := srv.Serve(l); err != nil {
log.Printf("HTTP server failed at runtime: %s", err)
ch <- err
}
}()
}
func main() {
l, err := getListener()
if err != nil {
log.Fatalf("failed to obtain listener: %s", err)
}
fail := make(chan error) // signal runtime failures to main
startHTTPServer(fail, l) // non-blocking
sig := make(chan os.Signal, 1)
signal.Notify(sig, os.Interrupt) // handle SIGINT
done := make(chan bool, 1) // initiate application shutdown
go func() {
for range sig {
close(done)
}
}()
select {
case <-done:
log.Println("Best HTTP server in the world is exiting (clean shutdown)...")
case err := <-fail:
log.Printf("Best HTTP server in the world is exiting (runtime exception)...: %s", err)
}
}