summaryrefslogtreecommitdiff
path: root/environment/environment.go
diff options
context:
space:
mode:
authorLaria Carolin Chabowski <laria@laria.me>2021-01-01 14:46:54 +0100
committerLaria Carolin Chabowski <laria@laria.me>2021-01-01 14:46:54 +0100
commit85473656174b1b1d6221d3bb76cc12fa5f7f7e8d (patch)
tree37349eee4f64781f06e8597dc4c457801eb03a47 /environment/environment.go
downloadlaria.me-85473656174b1b1d6221d3bb76cc12fa5f7f7e8d.tar.gz
laria.me-85473656174b1b1d6221d3bb76cc12fa5f7f7e8d.tar.bz2
laria.me-85473656174b1b1d6221d3bb76cc12fa5f7f7e8d.zip
Initial commit
Diffstat (limited to 'environment/environment.go')
-rw-r--r--environment/environment.go58
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
+}