From fb7f88ff7e814b6968e5e73bbf569a8d3134b66c Mon Sep 17 00:00:00 2001 From: nquidox Date: Thu, 5 Mar 2026 16:20:47 +0300 Subject: [PATCH] conf for gin engine --- config/config.go | 22 ++++++++++++++++++---- main.go | 23 +++++++++++++++++++++++ secret.env | 3 +++ 3 files changed, 44 insertions(+), 4 deletions(-) diff --git a/config/config.go b/config/config.go index 116d96e..3e5f097 100644 --- a/config/config.go +++ b/config/config.go @@ -8,9 +8,10 @@ import ( ) type Config struct { - AppConf AppConfig - TgConf TelegramConfig - DsConf DiscordConfig + AppConf AppConfig + TgConf TelegramConfig + DsConf DiscordConfig + HttpConf HttpConfig } type AppConfig struct { @@ -28,10 +29,17 @@ type DiscordConfig struct { ChannelID snowflake.ID } +type HttpConfig struct { + Host string + Port string + GinMode string +} + +// prod config func NewConfig() *Config { return &Config{ AppConfig{ - LogLvl: getEnv("APP_LOG_LEVEL", "info"), + LogLvl: getEnv("APP_LOG_LEVEL", "debug"), }, TelegramConfig{ Token: getEnv("TELEGRAM_TOKEN", ""), @@ -42,6 +50,12 @@ func NewConfig() *Config { GuildID: convertID(getEnv("GUILD_ID", "")), ChannelID: convertID(getEnv("CHANNEL_ID", "")), }, + + HttpConfig{ + Host: getEnv("HTTP_HOST", "0.0.0.0"), + Port: getEnv("HTTP_PORT", "8080"), + GinMode: getEnv("GIN_MODE", "debug"), + }, } } diff --git a/main.go b/main.go index 17df0dd..0a2d0f1 100644 --- a/main.go +++ b/main.go @@ -3,13 +3,16 @@ package main import ( "context" log "github.com/sirupsen/logrus" + "net" "os" "os/signal" "syscall" "tg-disc-bot/config" "tg-disc-bot/discordBot" "tg-disc-bot/dto" + "tg-disc-bot/router" "tg-disc-bot/tgBot" + "time" ) func main() { @@ -38,13 +41,33 @@ func main() { tgMsgs := tgb.Start(fromDiscord) dsMsgs := dsb.Start(fromTelegram) + r := router.NewHandler(router.Deps{ + Addr: net.JoinHostPort(c.HttpConf.Host, c.HttpConf.Port), + GinMode: c.HttpConf.GinMode, + }) + log.Info("App is now running. Press CTRL-C to exit.") + errChan := make(chan error, 10) + + go func() { + if err = r.Run(); err != nil { + errChan <- err + } + }() + for { select { case sig := <-shutdown: { + shutdownCtx, shutdownCancel := context.WithTimeout(ctx, 15*time.Second) + + if err = r.Shutdown(shutdownCtx); err != nil { + log.WithError(err).Error("Error shutting down router") + } + log.WithField("type", sig).Info("terminating, close app") + shutdownCancel() os.Exit(0) } case <-ctx.Done(): diff --git a/secret.env b/secret.env index 5c9db77..987788d 100644 --- a/secret.env +++ b/secret.env @@ -1,5 +1,8 @@ #APP APP_LOG_LEVEL=info +HTTP_HOST=0.0.0.0 +HTTP_PORT=8080 +GIN_MODE=release #Telegram TELEGRAM_TOKEN=