-
Notifications
You must be signed in to change notification settings - Fork 17
/
setup.go
88 lines (72 loc) · 2.11 KB
/
setup.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package main
import (
"fmt"
"io/ioutil"
"github.com/gopheracademy/congo/models"
"github.com/jinzhu/gorm"
jg "github.com/dgrijalva/jwt-go"
)
func pubKey(*jg.Token) (interface{}, error) {
return ioutil.ReadFile("keys/gc.rsa.pub")
}
func readPubKey() ([]byte, error) {
return ioutil.ReadFile("keys/gc.rsa.pub")
}
func privateKey() (interface{}, error) {
return ioutil.ReadFile("keys/gc.rsa")
}
func envFromSettings() (*Env, error) {
var e Env
constr := fmt.Sprintf("user=%s host=%s port=%d dbname=%s password=%s sslmode=disable",
settings.DatabaseUsername,
settings.DatabaseHost,
settings.DatabasePort,
settings.DatabaseName,
settings.DatabasePassword)
db, err := gorm.Open("postgres", constr)
if err != nil {
return nil, err
}
db.DB().SetMaxIdleConns(settings.MaxIdleConns)
db.DB().SetMaxOpenConns(settings.MaxOpenConns)
db.LogMode(settings.Debug)
e.DB = *db
return &e, nil
}
// Migrate creates seed records and updates the database schema
func Migrate(e *Env) {
// autoMigrate(e, &AccessToken{}, &RefreshToken{})
autoMigrate(e, &models.Tenant{})
autoMigrate(e, &models.User{})
autoMigrate(e, &models.Series{})
autoMigrate(e, &models.Event{})
autoMigrate(e, &models.Speaker{})
autoMigrate(e, &models.Presentation{})
createTenants(e.DB)
createUsers(e.DB)
createSeries(e.DB)
createEvents(e.DB)
}
func autoMigrate(e *Env, values ...interface{}) {
for _, value := range values {
e.DB.AutoMigrate(value)
}
}
// Storage is an interface for database access
type Storage interface{}
func makeStorage(e *Env) map[string]Storage {
storage := make(map[string]Storage, 0)
userStorage := models.NewUserDB(e.DB)
storage["USERSTORAGE"] = userStorage
tenantStorage := models.NewTenantDB(e.DB)
storage["TENANTSTORAGE"] = tenantStorage
eventStorage := models.NewEventDB(e.DB)
storage["EVENTSTORAGE"] = eventStorage
speakerStorage := models.NewSpeakerDB(e.DB)
storage["SPEAKERSTORAGE"] = speakerStorage
presentationStorage := models.NewPresentationDB(e.DB)
storage["PRESENTATIONSTORAGE"] = presentationStorage
seriesStorage := models.NewSeriesDB(e.DB)
storage["SERIESSTORAGE"] = seriesStorage
return storage
}