/
main.go
85 lines (67 loc) · 2.01 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
package main
import (
"flag"
"fmt"
"io/ioutil"
"net/http"
"os"
"strconv"
"sync"
"gitlab.com/shuhao/towncrier/backend"
_ "gitlab.com/shuhao/towncrier/sqlite_backend"
"gitlab.com/shuhao/towncrier/webreceiver"
"github.com/Sirupsen/logrus"
"github.com/facebookgo/grace/gracehttp"
)
// Flag variables
var configPath string
var pidPath string
// Configuration variables
var applicationConfig *ApplicationConfig
var realLogger = logrus.New()
var logger = realLogger.WithField("component", "main")
func init() {
flag.StringVar(&configPath, "config", "", "the master config file for the server")
flag.StringVar(&pidPath, "pidfile", "", "the pid file path for the server")
flag.Parse()
if configPath == "" || !pathExists(configPath) {
logger.WithField("path", configPath).Panic("config path not valid")
}
var err error
pid := os.Getpid()
if pidPath != "" {
logger.WithFields(logrus.Fields{
"path": pidPath,
"pid": pid,
}).Info("creating pid file")
err = ioutil.WriteFile(pidPath, []byte(strconv.Itoa(pid)), 0644)
if err != nil {
logger.WithField("error", err).Panic("cannot write to pid file")
}
}
applicationConfig, err = NewApplicationConfig(configPath)
if err != nil {
logger.WithField("error", err).Panic("cannot parse application config")
}
applicationConfig.Notifiers.HookAllNotifiers()
}
func main() {
wg := &sync.WaitGroup{}
notificationBackend := backend.GetBackend(applicationConfig.BackendName)
err := notificationBackend.Initialize(applicationConfig.BackendOpenString)
if err != nil {
logger.WithField("error", err).Panic("cannot initialize backend")
}
notificationBackend.Start(wg)
defer func() {
notificationBackend.Shutdown()
wg.Wait()
}()
notificationBackend.BlockUntilReady()
receiverApp := webreceiver.NewApp(notificationBackend, applicationConfig.Receiver)
receiverServer := &http.Server{
Addr: fmt.Sprintf("%s:%d", applicationConfig.Receiver.ListenHost, applicationConfig.Receiver.ListenPort),
Handler: receiverApp,
}
gracehttp.Serve(receiverServer)
}