openethereum-node-status/internal/server/router.go

58 lines
1.2 KiB
Go
Raw Normal View History

2021-12-14 12:24:18 +01:00
package server
import (
2021-12-14 15:50:07 +01:00
"net/http"
2021-12-14 12:24:18 +01:00
"github.com/gin-gonic/gin"
)
func Start(port string, ginMode string) error {
gin.SetMode(ginMode)
gin.DisableConsoleColor()
router := gin.Default()
router.GET("/metrics", metricsHandler)
router.GET("/health", healthHandler)
return router.Run(port)
}
2021-12-14 15:50:07 +01:00
func healthHandler(c *gin.Context) {
2021-12-14 16:29:30 +01:00
allOk, data := batchHealthCall()
2021-12-14 15:50:07 +01:00
if !allOk {
2021-12-14 16:03:47 +01:00
// choose a better status code if available
2021-12-15 10:57:47 +01:00
c.JSON(http.StatusServiceUnavailable, gin.H{
2021-12-14 15:50:07 +01:00
"message": "could not get node health status",
})
return
}
c.JSON(http.StatusOK, gin.H{
"syncComplete": data.SyncComplete,
"chainName": data.ChainName,
})
}
func metricsHandler(c *gin.Context) {
2021-12-14 16:29:30 +01:00
allOk, data := batchMetricsCall()
if !allOk {
// choose a better status code if available
2021-12-15 10:57:47 +01:00
c.JSON(http.StatusServiceUnavailable, gin.H{
2021-12-14 16:29:30 +01:00
"message": "could not get node metrics",
})
return
}
2021-12-14 15:50:07 +01:00
c.JSON(http.StatusOK, gin.H{
2021-12-15 10:57:47 +01:00
"gasPrice": data.GasPrice,
"blockNumber": data.BlockNumber,
"peerCount": data.PeerCount,
"pendingTransactionsCount": data.PendingTxCount,
"pendingTransactions": data.PendingTx,
"enode": data.Enode,
2021-12-14 15:50:07 +01:00
})
}