conf for gin engine

This commit is contained in:
nquidox 2026-03-05 16:20:47 +03:00
parent 5f72059bb6
commit fb7f88ff7e
3 changed files with 44 additions and 4 deletions

View file

@ -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"),
},
}
}

23
main.go
View file

@ -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():

View file

@ -1,5 +1,8 @@
#APP
APP_LOG_LEVEL=info
HTTP_HOST=0.0.0.0
HTTP_PORT=8080
GIN_MODE=release
#Telegram
TELEGRAM_TOKEN=