forked from systemli/ticker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
127 lines (105 loc) · 2.94 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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
package main
import (
"context"
"flag"
"net/http"
"os"
"os/signal"
"time"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/systemli/ticker/internal/api"
"github.com/systemli/ticker/internal/bridge"
"github.com/systemli/ticker/internal/config"
"github.com/systemli/ticker/internal/storage"
"github.com/sethvargo/go-password/password"
log "github.com/sirupsen/logrus"
)
var (
GitCommit string
GitVersion string
)
func main() {
var configPath string
flag.StringVar(&configPath, "config", "config.yml", "path to config.yml")
flag.Parse()
config := config.LoadConfig(configPath)
//TODO: Improve startup routine
if config.TelegramEnabled() {
user, err := bridge.BotUser(config.TelegramBotToken)
if err != nil {
log.WithError(err).Error("Unable to retrieve the user information for the Telegram Bot")
} else {
config.TelegramBotUser = user
}
}
lvl, err := log.ParseLevel(config.LogLevel)
if err != nil {
panic(err)
}
if config.LogFormat == "json" {
log.SetFormatter(&log.JSONFormatter{})
} else {
log.SetFormatter(&log.TextFormatter{FullTimestamp: true})
}
log.SetLevel(lvl)
log.Infof("starting ticker api on %s", config.Listen)
if GitCommit != "" && GitVersion != "" {
log.Infof("build info: %s (commit: %s)", GitVersion, GitCommit)
}
go func() {
http.Handle("/metrics", promhttp.Handler())
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
_, _ = w.Write([]byte(`<html>
<head><title>Ticker Metrics Exporter</title></head>
<body>
<h1>Ticker Metrics Exporter</h1>
<p><a href="/metrics">Metrics</a></p>
</body>
</html>`))
})
log.Fatal(http.ListenAndServe(config.MetricsListen, nil))
}()
store := storage.NewStorage(config.Database, config.UploadPath)
router := api.API(config, store)
server := &http.Server{
Addr: config.Listen,
Handler: router,
}
firstRun(store, config)
go func() {
if err := server.ListenAndServe(); err != nil && err != http.ErrServerClosed {
log.Fatal(err)
}
}()
// Wait for interrupt signal to gracefully shutdown the server with a timeout of 5 seconds.
quit := make(chan os.Signal, 1)
signal.Notify(quit, os.Interrupt)
<-quit
log.Infoln("Shutdown Ticker")
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
if err := server.Shutdown(ctx); err != nil {
log.Fatal(err)
}
}
func firstRun(store storage.TickerStorage, config config.Config) {
count, err := store.CountUser()
if err != nil {
log.Fatal("error using database")
}
if count == 0 {
pw, err := password.Generate(24, 3, 3, false, false)
if err != nil {
log.Fatal(err)
}
user, err := storage.NewAdminUser(config.Initiator, pw)
if err != nil {
log.Fatal("could not create first user")
}
err = store.SaveUser(&user)
if err != nil {
log.Fatal("could not persist first user")
}
log.WithField("email", user.Email).WithField("password", pw).Info("admin user created (change password now!)")
}
}