Make the healthcheck endpoint work for non-global production instance usages to fix #35

This commit is contained in:
David Dworken 2022-11-16 20:58:19 -08:00
parent fd3d932e9d
commit 78d33cf437
No known key found for this signature in database

View File

@ -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{})