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) {
var count int64
checkGormResult(GLOBAL_DB.Model(&shared.EncHistoryEntry{}).Count(&count))
if count < 100 {
panic("Suspiciously few enc history entries!")
db, err := GLOBAL_DB.DB()
if err != nil {
panic(fmt.Sprintf("failed to get DB: %v", err))
}
checkGormResult(GLOBAL_DB.Model(&shared.Device{}).Count(&count))
if count < 50 {
panic("Suspiciously few devices!")
err = db.Ping()
if err != nil {
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"
w.Write([]byte(ok))
@ -373,6 +383,10 @@ func isTestEnvironment() bool {
return os.Getenv("HISHTORY_TEST") != ""
}
func isProductionEnvironment(r *http.Request) bool {
return r.Host == "api.hishtory.dev"
}
func OpenDB() (*gorm.DB, error) {
if isTestEnvironment() {
db, err := gorm.Open(sqlite.Open("file::memory:?cache=shared"), &gorm.Config{})