test app with postgres and redis healthchecks
This commit is contained in:
139
main.go
Normal file
139
main.go
Normal file
@@ -0,0 +1,139 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"fmt"
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
"time"
|
||||
|
||||
_ "github.com/lib/pq"
|
||||
"github.com/redis/go-redis/v9"
|
||||
)
|
||||
|
||||
type result struct {
|
||||
Name string
|
||||
OK bool
|
||||
Message string
|
||||
}
|
||||
|
||||
func env(key, fallback string) string {
|
||||
if v := os.Getenv(key); v != "" {
|
||||
return v
|
||||
}
|
||||
return fallback
|
||||
}
|
||||
|
||||
func testPostgres() result {
|
||||
dsn := fmt.Sprintf("host=%s port=%s user=%s password=%s dbname=%s sslmode=disable",
|
||||
env("POSTGRES_HOST", "localhost"),
|
||||
env("POSTGRES_PORT", "5432"),
|
||||
env("POSTGRES_USER", "app"),
|
||||
env("POSTGRES_PASSWORD", ""),
|
||||
env("POSTGRES_DB", "app"),
|
||||
)
|
||||
db, err := sql.Open("postgres", dsn)
|
||||
if err != nil {
|
||||
return result{"PostgreSQL", false, fmt.Sprintf("open: %v", err)}
|
||||
}
|
||||
defer db.Close()
|
||||
db.SetConnMaxLifetime(5 * time.Second)
|
||||
|
||||
_, err = db.Exec(`CREATE TABLE IF NOT EXISTS healthcheck (id SERIAL PRIMARY KEY, checked_at TIMESTAMPTZ DEFAULT NOW(), msg TEXT)`)
|
||||
if err != nil {
|
||||
return result{"PostgreSQL", false, fmt.Sprintf("create table: %v", err)}
|
||||
}
|
||||
|
||||
_, err = db.Exec(`INSERT INTO healthcheck (msg) VALUES ($1)`, fmt.Sprintf("ping at %s", time.Now().Format(time.RFC3339)))
|
||||
if err != nil {
|
||||
return result{"PostgreSQL", false, fmt.Sprintf("insert: %v", err)}
|
||||
}
|
||||
|
||||
var count int
|
||||
err = db.QueryRow(`SELECT COUNT(*) FROM healthcheck`).Scan(&count)
|
||||
if err != nil {
|
||||
return result{"PostgreSQL", false, fmt.Sprintf("count: %v", err)}
|
||||
}
|
||||
|
||||
return result{"PostgreSQL", true, fmt.Sprintf("OK — %d rows in healthcheck", count)}
|
||||
}
|
||||
|
||||
func testRedis() result {
|
||||
addr := fmt.Sprintf("%s:%s",
|
||||
env("REDIS_HOST", "localhost"),
|
||||
env("REDIS_PORT", "6379"),
|
||||
)
|
||||
rdb := redis.NewClient(&redis.Options{Addr: addr})
|
||||
defer rdb.Close()
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
||||
defer cancel()
|
||||
|
||||
err := rdb.Set(ctx, "test-key", "hello from test-app", 0).Err()
|
||||
if err != nil {
|
||||
return result{"Redis", false, fmt.Sprintf("SET: %v", err)}
|
||||
}
|
||||
|
||||
val, err := rdb.Get(ctx, "test-key").Result()
|
||||
if err != nil {
|
||||
return result{"Redis", false, fmt.Sprintf("GET: %v", err)}
|
||||
}
|
||||
|
||||
return result{"Redis", true, fmt.Sprintf("OK — GET test-key = %q", val)}
|
||||
}
|
||||
|
||||
func handler(w http.ResponseWriter, r *http.Request) {
|
||||
if r.URL.Path == "/healthz" {
|
||||
w.WriteHeader(200)
|
||||
fmt.Fprint(w, "ok")
|
||||
return
|
||||
}
|
||||
|
||||
pg := testPostgres()
|
||||
rd := testRedis()
|
||||
|
||||
w.Header().Set("Content-Type", "text/html; charset=utf-8")
|
||||
fmt.Fprintf(w, `<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<title>StackOps Test App</title>
|
||||
<style>
|
||||
body { font-family: 'JetBrains Mono', monospace; background: #0a0a0a; color: #e5e5e5; padding: 40px; }
|
||||
h1 { color: #4ade80; }
|
||||
.test { margin: 16px 0; padding: 16px; border-radius: 8px; border: 1px solid #333; }
|
||||
.ok { border-color: #166534; background: #052e16; }
|
||||
.fail { border-color: #7f1d1d; background: #1c0a0a; }
|
||||
.label { font-size: 14px; color: #9ca3af; }
|
||||
.status { font-size: 18px; margin-top: 4px; }
|
||||
.ok .status { color: #4ade80; }
|
||||
.fail .status { color: #f87171; }
|
||||
.time { color: #6b7280; font-size: 12px; margin-top: 24px; }
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<h1>StackOps Test App</h1>
|
||||
<p style="color:#9ca3af">Dependency connectivity checks</p>
|
||||
`)
|
||||
for _, t := range []result{pg, rd} {
|
||||
cls := "ok"
|
||||
if !t.OK {
|
||||
cls = "fail"
|
||||
}
|
||||
fmt.Fprintf(w, ` <div class="test %s">
|
||||
<div class="label">%s</div>
|
||||
<div class="status">%s</div>
|
||||
</div>
|
||||
`, cls, t.Name, t.Message)
|
||||
}
|
||||
fmt.Fprintf(w, ` <div class="time">Checked at %s</div>
|
||||
</body>
|
||||
</html>`, time.Now().Format(time.RFC3339))
|
||||
}
|
||||
|
||||
func main() {
|
||||
http.HandleFunc("/", handler)
|
||||
port := env("PORT", "8080")
|
||||
log.Printf("test-app listening on :%s", port)
|
||||
log.Fatal(http.ListenAndServe(":"+port, nil))
|
||||
}
|
||||
Reference in New Issue
Block a user