diff options
Diffstat (limited to 'environment')
-rw-r--r-- | environment/environment.go | 58 |
1 files changed, 58 insertions, 0 deletions
diff --git a/environment/environment.go b/environment/environment.go new file mode 100644 index 0000000..a4ae719 --- /dev/null +++ b/environment/environment.go @@ -0,0 +1,58 @@ +// Package environment provides the Env type for commonly used data in the application. +package environment + +import ( + "database/sql" + + _ "github.com/go-sql-driver/mysql" + + "code.laria.me/laria.me/config" +) + +// Env provides commonly used data in the application +type Env struct { + configPath string + + config *config.Config + db *sql.DB +} + +func New(configPath string) *Env { + return &Env{ + configPath: configPath, + } +} + +func (e *Env) Config() (*config.Config, error) { + if e.config != nil { + return e.config, nil + } + + conf, err := config.LoadConfig(e.configPath) + if err != nil { + return nil, err + } + + e.config = conf + return conf, nil +} + +func (e *Env) DB() (*sql.DB, error) { + if e.db != nil { + return e.db, nil + } + + conf, err := e.Config() + if err != nil { + return nil, err + } + + var db *sql.DB + db, err = sql.Open("mysql", conf.DbDsn) + if err != nil { + return nil, err + } + + e.db = db + return db, nil +} |