Files
yhy-mager/backend/internal/httpapi/readiness.go
xiewenjun 54c318c615
Some checks failed
Check / delivery-static (push) Has been cancelled
Check / api (push) Has been cancelled
Check / web (push) Has been cancelled
feat: implement license platform business flows and delivery tooling
Add admin and customer interfaces, orders, signed activation and leases, protected package delivery, keyring verification, migrations, security controls, tests and deployment documentation.

Co-Authored-By: Craft Agent <agents-noreply@craft.do>
2026-10-09 15:33:22 +08:00

48 lines
1.0 KiB
Go

package httpapi
import (
"context"
"database/sql"
"net/http"
"time"
"github.com/gin-gonic/gin"
)
const RequiredSchemaVersion = 10
func RegisterReadiness(r *gin.Engine, db *sql.DB) {
r.GET("/api/v1/health/ready", func(c *gin.Context) {
ctx, cancel := context.WithTimeout(c.Request.Context(), 2*time.Second)
defer cancel()
if err := CheckSchema(ctx, db); err != nil {
c.JSON(http.StatusServiceUnavailable, gin.H{"status": "not_ready"})
return
}
c.JSON(http.StatusOK, gin.H{"status": "ready"})
})
}
func CheckSchema(ctx context.Context, db *sql.DB) error {
if db == nil {
return sql.ErrConnDone
}
var version uint64
var dirty bool
rows, err := db.QueryContext(ctx, "SELECT version, dirty FROM schema_migrations")
if err != nil {
return err
}
defer rows.Close()
if !rows.Next() {
return sql.ErrNoRows
}
if err := rows.Scan(&version, &dirty); err != nil {
return err
}
if rows.Next() || rows.Err() != nil || version != RequiredSchemaVersion || dirty {
return sql.ErrNoRows
}
return nil
}