forked from EverythingSuckz/TG-FileStreamBot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun.go
More file actions
78 lines (70 loc) · 1.99 KB
/
run.go
File metadata and controls
78 lines (70 loc) · 1.99 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
package main
import (
"EverythingSuckz/fsb/config"
"EverythingSuckz/fsb/internal/bot"
"EverythingSuckz/fsb/internal/cache"
"EverythingSuckz/fsb/internal/routes"
"EverythingSuckz/fsb/internal/types"
"EverythingSuckz/fsb/internal/uploader"
"EverythingSuckz/fsb/internal/utils"
"fmt"
"net/http"
"time"
"github.com/spf13/cobra"
"github.com/gin-gonic/gin"
"go.uber.org/zap"
)
var runCmd = &cobra.Command{
Use: "run",
Short: "Run the bot with the given configuration.",
DisableSuggestions: false,
Run: runApp,
}
var startTime time.Time = time.Now()
func runApp(cmd *cobra.Command, args []string) {
utils.InitLogger(config.ValueOf.Dev)
log := utils.Logger
mainLogger := log.Named("Main")
mainLogger.Info("Starting server")
config.Load(log, cmd)
router := getRouter(log)
mainBot, err := bot.StartClient(log)
if err != nil {
log.Panic("Failed to start main bot", zap.Error(err))
}
uploader.Load(log, mainBot.Dispatcher)
cache.InitCache(log)
workers, err := bot.StartWorkers(log)
if err != nil {
log.Panic("Failed to start workers", zap.Error(err))
return
}
workers.AddDefaultClient(mainBot, mainBot.Self)
bot.StartUserBot(log)
mainLogger.Info("Server started", zap.Int("port", config.ValueOf.Port))
mainLogger.Info("File Stream Bot", zap.String("version", versionString))
mainLogger.Sugar().Infof("Server is running at %s", config.ValueOf.Host)
err = router.Run(fmt.Sprintf(":%d", config.ValueOf.Port))
if err != nil {
mainLogger.Sugar().Fatalln(err)
}
}
func getRouter(log *zap.Logger) *gin.Engine {
if config.ValueOf.Dev {
gin.SetMode(gin.DebugMode)
} else {
gin.SetMode(gin.ReleaseMode)
}
router := gin.Default()
router.Use(gin.ErrorLogger())
router.GET("/", func(ctx *gin.Context) {
ctx.JSON(http.StatusOK, types.RootResponse{
Message: "Server is running.",
Ok: true,
Uptime: utils.TimeFormat(uint64(time.Since(startTime).Seconds())),
Version: versionString,
})
})
routes.Load(log, router)
return router
}