-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
110 lines (97 loc) · 2.39 KB
/
Copy pathmain.go
File metadata and controls
110 lines (97 loc) · 2.39 KB
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
package main
import (
"fmt"
"log/syslog"
"os"
"os/signal"
"syscall"
"github.com/SoujiThenria/endsinger/database"
"github.com/SoujiThenria/endsinger/discord"
log "github.com/SoujiThenria/endsinger/logging"
"github.com/sevlyar/go-daemon"
)
const (
VERSION = "1.0.0"
)
func main() {
// Just for simplicity reasons and consistency.
var err error
// Get the configuration for the bot.
conf := getConfig()
// The log config changes if the bot is started as daemon.
log.Init(conf.LogLevel, conf.LogColor)
// Start as daemon.
if conf.Daemon {
// Use the systemlog facility to log to the daemon log file.
slog, err := syslog.New(syslog.LOG_DAEMON, "endsinger")
if err != nil {
fmt.Println("Cannot change the log output to the system log facility.", err)
os.Exit(1)
}
// Disable color and timestamps.
log.UseColor(false)
log.Timestamp(false)
log.SetOutput(slog)
// Daemon config
cntx := &daemon.Context{
PidFileName: conf.PidFile,
PidFilePerm: 0644,
Umask: 027,
}
d, err := cntx.Reborn()
if err != nil {
fmt.Println("Cannot start process as a daemon:", err)
os.Exit(1)
}
if d != nil {
return
}
defer cntx.Release()
log.Debug("Bot started as a daemon.")
}
log.Info("Starting endsinger VERSION-%s", VERSION)
// Database stuff
if err = database.New(conf.DBPath); err != nil {
log.Fatal("Cannot initialize the database: %s", err)
}
defer func() {
if err = database.Close(); err != nil {
log.Warn("While closing the database: %s", err)
}
}()
// Discord stuff
err = discord.New(&discord.Data{
Token: conf.BotToken,
})
// Set bot status if there is any.
if conf.BotStatus != "" {
err = discord.SetStatus(discord.StatusListen, conf.BotStatus)
if err != nil {
log.Warn("Cannot set the bot status: %s", err)
}
}
if err != nil {
log.Fatal("Cannot initialize discord: %s", err)
}
if err = discord.Startup(); err != nil {
log.Fatal("Cannot start the discord bot: %s", err)
}
defer func() {
if err = discord.Shutdown(); err != nil {
log.Error("Failed to stop the bot properly: %s", err)
}
}()
// Signal handler
log.Info("endsinger started.")
if conf.Daemon {
err := daemon.ServeSignals()
if err != nil {
log.Fatal("Daemon cannot serve signals: %s", err)
}
} else {
stop := make(chan os.Signal, 1)
signal.Notify(stop, syscall.SIGINT, syscall.SIGTERM)
<-stop
}
log.Info("Signal received, shutting down.")
}