-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
112 lines (92 loc) · 2.54 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
package main
import (
"encoding/json"
"fmt"
"runtime"
"strings"
"time"
log "github.com/sirupsen/logrus"
"github.com/Kardbord/Kard-bot/kardbot"
"github.com/Kardbord/Kard-bot/kardbot/config"
// For runtime profiling if enabled in config
"net/http"
_ "net/http/pprof"
)
const MainConfigFile = "config/setup.json"
func init() {
log.SetReportCaller(true)
log.SetFormatter(&log.TextFormatter{
FullTimestamp: true,
TimestampFormat: time.UnixDate,
CallerPrettyfier: func(f *runtime.Frame) (string, string) {
split := strings.Split(f.File, "Kard-bot/")
filename := "Kard-bot/" + split[len(split)-1]
return "", fmt.Sprintf("%s:%d", filename, f.Line)
},
})
cfg := struct {
DefaultLogLvl string `json:"default-log-level"`
}{"info"}
jsonCfg, err := config.NewJsonConfig(MainConfigFile)
if err != nil {
log.Fatal(err)
}
err = json.Unmarshal(jsonCfg.Raw, &cfg)
if err != nil {
log.Fatal(err)
}
if lvl, err := log.ParseLevel(cfg.DefaultLogLvl); err == nil {
log.SetLevel(lvl)
} else {
log.SetLevel(log.InfoLevel)
log.Warnf(`Could not read default log level from config (%s). Defaulting to "%s".`, cfg.DefaultLogLvl, log.InfoLevel)
}
}
// Start an http server for pprof profiling data
// if configured. No-op if not.
// See https://pkg.go.dev/net/http/pprof
var pprofServe = func() {
log.Info("pprof not enabled (this is normal)")
}
var PprofCfg = pprofConfig{}
type pprofConfig struct {
Enabled bool `json:"enabled"`
Address string `json:"address"`
BlockProfileRate int `json:"block-profile-rate"`
MutexProfileFraction int `json:"mutex-profile-fraction"`
}
func init() {
jsonCfg, err := config.NewJsonConfig(MainConfigFile)
if err != nil {
log.Fatal(err)
}
{
cfg := struct {
pprofConfig `json:"pprof"`
}{}
err = json.Unmarshal(jsonCfg.Raw, &cfg)
if err != nil {
log.Fatal(err)
}
PprofCfg = cfg.pprofConfig
}
if !PprofCfg.Enabled {
return
}
if PprofCfg.Address == "" {
log.Warn("pprof is enabled but address is not set, not starting pprof server")
return
}
log.Infof("Setting block profile rate to %d", PprofCfg.BlockProfileRate)
runtime.SetBlockProfileRate(PprofCfg.BlockProfileRate)
log.Infof("Setting mutex profile fraction to %d", PprofCfg.MutexProfileFraction)
runtime.SetMutexProfileFraction(PprofCfg.MutexProfileFraction)
pprofServe = func() {
log.Infof("Starting pprof server at %s/debug/pprof/", PprofCfg.Address)
log.Info(http.ListenAndServe(PprofCfg.Address, nil))
}
}
func main() {
go pprofServe()
kardbot.RunAndBlock(PprofCfg.Enabled)
}