-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatabase.go
57 lines (46 loc) · 1.26 KB
/
database.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
package main
import (
pg "github.com/go-pg/pg/v9"
"github.com/go-pg/pg/v9/orm"
"github.com/labstack/echo"
"github.com/xiorcale/rubus-api/models"
"golang.org/x/crypto/bcrypt"
)
var modelsList = []interface{}{
(*models.User)(nil),
(*models.Device)(nil),
}
func createSchema(db *pg.DB) error {
for _, model := range modelsList {
if err := db.CreateTable(model, &orm.CreateTableOptions{IfNotExists: true}); err != nil {
return err
}
}
return nil
}
func deleteSchema(db *pg.DB) error {
for _, model := range modelsList {
if err := db.DropTable(model, &orm.DropTableOptions{IfExists: true, Cascade: true}); err != nil {
return err
}
}
return nil
}
func createAdmin(s server) error {
dbCfg := s.cfg.Section("database")
admin := dbCfg.Key("admin_username").String()
email := dbCfg.Key("admin_email").String()
password := dbCfg.Key("admin_password").String()
cost, _ := s.cfg.Section("security").Key("hashcost").Int()
bytes, _ := bcrypt.GenerateFromPassword([]byte(password), cost)
user := models.User{
Username: admin,
Email: email,
PasswordHash: string(bytes),
Role: models.EnumRoleAdmin,
}
if jsonErr := models.AddUser(s.db, &user); jsonErr != nil {
return echo.NewHTTPError(jsonErr.Status, jsonErr)
}
return nil
}