jlelse
/
kis3
Archived
1
Fork 0
This repository has been archived on 2021-01-04. You can view files and clone it, but cannot push or open issues or pull requests.
kis3/main.go

75 lines
1.4 KiB
Go
Raw Normal View History

2019-04-02 14:28:06 +00:00
package main
import (
"fmt"
"log"
"net/http"
2019-05-26 20:32:02 +00:00
"os"
"os/signal"
"syscall"
2019-10-13 11:13:11 +00:00
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api"
"github.com/gobuffalo/packr/v2"
"github.com/gorilla/mux"
2019-04-02 14:28:06 +00:00
)
type kis3 struct {
router *mux.Router
staticBox *packr.Box
2019-05-27 13:03:30 +00:00
tgBot *tgbotapi.BotAPI
2019-04-02 14:28:06 +00:00
}
var (
2019-05-01 09:46:52 +00:00
app = &kis3{
staticBox: packr.New("staticFiles", "./static"),
}
2019-04-02 14:28:06 +00:00
)
2019-10-13 11:13:11 +00:00
func main() {
// Init
2019-05-26 20:32:02 +00:00
initConfig()
e := initDatabase()
2019-04-02 14:28:06 +00:00
if e != nil {
log.Fatal("Database setup failed:", e)
}
2019-05-26 20:32:02 +00:00
initRouter()
2019-05-27 13:03:30 +00:00
initTelegramBot()
2019-10-13 11:13:11 +00:00
// Start
2019-05-26 20:32:02 +00:00
go startListeningToWeb()
go startReports()
2019-05-27 13:03:30 +00:00
go startStatsTelegram()
2019-05-26 20:32:02 +00:00
// Graceful stop
var gracefulStop = make(chan os.Signal, 1)
signal.Notify(gracefulStop, os.Interrupt, syscall.SIGTERM)
sig := <-gracefulStop
fmt.Printf("Received signal: %+v", sig)
os.Exit(0)
2019-04-02 14:28:06 +00:00
}
2019-05-26 20:32:02 +00:00
func initRouter() {
app.router = mux.NewRouter()
2019-05-26 20:32:02 +00:00
initStatsRouter()
initTrackingRouter()
2019-04-02 14:28:06 +00:00
}
2019-05-27 13:03:30 +00:00
func initTelegramBot() {
if appConfig.TGBotToken == "" {
fmt.Println("Telegram not configured.")
return
}
bot, e := tgbotapi.NewBotAPI(appConfig.TGBotToken)
if e != nil {
fmt.Println("Failed to setup Telegram:", e)
return
}
fmt.Println("Authorized Telegram bot on account", bot.Self.UserName)
app.tgBot = bot
}
2019-05-26 20:32:02 +00:00
func startListeningToWeb() {
2019-05-01 09:46:52 +00:00
port := appConfig.Port
2019-04-02 14:28:06 +00:00
addr := ":" + port
fmt.Printf("Listening to %s\n", addr)
log.Fatal(http.ListenAndServe(addr, app.router))
2019-04-02 14:28:06 +00:00
}