-
Notifications
You must be signed in to change notification settings - Fork 0
/
db.go
70 lines (59 loc) · 1.62 KB
/
db.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
package main
import (
"encoding/json"
"log"
"os"
"time"
"github.com/pkg/errors"
"github.com/spf13/viper"
"gorm.io/driver/mysql"
"gorm.io/gorm"
"gorm.io/gorm/logger"
)
// Commit model
type Commit struct {
ID string `gorm:"primarykey" json:"id"`
CreatedAt time.Time `gorm:"index:,sort:desc,type:btree" json:"created_at"`
Message string `gorm:"size:255" json:"message"`
Author string `gorm:"size:255" json:"author"`
AvatarURL string `gorm:"size:255" json:"avatar_url"`
Link string `gorm:"size:255" json:"link"`
}
// MarshalJSON for api responses
func (c *Commit) MarshalJSON() ([]byte, error) {
return json.Marshal(struct {
Commit
CreatedAt string `json:"created_at"`
}{
*c,
c.PrintCreatedAt(),
})
}
// PrintCreatedAt formats the created at timestamp
func (c *Commit) PrintCreatedAt() string {
return c.CreatedAt.Format(time.RFC3339)
}
// SetupDB and return active connection
func SetupDB() (*gorm.DB, error) {
cfg := &gorm.Config{Logger: dbLogger()}
db, err := gorm.Open(mysql.Open(viper.GetString("database.url")), cfg)
if err != nil {
return nil, errors.Wrap(err, "failed to connect to database")
}
if viper.GetBool("debug") {
db = db.Debug()
}
// Migrate the schema
if err := db.AutoMigrate(&Commit{}); err != nil {
return nil, errors.Wrap(err, "failed to migrate database schema")
}
return db, nil
}
func dbLogger() logger.Interface {
return logger.New(log.New(os.Stdout, "\r\n", log.LstdFlags), logger.Config{
SlowThreshold: time.Second,
LogLevel: logger.Warn,
IgnoreRecordNotFoundError: false,
Colorful: true,
})
}