-
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.
* Adding Health endpoint * pr comments + 503 if not healthy * refactored admin server and api + health endpoint tests * fix health condition * fix admin routing * added comments + changed from ChainSync to ChainBootstrapStatus * Adding healthcheck for solo mode * adding solo + tests * fix log_level handler funcs * feat(admin): toggle api logs via admin API * feat(admin): add license headers * refactor health package + add p2p count * remove solo methods * moving health service to api pkg * added defaults + api health query * pr comments * pr comments --------- Co-authored-by: otherview <[email protected]>
- Loading branch information
1 parent
7579db4
commit de248a6
Showing
10 changed files
with
208 additions
and
14 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
// 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 apilogs | ||
|
||
import ( | ||
"net/http" | ||
"sync" | ||
"sync/atomic" | ||
|
||
"github.com/gorilla/mux" | ||
"github.com/vechain/thor/v2/api/utils" | ||
"github.com/vechain/thor/v2/log" | ||
) | ||
|
||
type APILogs struct { | ||
enabled *atomic.Bool | ||
mu sync.Mutex | ||
} | ||
|
||
type Status struct { | ||
Enabled bool `json:"enabled"` | ||
} | ||
|
||
func New(enabled *atomic.Bool) *APILogs { | ||
return &APILogs{ | ||
enabled: enabled, | ||
} | ||
} | ||
|
||
func (a *APILogs) Mount(root *mux.Router, pathPrefix string) { | ||
sub := root.PathPrefix(pathPrefix).Subrouter() | ||
sub.Path(""). | ||
Methods(http.MethodGet). | ||
Name("get-api-logs-enabled"). | ||
HandlerFunc(utils.WrapHandlerFunc(a.areAPILogsEnabled)) | ||
|
||
sub.Path(""). | ||
Methods(http.MethodPost). | ||
Name("post-api-logs-enabled"). | ||
HandlerFunc(utils.WrapHandlerFunc(a.setAPILogsEnabled)) | ||
} | ||
|
||
func (a *APILogs) areAPILogsEnabled(w http.ResponseWriter, _ *http.Request) error { | ||
a.mu.Lock() | ||
defer a.mu.Unlock() | ||
|
||
return utils.WriteJSON(w, Status{ | ||
Enabled: a.enabled.Load(), | ||
}) | ||
} | ||
|
||
func (a *APILogs) setAPILogsEnabled(w http.ResponseWriter, r *http.Request) error { | ||
a.mu.Lock() | ||
defer a.mu.Unlock() | ||
|
||
var req Status | ||
if err := utils.ParseJSON(r.Body, &req); err != nil { | ||
return utils.BadRequest(err) | ||
} | ||
a.enabled.Store(req.Enabled) | ||
|
||
log.Info("api logs updated", "pkg", "apilogs", "enabled", req.Enabled) | ||
|
||
return utils.WriteJSON(w, Status{ | ||
Enabled: a.enabled.Load(), | ||
}) | ||
} |
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,91 @@ | ||
// 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 apilogs | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"net/http" | ||
"net/http/httptest" | ||
"sync/atomic" | ||
"testing" | ||
|
||
"github.com/gorilla/mux" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
type TestCase struct { | ||
name string | ||
method string | ||
expectedHTTP int | ||
startValue bool | ||
expectedEndValue bool | ||
requestBody bool | ||
} | ||
|
||
func marshalBody(tt TestCase, t *testing.T) []byte { | ||
var reqBody []byte | ||
var err error | ||
if tt.method == "POST" { | ||
reqBody, err = json.Marshal(Status{Enabled: tt.requestBody}) | ||
if err != nil { | ||
t.Fatalf("could not marshal request body: %v", err) | ||
} | ||
} | ||
return reqBody | ||
} | ||
|
||
func TestLogLevelHandler(t *testing.T) { | ||
tests := []TestCase{ | ||
{ | ||
name: "Valid POST input - set logs to enabled", | ||
method: "POST", | ||
expectedHTTP: http.StatusOK, | ||
startValue: false, | ||
requestBody: true, | ||
expectedEndValue: true, | ||
}, | ||
{ | ||
name: "Valid POST input - set logs to disabled", | ||
method: "POST", | ||
expectedHTTP: http.StatusOK, | ||
startValue: true, | ||
requestBody: false, | ||
expectedEndValue: false, | ||
}, | ||
{ | ||
name: "GET request - get current level INFO", | ||
method: "GET", | ||
expectedHTTP: http.StatusOK, | ||
startValue: true, | ||
expectedEndValue: true, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
logLevel := atomic.Bool{} | ||
logLevel.Store(tt.startValue) | ||
|
||
reqBodyBytes := marshalBody(tt, t) | ||
|
||
req, err := http.NewRequest(tt.method, "/admin/apilogs", bytes.NewBuffer(reqBodyBytes)) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
rr := httptest.NewRecorder() | ||
router := mux.NewRouter() | ||
New(&logLevel).Mount(router, "/admin/apilogs") | ||
router.ServeHTTP(rr, req) | ||
|
||
assert.Equal(t, tt.expectedHTTP, rr.Code) | ||
responseBody := Status{} | ||
assert.NoError(t, json.Unmarshal(rr.Body.Bytes(), &responseBody)) | ||
assert.Equal(t, tt.expectedEndValue, responseBody.Enabled) | ||
}) | ||
} | ||
} |
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
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
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