-
Notifications
You must be signed in to change notification settings - Fork 13
/
main.go
95 lines (79 loc) · 1.78 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
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
package main
import (
"fmt"
"io/ioutil"
"log"
"net"
"net/http"
"os"
"strconv"
"strings"
"syscall"
)
func ListenFds() []*os.File {
// Minimal implementation of systemd's socket activation protocol
pid, err := strconv.Atoi(os.Getenv("LISTEN_PID"))
if err != nil || pid != os.Getpid() {
return nil
}
nfds, err := strconv.Atoi(os.Getenv("LISTEN_FDS"))
if err != nil || nfds == 0 {
return nil
}
files := []*os.File(nil)
for fd := 3; fd < 3+nfds; fd++ {
syscall.CloseOnExec(fd)
files = append(files, os.NewFile(uintptr(fd), ""))
}
return files
}
func handler(w http.ResponseWriter, r *http.Request) {
fn := os.Getenv("STATE_DIRECTORY") + "/counter"
file, err := os.OpenFile(fn, os.O_RDWR|os.O_CREATE, 0644)
if err != nil {
log.Fatal(err)
}
defer file.Close()
// Let's take a BSD lock to synchronize access to the counter file
err = syscall.Flock(int(file.Fd()), syscall.LOCK_EX)
if err != nil {
log.Fatal(err)
}
contents, err := ioutil.ReadAll(file)
if err != nil {
log.Fatal(err)
}
counter := 0
trimmed := strings.TrimSpace(string(contents))
if trimmed != "" {
counter, err = strconv.Atoi(trimmed)
if err != nil {
log.Fatal(err)
}
}
counter++
fmt.Fprintf(w, "Hello! You are visitor #%d!\n", counter)
file.Truncate(0)
file.Seek(0, 0)
fmt.Fprintf(file, "%d\n", counter)
}
func run_http(f *os.File) {
s, err := net.FileListener(f)
if err != nil {
log.Fatal(err)
}
log.Fatal(http.Serve(s, nil))
}
func main() {
http.HandleFunc("/", handler)
listeners := ListenFds()
if len(listeners) == 0 {
log.Fatal(http.ListenAndServe(":8080", nil))
} else {
// If multiple sockets are passed, spawn all but the last one as go-routine
for _, l := range listeners[:len(listeners)-1] {
go run_http(l)
}
run_http(listeners[len(listeners)-1])
}
}