mirror of
https://github.com/studiokaiji/nostr-webhost.git
synced 2025-12-18 15:24:19 +01:00
Merge pull request #68 from studiokaiji/feature-healthcheck
Feature-healthcheck
This commit is contained in:
@@ -41,7 +41,6 @@ COPY --from=builder /app/hostr .
|
|||||||
|
|
||||||
# Create directories for data and config
|
# Create directories for data and config
|
||||||
RUN mkdir -p /app/data /home/hostr/.nostr-webhost && \
|
RUN mkdir -p /app/data /home/hostr/.nostr-webhost && \
|
||||||
chmod +x /app/docker-entrypoint.sh && \
|
|
||||||
chown -R hostr:hostr /app /home/hostr
|
chown -R hostr:hostr /app /home/hostr
|
||||||
|
|
||||||
# Switch to non-root user
|
# Switch to non-root user
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
"strings"
|
"strings"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
"github.com/nbd-wtf/go-nostr"
|
"github.com/nbd-wtf/go-nostr"
|
||||||
@@ -28,6 +29,56 @@ func Start(port string, mode string) {
|
|||||||
|
|
||||||
fmt.Println("[Hostr] Using relays:", strings.Join(allRelays, ", "))
|
fmt.Println("[Hostr] Using relays:", strings.Join(allRelays, ", "))
|
||||||
|
|
||||||
|
// Health check endpoint
|
||||||
|
r.GET("/health", func(ctx *gin.Context) {
|
||||||
|
// Test connection to relays by creating a simple filter
|
||||||
|
testCtx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
||||||
|
defer cancel()
|
||||||
|
|
||||||
|
// Try to connect to at least one relay
|
||||||
|
activeRelays := []string{}
|
||||||
|
for _, relay := range allRelays {
|
||||||
|
_, err := pool.EnsureRelay(relay)
|
||||||
|
if err == nil {
|
||||||
|
activeRelays = append(activeRelays, relay)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
healthStatus := gin.H{
|
||||||
|
"status": "healthy",
|
||||||
|
"mode": mode,
|
||||||
|
"relays": gin.H{
|
||||||
|
"configured": allRelays,
|
||||||
|
"active": activeRelays,
|
||||||
|
"count": len(activeRelays),
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
// If no relays are active, return unhealthy status
|
||||||
|
if len(activeRelays) == 0 {
|
||||||
|
healthStatus["status"] = "unhealthy"
|
||||||
|
healthStatus["error"] = "No active relay connections"
|
||||||
|
ctx.JSON(http.StatusServiceUnavailable, healthStatus)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// Try a simple query to verify relay connectivity
|
||||||
|
testFilter := nostr.Filter{
|
||||||
|
Kinds: []int{consts.KindWebhostHTML},
|
||||||
|
Limit: 1,
|
||||||
|
}
|
||||||
|
|
||||||
|
testEv := pool.QuerySingle(testCtx, activeRelays[:1], testFilter)
|
||||||
|
if testEv != nil || testCtx.Err() == context.DeadlineExceeded {
|
||||||
|
// Either found an event or timeout (both indicate relay is responding)
|
||||||
|
healthStatus["relay_check"] = "responsive"
|
||||||
|
} else {
|
||||||
|
healthStatus["relay_check"] = "no_response"
|
||||||
|
}
|
||||||
|
|
||||||
|
ctx.JSON(http.StatusOK, healthStatus)
|
||||||
|
})
|
||||||
|
|
||||||
r.GET("/e/:hex_or_nevent", func(ctx *gin.Context) {
|
r.GET("/e/:hex_or_nevent", func(ctx *gin.Context) {
|
||||||
hexOrNevent := ctx.Param("hex_or_nevent")
|
hexOrNevent := ctx.Param("hex_or_nevent")
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user