mirror of
https://github.com/ddworken/hishtory.git
synced 2025-02-02 11:39:24 +01:00
Make the healthcheck endpoint work for non-global production instance usages to fix #35
This commit is contained in:
parent
fd3d932e9d
commit
78d33cf437
@ -342,14 +342,24 @@ func addDeletionRequestHandler(w http.ResponseWriter, r *http.Request) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func healthCheckHandler(w http.ResponseWriter, r *http.Request) {
|
func healthCheckHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
var count int64
|
db, err := GLOBAL_DB.DB()
|
||||||
checkGormResult(GLOBAL_DB.Model(&shared.EncHistoryEntry{}).Count(&count))
|
if err != nil {
|
||||||
if count < 100 {
|
panic(fmt.Sprintf("failed to get DB: %v", err))
|
||||||
panic("Suspiciously few enc history entries!")
|
|
||||||
}
|
}
|
||||||
checkGormResult(GLOBAL_DB.Model(&shared.Device{}).Count(&count))
|
err = db.Ping()
|
||||||
if count < 50 {
|
if err != nil {
|
||||||
panic("Suspiciously few devices!")
|
panic(fmt.Sprintf("failed to ping DB: %v", err))
|
||||||
|
}
|
||||||
|
if isProductionEnvironment(r) {
|
||||||
|
var count int64
|
||||||
|
checkGormResult(GLOBAL_DB.Model(&shared.EncHistoryEntry{}).Count(&count))
|
||||||
|
if count < 100 {
|
||||||
|
panic("Suspiciously few enc history entries!")
|
||||||
|
}
|
||||||
|
checkGormResult(GLOBAL_DB.Model(&shared.Device{}).Count(&count))
|
||||||
|
if count < 50 {
|
||||||
|
panic("Suspiciously few devices!")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
ok := "OK"
|
ok := "OK"
|
||||||
w.Write([]byte(ok))
|
w.Write([]byte(ok))
|
||||||
@ -373,6 +383,10 @@ func isTestEnvironment() bool {
|
|||||||
return os.Getenv("HISHTORY_TEST") != ""
|
return os.Getenv("HISHTORY_TEST") != ""
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func isProductionEnvironment(r *http.Request) bool {
|
||||||
|
return r.Host == "api.hishtory.dev"
|
||||||
|
}
|
||||||
|
|
||||||
func OpenDB() (*gorm.DB, error) {
|
func OpenDB() (*gorm.DB, error) {
|
||||||
if isTestEnvironment() {
|
if isTestEnvironment() {
|
||||||
db, err := gorm.Open(sqlite.Open("file::memory:?cache=shared"), &gorm.Config{})
|
db, err := gorm.Open(sqlite.Open("file::memory:?cache=shared"), &gorm.Config{})
|
||||||
|
Loading…
Reference in New Issue
Block a user