From f4368074a7cc07ad305ae9fb9b08d6b8dd3025eb Mon Sep 17 00:00:00 2001 From: Anhgelus Morhtuuzh Date: Thu, 22 Jan 2026 16:04:19 +0100 Subject: feat(context): create custom with config data in --- common/config.go | 64 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ common/context.go | 51 ++++++++++++++++++++++++++++++++++++++++++++ config.go | 64 ------------------------------------------------------- main.go | 17 +++++++++------ 4 files changed, 126 insertions(+), 70 deletions(-) create mode 100644 common/config.go create mode 100644 common/context.go delete mode 100644 config.go diff --git a/common/config.go b/common/config.go new file mode 100644 index 0000000..07cff8d --- /dev/null +++ b/common/config.go @@ -0,0 +1,64 @@ +package common + +import ( + "fmt" + "os" + + "github.com/pelletier/go-toml/v2" + "gorm.io/driver/postgres" + "gorm.io/gorm" +) + +type Config struct { + Debug bool `toml:"debug"` + Author string `toml:"author"` + Database *PostgresConfig `toml:"database"` +} + +type PostgresConfig struct { + Host string `toml:"host"` + User string `toml:"user"` + Password string `toml:"password"` + DBName string `toml:"db_name"` + Port int `toml:"port"` +} + +func (p *PostgresConfig) SetDefaultValues() { + p.Host = "localhost" + p.User = "" + p.Password = "" + p.DBName = "" + p.Port = 5432 +} + +func (p *PostgresConfig) Connect() (*gorm.DB, error) { + db, err := gorm.Open(postgres.Open(p.generateDsn()), &gorm.Config{}) + if err != nil { + return nil, err + } + return db, nil +} + +// generateDsn for the connection to postgres using the given config.SQLCredentials +func (p *PostgresConfig) generateDsn() string { + return fmt.Sprintf( + "host=%s user=%s password=%s dbname=%s port=%d sslmode=disable TimeZone=Europe/Paris", + p.Host, p.User, p.Password, p.DBName, p.Port, + ) +} + +func (c *Config) SetDefaultValues() { + c.Debug = false + c.Author = "anhgelus" + c.Database = &PostgresConfig{} + c.Database.SetDefaultValues() +} + +func LoadConfig(path string) (*Config, error) { + b, err := os.ReadFile(path) + if err != nil { + return nil, err + } + var cfg Config + return &cfg, toml.Unmarshal(b, &cfg) +} diff --git a/common/context.go b/common/context.go new file mode 100644 index 0000000..3a79264 --- /dev/null +++ b/common/context.go @@ -0,0 +1,51 @@ +package common + +import ( + "context" + + "gorm.io/gorm" +) + +type key uint8 + +const ( + keyDB key = 0 + keyDebug key = 1 + keyAuthor key = 2 +) + +func SetDB(ctx context.Context, db *gorm.DB) context.Context { + return context.WithValue(ctx, keyDB, db) +} + +func GetDB(ctx context.Context) *gorm.DB { + raw := ctx.Value(keyDB) + if raw == nil { + return nil + } + return raw.(*gorm.DB) +} + +func SetDebug(ctx context.Context, b bool) context.Context { + return context.WithValue(ctx, keyDebug, b) +} + +func IsDebug(ctx context.Context) bool { + raw := ctx.Value(keyDebug) + if raw == nil { + return false + } + return raw.(bool) +} + +func SetAuthor(ctx context.Context, s string) context.Context { + return context.WithValue(ctx, keyAuthor, s) +} + +func GetAuthor(ctx context.Context) string { + raw := ctx.Value(keyAuthor) + if raw == nil { + return "" + } + return raw.(string) +} diff --git a/config.go b/config.go deleted file mode 100644 index 725074c..0000000 --- a/config.go +++ /dev/null @@ -1,64 +0,0 @@ -package main - -import ( - "fmt" - "os" - - "github.com/pelletier/go-toml/v2" - "gorm.io/driver/postgres" - "gorm.io/gorm" -) - -type Config struct { - Debug bool `toml:"debug"` - Author string `toml:"author"` - Database *PostgresConfig `toml:"database"` -} - -type PostgresConfig struct { - Host string `toml:"host"` - User string `toml:"user"` - Password string `toml:"password"` - DBName string `toml:"db_name"` - Port int `toml:"port"` -} - -func (p *PostgresConfig) SetDefaultValues() { - p.Host = "localhost" - p.User = "" - p.Password = "" - p.DBName = "" - p.Port = 5432 -} - -func (p *PostgresConfig) Connect() (*gorm.DB, error) { - db, err := gorm.Open(postgres.Open(p.generateDsn()), &gorm.Config{}) - if err != nil { - return nil, err - } - return db, nil -} - -// generateDsn for the connection to postgres using the given config.SQLCredentials -func (p *PostgresConfig) generateDsn() string { - return fmt.Sprintf( - "host=%s user=%s password=%s dbname=%s port=%d sslmode=disable TimeZone=Europe/Paris", - p.Host, p.User, p.Password, p.DBName, p.Port, - ) -} - -func (c *Config) SetDefaultValues() { - c.Debug = false - c.Author = "anhgelus" - c.Database = &PostgresConfig{} - c.Database.SetDefaultValues() -} - -func getConfig(path string) (*Config, error) { - b, err := os.ReadFile(path) - if err != nil { - return nil, err - } - var cfg Config - return &cfg, toml.Unmarshal(b, &cfg) -} diff --git a/main.go b/main.go index 496309d..960f47e 100644 --- a/main.go +++ b/main.go @@ -18,7 +18,6 @@ import ( "git.anhgelus.world/anhgelus/les-copaings-bot/exp" "git.anhgelus.world/anhgelus/les-copaings-bot/rolereact" "git.anhgelus.world/anhgelus/les-copaings-bot/user" - "github.com/anhgelus/gokord" "github.com/anhgelus/gokord/cmd" _ "github.com/joho/godotenv/autoload" discordgo "github.com/nyttikord/gokord" @@ -72,7 +71,7 @@ func init() { func main() { flag.Parse() - cfg, err := getConfig(cfgPath) + cfg, err := common.LoadConfig(cfgPath) if err != nil { panic(err) } @@ -90,6 +89,9 @@ func main() { adm := int64(discord.PermissionManageGuild) ctx := user.SetState(context.Background(), user.NewState()) + ctx = common.SetDB(ctx, db) + ctx = common.SetDebug(ctx, cfg.Debug) + ctx = common.SetAuthor(ctx, cfg.Author) /*rankCmd := cmd.New("rank", "Affiche le niveau d'un copaing"). AddOption(cmd.NewOption( @@ -162,7 +164,7 @@ func main() { events.AddHandler(func(ctx context.Context, s bot.Session, e *event.Ready) { guildID := "" logger := bot.Logger(ctx) - if gokord.Debug { + if common.IsDebug(ctx) { gs, err := s.GuildAPI().UserGuilds(1, "", "", false).Do(ctx) if err != nil { logger.Error("fetching guilds for debug", "error", err) @@ -273,16 +275,19 @@ func main() { } } -func setupTimers(ctx context.Context, dg bot.Session) { +func setupTimers(ctx context.Context, dg *discordgo.Session) { d := 24 * time.Hour - if gokord.Debug { + debug := common.IsDebug(ctx) + if debug { d = 3 * exp.DebugFactor * time.Second } d2 := 30 * time.Minute - if gokord.Debug { + if debug { d2 = 1 * exp.DebugFactor * time.Second } + // because logger was never set in this context + ctx = bot.SetLogger(ctx, dg.Logger()) user.PeriodicReducer(ctx, dg) stopPeriodicReducer = common.NewTimer(ctx, d, func(ctx context.Context, _ context.CancelFunc) { -- cgit v1.2.3