-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(cmd): transform into a cli app
BREAKING CHANGE: The behavior of running the executable was changed.
- Loading branch information
1 parent
cacca98
commit 12bbfe9
Showing
7 changed files
with
180 additions
and
58 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
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/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() | ||
log := utils.Logger | ||
mainLogger := log.Named("Main") | ||
mainLogger.Info("Starting server") | ||
config.Load(log, cmd) | ||
router := getRouter(log) | ||
|
||
_, err := bot.StartClient(log) | ||
if err != nil { | ||
log.Info(err.Error()) | ||
return | ||
} | ||
cache.InitCache(log) | ||
bot.StartWorkers(log) | ||
bot.StartUserBot(log) | ||
mainLogger.Info("Server started", zap.Int("port", config.ValueOf.Port)) | ||
mainLogger.Info("File Stream Bot", zap.String("version", versionString)) | ||
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters