-
Notifications
You must be signed in to change notification settings - Fork 251
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'pedro/health_endpoint' into darren/admin-api-log-toggler
- Loading branch information
Showing
18 changed files
with
370 additions
and
335 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
// Copyright (c) 2024 The VeChainThor developers | ||
|
||
// Distributed under the GNU Lesser General Public License v3.0 software license, see the accompanying | ||
// file LICENSE or <https://www.gnu.org/licenses/lgpl-3.0.html> | ||
|
||
package health | ||
|
||
import ( | ||
"net/http" | ||
"strconv" | ||
"time" | ||
|
||
"github.com/gorilla/mux" | ||
"github.com/vechain/thor/v2/api/utils" | ||
) | ||
|
||
type API struct { | ||
healthStatus *Health | ||
} | ||
|
||
func NewAPI(healthStatus *Health) *API { | ||
return &API{ | ||
healthStatus: healthStatus, | ||
} | ||
} | ||
|
||
func (h *API) handleGetHealth(w http.ResponseWriter, r *http.Request) error { | ||
// Parse query parameters | ||
query := r.URL.Query() | ||
|
||
// Default to constants if query parameters are not provided | ||
blockTolerance := defaultBlockTolerance | ||
minPeerCount := defaultMinPeerCount | ||
|
||
// Override with query parameters if they exist | ||
if queryBlockTolerance := query.Get("blockTolerance"); queryBlockTolerance != "" { | ||
if parsed, err := time.ParseDuration(queryBlockTolerance); err == nil { | ||
blockTolerance = parsed | ||
} | ||
} | ||
|
||
if queryMinPeerCount := query.Get("minPeerCount"); queryMinPeerCount != "" { | ||
if parsed, err := strconv.Atoi(queryMinPeerCount); err == nil { | ||
minPeerCount = parsed | ||
} | ||
} | ||
|
||
acc, err := h.healthStatus.Status(blockTolerance, minPeerCount) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if !acc.Healthy { | ||
w.WriteHeader(http.StatusServiceUnavailable) // Set the status to 503 | ||
} else { | ||
w.WriteHeader(http.StatusOK) // Set the status to 200 | ||
} | ||
return utils.WriteJSON(w, acc) | ||
} | ||
|
||
func (h *API) Mount(root *mux.Router, pathPrefix string) { | ||
sub := root.PathPrefix(pathPrefix).Subrouter() | ||
|
||
sub.Path(""). | ||
Methods(http.MethodGet). | ||
Name("health"). | ||
HandlerFunc(utils.WrapHandlerFunc(h.handleGetHealth)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
// Copyright (c) 2024 The VeChainThor developers | ||
|
||
// Distributed under the GNU Lesser General Public License v3.0 software license, see the accompanying | ||
// file LICENSE or <https://www.gnu.org/licenses/lgpl-3.0.html> | ||
|
||
package health | ||
|
||
import ( | ||
"encoding/json" | ||
"io" | ||
"net/http" | ||
"net/http/httptest" | ||
"testing" | ||
|
||
"github.com/gorilla/mux" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
"github.com/vechain/thor/v2/comm" | ||
"github.com/vechain/thor/v2/test/testchain" | ||
"github.com/vechain/thor/v2/txpool" | ||
) | ||
|
||
var ts *httptest.Server | ||
|
||
func TestHealth(t *testing.T) { | ||
initAPIServer(t) | ||
|
||
var healthStatus Status | ||
respBody, statusCode := httpGet(t, ts.URL+"/health") | ||
require.NoError(t, json.Unmarshal(respBody, &healthStatus)) | ||
assert.False(t, healthStatus.Healthy) | ||
assert.Equal(t, http.StatusServiceUnavailable, statusCode) | ||
} | ||
|
||
func initAPIServer(t *testing.T) { | ||
thorChain, err := testchain.NewIntegrationTestChain() | ||
require.NoError(t, err) | ||
|
||
router := mux.NewRouter() | ||
NewAPI( | ||
New(thorChain.Repo(), comm.New(thorChain.Repo(), txpool.New(thorChain.Repo(), nil, txpool.Options{}))), | ||
).Mount(router, "/health") | ||
|
||
ts = httptest.NewServer(router) | ||
} | ||
|
||
func httpGet(t *testing.T, url string) ([]byte, int) { | ||
res, err := http.Get(url) //#nosec G107 | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
defer res.Body.Close() | ||
|
||
r, err := io.ReadAll(res.Body) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
return r, res.StatusCode | ||
} |
Oops, something went wrong.