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 { type Config struct {
AppConf AppConfig AppConf AppConfig
TgConf TelegramConfig TgConf TelegramConfig
DsConf DiscordConfig DsConf DiscordConfig
HttpConf HttpConfig
} }
type AppConfig struct { type AppConfig struct {
@ -28,10 +29,17 @@ type DiscordConfig struct {
ChannelID snowflake.ID ChannelID snowflake.ID
} }
type HttpConfig struct {
Host string
Port string
GinMode string
}
// prod config
func NewConfig() *Config { func NewConfig() *Config {
return &Config{ return &Config{
AppConfig{ AppConfig{
LogLvl: getEnv("APP_LOG_LEVEL", "info"), LogLvl: getEnv("APP_LOG_LEVEL", "debug"),
}, },
TelegramConfig{ TelegramConfig{
Token: getEnv("TELEGRAM_TOKEN", ""), Token: getEnv("TELEGRAM_TOKEN", ""),
@ -42,6 +50,12 @@ func NewConfig() *Config {
GuildID: convertID(getEnv("GUILD_ID", "")), GuildID: convertID(getEnv("GUILD_ID", "")),
ChannelID: convertID(getEnv("CHANNEL_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 ( import (
"context" "context"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
"net"
"os" "os"
"os/signal" "os/signal"
"syscall" "syscall"
"tg-disc-bot/config" "tg-disc-bot/config"
"tg-disc-bot/discordBot" "tg-disc-bot/discordBot"
"tg-disc-bot/dto" "tg-disc-bot/dto"
"tg-disc-bot/router"
"tg-disc-bot/tgBot" "tg-disc-bot/tgBot"
"time"
) )
func main() { func main() {
@ -38,13 +41,33 @@ func main() {
tgMsgs := tgb.Start(fromDiscord) tgMsgs := tgb.Start(fromDiscord)
dsMsgs := dsb.Start(fromTelegram) 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.") 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 { for {
select { select {
case sig := <-shutdown: 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") log.WithField("type", sig).Info("terminating, close app")
shutdownCancel()
os.Exit(0) os.Exit(0)
} }
case <-ctx.Done(): case <-ctx.Done():

View file

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