This repository has been archived by the owner on Mar 11, 2021. It is now read-only.
/
status.go
104 lines (88 loc) · 2.46 KB
/
status.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
package controller
import (
"fmt"
"time"
"github.com/fabric8-services/fabric8-cluster/app"
"github.com/fabric8-services/fabric8-common/log"
"github.com/goadesign/goa"
"github.com/jinzhu/gorm"
)
var (
// Commit current build commit set by build script
Commit = "0"
// BuildTime set by build script in ISO 8601 (UTC) format: YYYY-MM-DDThh:mm:ssTZD (see https://www.w3.org/TR/NOTE-datetime for details)
BuildTime = "0"
// StartTime in ISO 8601 (UTC) format
StartTime = time.Now().UTC().Format("2006-01-02T15:04:05Z")
)
type statusConfiguration interface {
DeveloperModeEnabled() bool
DefaultConfigurationError() error
}
// DBChecker is to be used to check if the DB is reachable
type DBChecker interface {
Ping() error
}
// StatusController implements the status resource.
type StatusController struct {
*goa.Controller
dbChecker DBChecker
config statusConfiguration
}
// NewStatusController creates a status controller.
func NewStatusController(service *goa.Service, dbChecker DBChecker, config statusConfiguration) *StatusController {
return &StatusController{
Controller: service.NewController("StatusController"),
dbChecker: dbChecker,
config: config,
}
}
// Show runs the show action.
func (c *StatusController) Show(ctx *app.ShowStatusContext) error {
res := &app.Status{
Commit: Commit,
BuildTime: BuildTime,
StartTime: StartTime,
}
devMode := c.config.DeveloperModeEnabled()
if devMode {
res.DevMode = &devMode
}
dbErr := c.dbChecker.Ping()
if dbErr != nil {
log.Error(ctx, map[string]interface{}{
"db_error": dbErr.Error(),
}, "database configuration error")
res.DatabaseStatus = fmt.Sprintf("Error: %s", dbErr.Error())
} else {
res.DatabaseStatus = "OK"
}
configErr := c.config.DefaultConfigurationError()
if configErr != nil {
log.Error(ctx, map[string]interface{}{
"config_error": configErr.Error(),
}, "configuration error")
res.ConfigurationStatus = fmt.Sprintf("Error: %s", configErr.Error())
} else {
res.ConfigurationStatus = "OK"
}
if dbErr != nil || (configErr != nil && !devMode) {
return ctx.ServiceUnavailable(res)
}
return ctx.OK(res)
}
// GormDBChecker implements DB checker
type GormDBChecker struct {
db *gorm.DB
}
// NewGormDBChecker constructs a new GormDBChecker
func NewGormDBChecker(db *gorm.DB) DBChecker {
return &GormDBChecker{
db: db,
}
}
//Ping pings the database to verify the connection
func (c *GormDBChecker) Ping() error {
_, err := c.db.DB().Exec("select 1")
return err
}