2020-10-11 19:32:58 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"flag"
|
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
"os/signal"
|
|
|
|
"syscall"
|
2020-10-12 13:47:28 +00:00
|
|
|
|
|
|
|
"subscribe-bot/config"
|
|
|
|
"subscribe-bot/db"
|
|
|
|
"subscribe-bot/discord"
|
|
|
|
"subscribe-bot/osuapi"
|
|
|
|
"subscribe-bot/scrape"
|
2020-10-11 19:32:58 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var exit_chan = make(chan int)
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
configPath := flag.String("config", "config.toml", "Path to the config file (defaults to config.toml)")
|
|
|
|
flag.Parse()
|
|
|
|
|
2020-10-12 13:47:28 +00:00
|
|
|
config, err := config.ReadConfig(*configPath)
|
2020-10-11 19:32:58 +00:00
|
|
|
|
2020-10-12 13:47:28 +00:00
|
|
|
api := osuapi.New(&config)
|
2020-10-11 19:32:58 +00:00
|
|
|
|
2020-10-12 13:47:28 +00:00
|
|
|
db, err := db.OpenDb("db", api)
|
2020-10-11 19:32:58 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2020-10-12 12:23:32 +00:00
|
|
|
log.Println("opened db")
|
2020-10-11 19:32:58 +00:00
|
|
|
|
2020-10-12 13:47:28 +00:00
|
|
|
bot, err := discord.NewBot(&config, db, api)
|
2020-10-11 19:32:58 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2020-10-12 13:47:28 +00:00
|
|
|
go scrape.RunScraper(bot, db, api)
|
2020-10-11 19:32:58 +00:00
|
|
|
|
|
|
|
signal_chan := make(chan os.Signal, 1)
|
|
|
|
signal.Notify(signal_chan,
|
|
|
|
syscall.SIGHUP,
|
|
|
|
syscall.SIGINT,
|
|
|
|
syscall.SIGTERM,
|
|
|
|
syscall.SIGQUIT)
|
|
|
|
go func() {
|
|
|
|
for {
|
|
|
|
s := <-signal_chan
|
|
|
|
switch s {
|
|
|
|
case syscall.SIGHUP:
|
|
|
|
fallthrough
|
|
|
|
case syscall.SIGINT:
|
|
|
|
fallthrough
|
|
|
|
case syscall.SIGTERM:
|
|
|
|
fallthrough
|
|
|
|
case syscall.SIGQUIT:
|
|
|
|
exit_chan <- 0
|
|
|
|
default:
|
|
|
|
exit_chan <- 1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
code := <-exit_chan
|
|
|
|
|
|
|
|
db.Close()
|
|
|
|
bot.Close()
|
2020-10-12 13:47:28 +00:00
|
|
|
scrape.Ticker.Stop()
|
2020-10-11 19:32:58 +00:00
|
|
|
os.Exit(code)
|
|
|
|
}
|