-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
71 lines (57 loc) · 1.44 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
// Copyright © 2017-present Bjørn Erik Pedersen <[email protected]>.
//
// Use of this source code is governed by an MIT-style
// license that can be found in the LICENSE file.
package main
import (
"context"
"flag"
"net/http"
"os"
"os/signal"
"syscall"
"time"
"github.com/bep/twittercard-proxy/proxy"
)
var version = "master"
func main() {
var (
httpAddr string
cardsFile string
)
flag.StringVar(&httpAddr, "http", "0.0.0.0:1414", "The HTTP listen address")
flag.StringVar(&cardsFile, "f", "./twittercards.json", "The JSON filename with twitter cards")
flag.Parse()
p := proxy.NewTcProxy(cardsFile)
if err := p.Load(); err != nil {
p.Log.Fatal(err)
}
server := http.Server{
Addr: httpAddr,
Handler: p,
}
go func() {
p.Log.Fatal(server.ListenAndServe())
}()
p.Log.Printf("twittercard-proxy %s", version)
p.Log.Printf("HTTP listener on %s...", httpAddr)
signalChan := make(chan os.Signal, 1)
signal.Notify(signalChan, syscall.SIGINT, syscall.SIGTERM, syscall.SIGHUP)
for {
s := <-signalChan
switch s {
case syscall.SIGHUP:
if err := p.Load(); err != nil {
p.Log.Println("ERROR: Failed to reload twitter cards:", err)
}
default:
p.Log.Printf("Captured %v. Exiting...", s)
shutdownCtx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
server.Shutdown(shutdownCtx)
<-shutdownCtx.Done()
p.Log.Println(shutdownCtx.Err())
os.Exit(0)
}
}
}