-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathspork.go
executable file
·75 lines (57 loc) · 1.22 KB
/
spork.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
package main
//go:generate go run -tags=dev internal/assets/generate.go
//go:generate go run -tags=dev internal/templates/generate.go
import (
"context"
"flag"
"fmt"
"log"
"net/http"
"os"
"os/signal"
"runtime"
"syscall"
"time"
"go.tmthrgd.dev/spork/dbus"
)
var shutdown = make(chan struct{})
func init() {
log.SetFlags(0)
}
func main() {
runtime.SetBlockProfileRate(5)
runtime.SetMutexProfileFraction(5)
addr := flag.String("addr", ":8080", "the address to listen on")
flag.Parse()
if err := dbus.BusConnect(); err != nil {
log.Fatal(err)
}
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
fmt.Printf("Listening on %s\n", *addr)
srv := &http.Server{
Addr: *addr,
Handler: handler(ctx),
}
go func() {
if err := srv.ListenAndServe(); err != http.ErrServerClosed {
log.Fatal(err)
}
}()
// termination handler
term := make(chan os.Signal, 1)
signal.Notify(term, os.Interrupt, syscall.SIGTERM)
<-term
// gracefull shutdown
sctx, cancel := context.WithTimeout(ctx, 15*time.Second)
defer cancel()
go func() {
<-term
signal.Stop(term)
cancel()
}()
close(shutdown)
if err := srv.Shutdown(sctx); err != nil {
log.Printf("spork: error shutting down: %v", err)
}
}