From 8f2ed68f5baf0639cf054e23c74d6f637bef33f5 Mon Sep 17 00:00:00 2001 From: furu04 Date: Thu, 25 Dec 2025 19:32:11 +0900 Subject: [PATCH] =?UTF-8?q?config=E6=A7=8B=E9=80=A0=E4=BD=93&=E8=AA=AD?= =?UTF-8?q?=E3=81=BF=E8=BE=BC=E3=81=BF=E5=87=A6=E7=90=86=E3=80=81DB?= =?UTF-8?q?=E6=8E=A5=E7=B6=9A=E5=87=A6=E7=90=86=E3=82=92=E5=AE=9F=E8=A3=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- internal/config/config.go | 39 ++++++++++++++++++++++++++++++++++ internal/database/database.go | 40 +++++++++++++++++++++++++++++++++++ 2 files changed, 79 insertions(+) diff --git a/internal/config/config.go b/internal/config/config.go index e69de29..44a656b 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -0,0 +1,39 @@ +type Config struct { + DB struct { + Type string `ini:"DBTYPE"` + Host string `ini:"DBHOST"` + Port int `ini:"DBPORT"` + User string `ini:"DBUSER"` + Password string `ini:"DBPASS"` + Name string `ini:"DBNAME"` + } `ini:"db"` + Server struct { + Host string `ini:"SERVERHOST"` + AllowOrigin string `ini:"ALLOWORIGIN"` + } `ini:"server"` + Security struct { + AISpamProtection bool `ini:"AI_SPAM_PROTECTION"` + AIProvider string `ini:"AI_PROVIDER"` + AIEndpoint string `ini:"AI_ENDPOINT"` + AIAPIKey string `ini:"AI_APIKEY"` + AISecretKey string `ini:"AI_SECRETKEY"` + AIModel string `ini:"AI_MODEL"` + AIInstructions string `ini:"AI_INSTURCTIONS"` + CaptchaEnabled bool `ini:"CAPTCHA_ENABLED"` + CaptchaType string `ini:"CAPTCHA_TYPE"` + CaptchaSiteKey string `ini:"CAPTCHA_SITEKEY"` + CaptchaSecretKey string `ini:"CAPTCHA_SECRETKEY"` + } `ini:"security"` +} + +import "gopkg.in/ini.v1" + +func LoadConfig(filePath string) (*Config, error) { + cfg, err := ini.Load(filePath) + if err != nil { + return nil, err + } + config := &Config{} + err = cfg.MapTo(config) + return config, err +} \ No newline at end of file diff --git a/internal/database/database.go b/internal/database/database.go index e69de29..9e2f08f 100644 --- a/internal/database/database.go +++ b/internal/database/database.go @@ -0,0 +1,40 @@ +import ( + "database/sql" + "fmt" + _ "github.com/go-sql-driver/mysql" + _ "github.com/mattn/go-sqlite3" +) + +type Database struct { + *sql.DB +} + +func NewDatabase(config *Config) (*Database, error) { + var dsn string + if config.DB.Type == "mysql" { + dsn = fmt.Sprintf("%s:%s@tcp(%s:%d)/%s?charset=utf8mb4&parseTime=True&loc=Local", + config.DB.User, config.DB.Password, config.DB.Host, config.DB.Port, config.DB.Name) + } else if config.DB.Type == "sqlite" { + dsn = config.DB.Name + ".db" + } else { + return nil, fmt.Errorf("unsupported database type: %s", config.DB.Type) + } + + db, err := sql.Open(config.DB.Type, dsn) + if err != nil { + return nil, err + } + + if err := db.Ping(); err != nil { + return nil, err + } + + db.SetMaxOpenConns(25) + db.SetMaxIdleConns(25) + + return &Database{DB: db}, nil +} + +func (d *Database) Close() error { + return d.DB.Close() +} \ No newline at end of file