Usage Examples¶
Send a Message via HTTP¶
package main
import (
"fmt"
"time"
homerun "github.com/stuttgart-things/homerun-library/v4"
)
func main() {
msg := homerun.Message{
Title: "Deployment Complete",
Message: "Service xyz deployed to production",
Severity: "success",
Author: "ci-pipeline",
Timestamp: time.Now().Format(time.RFC3339),
System: "production",
Tags: "deployment,production",
}
rendered, err := homerun.RenderBody(homerun.HomeRunBodyData, msg)
if err != nil {
panic(err)
}
resp, err := homerun.SendToHomerun(
"https://homerun.example.com/generic",
"my-auth-token",
[]byte(rendered),
false, // verify TLS
)
if err != nil {
panic(err)
}
if !resp.OK() {
panic("homerun rejected the message: " + resp.Status)
}
fmt.Printf("Status: %s\nBody: %s\n", resp.Status, string(resp.Body))
}
Create a Message with NewMessage¶
msg := homerun.NewMessage("ci-pipeline", "Build #42 completed", "info")
fmt.Printf("Created at: %s\n", msg.Timestamp)
Enqueue a Message into Redis Streams¶
package main
import (
"fmt"
homerun "github.com/stuttgart-things/homerun-library/v4"
)
func main() {
objectID, streamID, err := homerun.EnqueueMessageInRedisStreams(
homerun.Message{
Title: "Build Finished",
Message: "Build #42 completed successfully",
Severity: "info",
Author: "build-system",
System: "ci",
Tags: "build,ci",
},
homerun.RedisConfig{
Addr: "localhost",
Port: "6379",
Password: "",
Stream: "notifications",
},
)
if err != nil {
panic(err)
}
fmt.Printf("Stored as %s in stream %s\n", objectID, streamID)
}
Index a Message in RediSearch¶
package main
import homerun "github.com/stuttgart-things/homerun-library/v4"
func main() {
err := homerun.StoreInRediSearch(
homerun.Message{
Title: "Alert",
Message: "Disk usage above 90%",
Severity: "warning",
Author: "monitoring",
System: "infra",
Tags: "disk,alert",
},
homerun.RedisConfig{
Addr: "localhost",
Port: "6379",
Password: "",
Index: "messages-idx",
},
)
if err != nil {
panic(err)
}
}
Print a Table¶
package main
import (
"os"
"github.com/jedib0t/go-pretty/v6/table"
homerun "github.com/stuttgart-things/homerun-library/v4"
)
func main() {
homerun.PrintTableRows(
os.Stdout,
table.Row{"Service", "Status", "Version"},
[]table.Row{
{"homerun-api", "running", "v1.2.3"},
{"homerun-scout", "running", "v0.9.0"},
},
table.StyleLight,
)
}
Utility Functions¶
// Generate a UUID
id := homerun.GenerateUUID()
// Pick a random item
item := homerun.GetRandomObject([]string{"alpha", "beta", "gamma"})
// Get env var with fallback
addr := homerun.GetEnv("REDIS_ADDR", "localhost")
// Check if env var is set
if homerun.EnvVarExists("API_TOKEN") {
// ...
}
Wait for Redis at Startup¶
func main() {
homerun.SetLogger(slog.Default()) // to see the retries
rc := homerun.RedisConfig{Addr: "redis-stack", Port: "6379"}
timeout, err := homerun.LoadRedisStartupTimeout() // REDIS_STARTUP_TIMEOUT, default 120s
if err != nil {
log.Fatal(err)
}
ctx, stop := signal.NotifyContext(context.Background(), syscall.SIGTERM, os.Interrupt)
defer stop()
if err := homerun.WaitForRedisContext(ctx, rc, timeout); err != nil {
log.Fatal(err)
}
// ... open the HTTP port, start consuming
}
Any other dependency can be waited for with WaitForReady and a probe: