From f21cb5dcb8fed8f0572a4468813744854b67b411 Mon Sep 17 00:00:00 2001 From: Maksim Pischulenok Date: Tue, 22 Nov 2022 23:31:16 +0300 Subject: [PATCH] Improve error logging --- .gitignore | 3 ++- main.go | 51 +++++++++++++++++++++++++++++---------------------- 2 files changed, 31 insertions(+), 23 deletions(-) diff --git a/.gitignore b/.gitignore index 2eea525..d70ad07 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ -.env \ No newline at end of file +.env +data \ No newline at end of file diff --git a/main.go b/main.go index cea1f3c..e27a631 100644 --- a/main.go +++ b/main.go @@ -2,16 +2,17 @@ package main import ( "fmt" - "gorm.io/driver/sqlite" - "gorm.io/gorm" - "gorm.io/gorm/clause" "html" "log" "os" - "path" + "path/filepath" "strings" "time" + "gorm.io/driver/sqlite" + "gorm.io/gorm" + "gorm.io/gorm/clause" + tele "gopkg.in/telebot.v3" ) @@ -25,14 +26,20 @@ var DB *gorm.DB func ConnectDB() { var err error - DB, err = gorm.Open(sqlite.Open(path.Join("data", "db.sqlite3")), &gorm.Config{}) + + err = os.MkdirAll(filepath.Join(".", "data"), os.ModePerm) if err != nil { - panic("failed to connect database") + log.Fatal(err) + } + + DB, err = gorm.Open(sqlite.Open(filepath.Join(".", "data", "db.sqlite3")), &gorm.Config{}) + if err != nil { + log.Fatal(err) } err = DB.AutoMigrate(&ChatUser{}) if err != nil { - panic("failed to migrate") + log.Fatal(err) } } @@ -42,23 +49,11 @@ func InitBot() *tele.Bot { Poller: &tele.LongPoller{Timeout: 10 * time.Second}, }) if err != nil { - panic(err) + log.Fatal(err) } return b } -func main() { - ConnectDB() - b := InitBot() - b.Use(logger) - b.Handle("/start", handleStart) - b.Handle("/in", handleIn) - b.Handle("/out", handleOut) - b.Handle("/all", handleAll) - b.Handle("/stats", handleStats) - b.Start() -} - func logger(next tele.HandlerFunc) tele.HandlerFunc { return func(c tele.Context) error { log.Println("user", c.Sender().ID, "sent", c.Message().Text) @@ -118,8 +113,8 @@ func handleAll(c tele.Context) error { if end > len(mentions) { end = len(mentions) } - err := c.Send(strings.Join(mentions[i:end], " "), tele.ModeHTML) - if err != nil { + + if err := c.Send(strings.Join(mentions[i:end], " "), tele.ModeHTML); err != nil { return err } } @@ -138,3 +133,15 @@ func handleStats(c tele.Context) error { msg := fmt.Sprintf("`Users: %6d\nChats: %6d\nGroups: %6d`", userCount, chatCount, groupCount) return c.Send(msg, tele.ModeMarkdownV2) } + +func main() { + ConnectDB() + b := InitBot() + b.Use(logger) + b.Handle("/start", handleStart) + b.Handle("/in", handleIn) + b.Handle("/out", handleOut) + b.Handle("/all", handleAll) + b.Handle("/stats", handleStats) + b.Start() +}