aboutsummaryrefslogtreecommitdiff
path: root/config.go
diff options
context:
space:
mode:
authorAnhgelus Morhtuuzh <anhgelus@anhgelus.world>2025-05-21 17:28:18 +0200
committerAnhgelus Morhtuuzh <anhgelus@anhgelus.world>2025-05-21 17:28:18 +0200
commit642025681befa35f67cc7c25383eb782ac01feac (patch)
tree6c2b1fdb2712300cdd4615de8d8cae06e8b382af /config.go
parent24f0db37218c4050e01edaa031410382aa252324 (diff)
build(gokord): upgrade to 0.7.0
Diffstat (limited to 'config.go')
-rw-r--r--config.go80
1 files changed, 80 insertions, 0 deletions
diff --git a/config.go b/config.go
new file mode 100644
index 0000000..421f1b1
--- /dev/null
+++ b/config.go
@@ -0,0 +1,80 @@
+package main
+
+import (
+ "fmt"
+ "github.com/anhgelus/gokord"
+ "github.com/pelletier/go-toml/v2"
+ "gorm.io/driver/postgres"
+ "gorm.io/gorm"
+)
+
+type Config struct {
+ Debug bool `toml:"debug"`
+ Author string `toml:"author"`
+ Redis *gokord.RedisCredentials `toml:"redis"`
+ 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) IsDebug() bool {
+ return c.Debug
+}
+
+func (c *Config) GetAuthor() string {
+ return c.Author
+}
+
+func (c *Config) GetRedisCredentials() *gokord.RedisCredentials {
+ return c.Redis
+}
+
+func (c *Config) SetDefaultValues() {
+ c.Debug = false
+ c.Author = "anhgelus"
+ c.Redis = &gokord.RedisCredentials{}
+ c.Redis.SetDefaultValues()
+ c.Database = &PostgresConfig{}
+ c.Database.SetDefaultValues()
+}
+
+func (c *Config) GetSQLCredentials() gokord.SQLCredentials {
+ return c.Database
+}
+
+func (c *Config) Marshal() ([]byte, error) {
+ return toml.Marshal(c)
+}
+
+func (c *Config) Unmarshal(bytes []byte) error {
+ return toml.Unmarshal(bytes, c)
+}